Quantcast
Channel: X-Squared On Demand »» Configuration
Viewing all articles
Browse latest Browse all 20

Workflow ISCHANGED() translated to Apex trigger

$
0
0

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){
			DateTime dt = i.Date_and_Time__c;
			if(Trigger.oldMap.get(i.id).Date_and_Time__c != dt){
				// 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!


Viewing all articles
Browse latest Browse all 20

Trending Articles