0

I am trying to explore order of execution more. Hence I tried following things.

  1. Create a number field say - TestNumber
  2. Wrote a trigger. In before context, updated it +1.
  3. Have a workflow which will in turn update same field by +1.
  4. Have a process builder which is running one no criteria. It has 2 actions:

    i. Update same field +1
    ii. called an invocableMethod and print value of testNumber__c

Debug I received:

enter image description here

Question:

In invocable Apex, I believe it should be 6(since it is passing the value). Why it is 4 (where as before and after executions happened after it)?

Code invoking all process:

Account a = new Account();
a.name = 'test3';
a.testNumber__c = 1; 
insert a;
4

This seems to be the expected behavior considering you are updating the same field from a workflow update too. If you see the Triggers and Order of Execution documentation, and refer to the below excerpt from the documentation, this seems to be coming into picture here.

Trigger.old contains a version of the objects before the specific update that fired the trigger. However, there is an exception. When a record is updated and subsequently triggers a workflow rule field update, Trigger.old in the last update trigger doesn’t contain the version of the object immediately before the workflow update, but the object before the initial update was made. For example, suppose that an existing record has a number field with an initial value of 1. A user updates this field to 10, and a workflow rule field update fires and increments it to 11. In the update trigger that fires after the workflow field update, the field value of the object obtained from Trigger.old is the original value of 1, rather than 10, as would typically be the case.

Taking a stab here as what I think is the flow in your case:

  1. Create a number field say - TestNumber
  2. Wrote a trigger. In before context, updated it +1. (Value here is 2)
  3. Have a workflow which will in turn update same field by +1.

    (The workflow actually worked again on Value 1 here because of the reference above, so the Value here now is still 2 after incrementing by 1 on the Old value 1. Now because of the field update triggered from a workflow, the before and after triggers are executed again (refer the order of execution). So the final value out of this process would have been set again in the trigger which will be 2+1 = 3)

  4. Have a process builder which is running one no criteria. It has 2 actions:

    • i. Update same field +1 (Value here is now 3+1 = 4)
    • ii. called an invocableMethod and print value of testNumber__c
  • It is having 4 without process builder as well. And after that in process builder, it is again firing the events making it to 5 and in triggers before condition is getting 6. So even considering old value of trigger either it should be 1/3/5. – Ysr Shk Jul 18 '18 at 20:43
  • 1
    Based on your logs, it's 4 in the invocable method which was called from the process builder. Process builder will executed per the order of execution only after all after triggers (post workflow rule updates) have been executed. This is the best I think relates to with the information that you have provided. – Jayant Das Jul 18 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.