How to resolve the issue related to the order of execution in Salesforce?
I need a small clarification on the order of execution in salesforce. Like i have trigger and Workflow field update both on Record insert operation/event, so in this case the final value will be from Workflow ,which is working fine.
But in case of Record update operation/event, it's taking the final value from trigger(and not from WF field update).
As per the documentation ,Workflow fires after the trigger, then why in the Update scenario above ,the final value is coming from Trigger.
To resolve the issue related to the order of execution in Salesforce, you must understand that - A Workflow Field Update triggers a second round of before and after triggers, so if a workflow and trigger both operate on the same field, the trigger will win. As explained in Triggers and Order of Execution:
Executes workflow rules. If there are workflow field updates:
a. Updates the record again.
b. Runs system validations again. Custom validation rules, flows, duplicate rules, processes, and escalation rules aren’t run again.
c. Executes before update triggers and after update triggers, regardless of the record operation (insert or update), one more time (and only one more time)
Note 11(c) is applicable here. Your update triggers will run if a Workflow Field Update results in a modification to a record. For this reason, it is important that you choose one or the other method of updating records. You should not mix declarative features (Workflow Rules, Flows) with code (triggers) on the same object.