• Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?

X-Squared On Demand

Salesforce solutions delivered

  • Home
  • About
    • Leadership
    • Partners
    • Blogroll
  • Force-Squared Blog
    • Tips and Tricks
    • Configuration
    • Development
  • Support
    • Knowledge Base
    • Submit a Case
  • Is It Dreamforce Yet?
  • Tips and Tricks
  • Configuration
  • Development
You are here: Home / Salesforce CRM / Development / Apex / Workflow ISCHANGED() translated to Apex trigger

Workflow ISCHANGED() translated to Apex trigger

February 2, 2012 by David Schach 2 Comments

Workflow is great. I can simply and declaratively make changes, and can easily update things like email templates, criteria, tasks, etc. without using Eclipse and writing/running unit tests.
Sometimes, however, workflow isn’t enough; we need to use a trigger.

Today, I had a use-case that when a DateTime field is filled, a contact (identified via a lookup on a parent object – so a grandparent record) should receive a notification. That’s easy enough to do with a ISCHANGED(DateTimeField__c) workflow criteron, but the Email Alert can’t “find” the contact. A trigger is necessary.

Here’s how I coded the trigger:

trigger InterviewUpdates on Interview__c (after update, before update) {
  if(Trigger.IsBefore){
     for(Interview__c i : Trigger.New){
       if(i.Date_and_Time__c != Trigger.oldMap.get(i.id).Date_and_Time__c){
         // Send email to applicant, passing Interview, DateTime, and Contact
         // (which we found via a query coded outside the loop to avoid governor limits)
        }
     }
  }
}

I’ve left out a lot of parts here, but I hope that the main bit of using Trigger.oldMap to find the old value and compare it to the new one comes through.

Happy coding!

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to email a link to a friend (Opens in new window) Email

Related

Filed Under: Apex, Configuration, Tips and Tricks Tagged With: Apex

← Best Practice: Multiple Chatter Posts of the Same File Create and Populate a Map Without Loops →

Comments

  1. E.J. Wilburn says

    February 2, 2012 at 08:22

    Trigger.New and Trigger.Old are in the same order, you can do this a little faster and easier like this:

    trigger InterviewUpdates on Interview__c (after update, before update) {
    if(Trigger.IsBefore){
    for ( Integer x = 0; x < Trigger.New.size(); x++ )
    if ( Trigger.New[x].Date_and_Time__c != Trigger.Old[x].Date_and_Time__c ) {
    // Send email to applicant, passing Interview, DateTime, and Contact
    // (which we found via a query coded outside the loop to avoid governor limits)
    }
    }
    }
    }

    Reply
    • David Schach says

      February 2, 2012 at 08:32

      Interesting. We differ in that I don’t think initializing a counter and doing a for loop like that is at all easier. In general, keeping variables to a minimum seems a best-practice.
      We get the same result, but I think mine doesn’t add confusion with ‘x’ floating around.

      Reply

Share Your ThoughtsCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Is it Dreamforce Yet?

Find out!

Find us on the AppExchange

Mass Update Contacts 2.0

Get It Now

Recent Posts

  • Prevent Duplicate Emails on Leads
  • Duplicate Record Item Enrichment and Auto-Deletion Code
  • Lightning Component With Running User Information
  • ChatterBINGO is Now Open-Source
  • Display Only My Role’s Records on a Report

Post Categories

Popular Tags

#df09 #df10 Akismet Apex AppBuilder AppExchange Appirio Astadia Blogs Chatter Configuration Content DreamFactory Dreamforce Eclipse IDE Enterprise Force.com Builder Force.com Platform Google Infowelders Integration Just for fun Lightning New Developments New Features Partner Program PersonAccount Projects Publisher Salesforce Salesforce1 Salesforce for Google Apps sfdcverse Sites Visualforce Web-to X-Squared On Demand

Find Us Online

  • Twitter
  • Facebook
  • LinkedIn
  • RSS

Subscribe

RSS Feed Comments

Subscribe via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 164 other subscribers

Copyright © 2008–2025 X-Squared On Demand · Genesis Framework by StudioPress · WordPress · Log in