Time-based Workflow that’s Evaluated Every Time a Record is Saved
Imagine that you’re creating a workflow rule that has a time-based action. You want to set the rule’s Evaluation Criteria to “Evaluate the rule when a record is created, and every time it’s edited,” but Salesforce tells you, “You cannot add time-dependent workflow actions with this option.” Salesforce makes you pick “created, and any time it’s edited to subsequently meet criteria.” How can you get around this, to make sure the rule is evaluated every time the record is edited, even if you haven’t changed any of the record’s fields?
The answer comes when you realize how Salesforce determines whether the record has been “edited to subsequently meet the criteria.” When you save the record, Salesforce evaluates the criteria based on the record’s old values (before any changes you’ve just made), and then evaluates the criteria based on the record’s new values. You need to come up with a formula for the rule criteria that evaluates to FALSE for the old values and to TRUE for the new values. That way, Salesforce will think that the record has just been “edited to subsequently meet the criteria.”
What if you add to your rule criteria a condition that says, “and if the record’s LastModifiedDate is in the last few seconds?” When Salesforce evaluates the criteria based on the record’s old values, that condition will be FALSE, and when it evaluates the criteria based on the record’s new values, the condition will be TRUE. This is just what we want.
Here’s an example. A client had just created an Account workflow rule with criteria of “Expiration_Date__c >= TODAY(),” and a time-based action that fires “0 days after Expiration_Date__c” to change the Account’s Status to Inactive. They already had a bunch of Accounts with Expiration Dates in the future, but they needed to get these Accounts into the time-based workflow queue. If they created the new workflow rule and then did a data load to just update the Accounts without changing any of the values, the Accounts wouldn’t get added to the queue, because they weren’t changing to meet the criteria. However, if we change the workflow rule’s criteria to this:
Expiration_Date__c >= TODAY() && LastModifiedDate+0.0000578704 >= NOW()
(with a new clause that says, “if the LastModifiedDate is within the last 5 seconds”), then when we do a data load to tickle the Account records without changing any of the fields, Salesforce will think that the records have just been “edited to subsequently meet the criteria.” That’s because the old values for the Account record (before our update operation) have a LastModifiedDate that’s not within the last 5 seconds (so the record does not meet the criteria), and the new values for the Account record (after our update operation) have a LastModifiedDate that is within the last 5 seconds (so the record now does meet the criteria).
Effectively, with the addition of a clause that checks the LastModifiedDate, we’ve changed the evaluation criteria from “edited to subsequently meet the criteria” to “every time it’s edited!”
So why does LastModifiedDate+0.0000578704 >= NOW() translate into “modified in the last 5 seconds?” Provide your answer in the comments below.