function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Kelly KKelly K 

Order of Execution and Transactions

Hi All,

I was wondering if I could get someone to verify or clarify that one transaction/execution context is the equivalent of the order of exeuction running once and only once?

A little context - 
Back in 2013, I set up a process where whenever a record is a created, a workflow rule will run to fill out a field (grabs the id of the record for tracking if it's cloned later). I have a validation rule that ensures that this field is filled in whenever an update occurs, new records tell the validation rule to evaluate to false.

For this particular object, I have one trigger that calls apex classes & methods as needed (we have several). 
No issues, everything works as expected, until this last January came around and my validation rule starts to evaluate to true on new records. So I log a case, I didn't make any changes to this process and things don't start failing unless something changed on the back end.

The response I'm getting back from t2 support is this:

"There are two operations on the same record in the same transaction. The record is inserted first. The after insert trigger is fired. It invokes an apex class. There is a DML statement ( update ) in the apex class which updates the same record.

When the record is inserted, the order of execution triggers for the first time. The validation rule is invoked for the first time. When we reach the update statement, the apex class updates the same record again.

This update forces the order of execution to trigger again. As a result, the validation rule fires again.

The events are shown in a single transaction in Debug logs, but the order of execution has been fired twice. "

Based on the documentation, it doesn't seem like you can have two order of executions and only a single transaction?


Workflows and processes have a checkbox rerun rules after update checkbox. check if thats causing any issue?
Kelly KKelly K

We don't use processes since we have an older org and all of our stuff is in workflow.

As far as that causing an issue, can that cause the behavior where in a singular transaction you have two orders of executions running asyncrhonously?

The problem with my scenario is that it seems like the validation rule is hitting a 2nd time but before the workflow rules have run, which doesn't align with the order of execution documentation.

Kelly KKelly K
Just verified in my sandbox that unchecking all 're-evals workflow rules after field change' were disabled on that object and the error still persists.
Check whether any of the Before triggers are undoing when they run second time (after field update) what work flow has done . ALso you can you post workflow and validation rule here.
Kelly KKelly K
I can verify that I have nothing in apex upating that same field - only the workflow rule.

My concern for this whole process is that the validation rules are firing twice in the context and the 2nd one is firing before workflow rules have run. According to the documentation, custom validation rules should only fire once.