Wednesday, April 09, 2008 cron

Time-based workflows in salesforce currently execute relative to a time. For example, you can use them to to schedule a task for the case owner to follow up with the customer two days after a
case is closed.

It's not currently possible to schedule time-based workflows to execute at a particular time every day - like a cron job would. For that, we'd need to add some secret sauce to time-based workflows.

The Apex trigger below will populate a custom DateTime field called Midnight_Local_Time__c with, you guessed it, midnight - local time for the running user. In conjunction with a time-based workflow, this trigger would allow you to do things like lock all 'Closed' Case records at midnight
trigger Case_PopulateMidnightLocalTime on Case (after update)
List cases = new List();
for(Case c : [SELECT CreatedDate FROM Case WHERE Id IN AND IsClosed = true AND Midnight_Local_Time__c = null])
CaseTriggerHelper.SetMidnightTime(c, c.CreatedDate);


update cases;

public static void SetMidnightTime(Case c, DateTime dt)
// SFDC defaults 'Midnight' to the 'Time' for this call to newInstance();
DateTime midnightLocalTime = DateTime.newInstance(dt.year(), dt.month(),;
midnightLocalTime = midnightLocalTime.addDays(1).addMinutes(-1);
c.Midnight_Local_Time__c = midnightLocalTime;
If you use this, you'll want to keep in mind that there are limitations around time-based workflows and triggers though:
  • Time-based workflows aren't guaranteed to execute exactly on time, there could be a significant delay in their execution.

  • Unlimited Edition is limited to 20,000 time triggers per org

  • Enterprise Edition is limited to 10,000 time triggers per org
Linvio's CronKit appears to be a useful solution to this problem as well.

No comments: