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.”

timebase workflow

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!”

Trivia Question

So why does LastModifiedDate+0.0000578704 >= NOW() translate into “modified in the last 5 seconds?” Provide your answer in the comments below.

MJ Kahn, SVP of Technology at OpFocus

about the author

MJ Kahn

At OpFocus, MJ architects and constructs solutions that would impress the builders of the pyramids. She solves technical puzzles that would frustrate daVinci. She leaps tall buildings (like the new Salesforce tower) in a single bound.

Well ok, maybe she doesn’t. But she does help lead an amazing team of smart, talented, and dedicated consultants who do. MJ’s job at OpFocus is provide technical leadership and guidance to OpFocus clients and team members so that, working together, we can create innovative yet practical solutions to real-world business problems within the Salesforce ecosystem.

Prior to OpFocus, MJ built an extensive background in technology and has held a variety of development, consulting, and management positions at companies like Sybase, Vignette, and Honeywell, among others. In these roles, MJ’s focus was helping companies effectively and intelligently use technology to solve business problems. An Apex and Visualforce consultant since mid-2008, MJ has worked with scores of companies to help them understand and utilize platforms like Force.com to solve business issues and also guide them in developing their own AppExchange product offerings.