Those of you who follow Steve Andersen’s blog may have seen his post re: cron jobs using Apex in April 2007. Kudos to you, Steve for a fantastic post (better late than never!) not only because of its universal application, but also because it got me thinking of other roundabout ways of utilizing Apex.
Over the last several months, I’ve had several clients request that a record be checked for certain conditions after a certain amount of time has elapsed. While the solution to this requirement may be intuitive to some of us seasoned Salesforce.com fanboys, I figured I’d walk through it for those new to the platform.
Sample Requirement: After a Lead is inserted, wait three days, then check if any tasks have been associated with this record. If not, send an email alert to the user’s manager.
First, we create a custom checkbox field on Leads called “Check for Activity”. Then, create a workflow rule that runs when a Lead is inserted (add any additional filters if necessary). Add to this workflow rule a time-based field update that runs three days after the Lead was inserted. The field update will mark the “Check for Activity” checkbox “TRUE”. Then, we create an Apex trigger that runs After Update on Leads:
trigger checkForLeadActivity on Lead (after update) {
for (Lead l : trigger.new) {
if (l.check_for_activity__c == true) {
//run our code
}
}
}
Obviously, I’ve left you high and dry on the code, but if you need more assistance feel free to reach out to me and I’d be happy to walk you through the code.
Happy Apex-ing!
*EDIT
I want to make sure I stress the importance of coding your triggers for bulk processing. The code block above should actually read like so:
trigger checkForLeadActivity on Lead (after update) {
Lead[] leads = new Lead[]{};
for (Lead l : trigger.new) {
if (l.check_for_activity__c == true) {
leads.add(l);
}
}
//process leads outside the for loop
//to avoid governor limits on things like SOQL queries
}
Tags:
Banter
No comments for “running a trigger via workflow”
Post a comment