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
Megan Moody 10Megan Moody 10 

Workflow rules and triggers and CODE_UNIT_FINISHED

I have created three workflow rules - all doing field updates on the Opportunity Product - that execute against the Opportunity Product. There is also an Apex trigger coded for the Opportunity Product (that I did not code). Two of the workflow rules work just fine. However, when the third one is active, it causes some behavior that is different related to the Apex trigger. 

After combing through the debug logs, the most glaring difference is that when the WFRs are executed, the successful test (i.e. with only two active) has a CODE_UNIT_FINISHED entry at the end of the group. When the unsuccessful WFR is added, the CODE_UNIT_FINISHED entry does not appear until the end of the log file. 

Also, the unsuccessful WFR does work in terms of updating the field it should be updating. The problem is that it breaks when the user enters values that prompt the logic in the trigger. 

I'm hoping someone might be able to tell me why the CODE_UNIT_FINSHED for the workflow rules could change based on the activation/deactivation of one workflow rule. 

The pattern for a successful run is:
CODE_UNIT_STARTED - Triggers
CODE_UNIT_STARTED - OpportunityProductTrigger (before insert)
CODE_UNIT_FINISHED - OpportunityProductTrigger (before insert)
CODE_UNIT_STARTED - OpportunityProductTrigger (after insert)
CODE_UNIT_FINISHED - OpportunityProductTrigger (after insert)
CODE_UNIT_STARTED - Workflow:OpportunityLineItem
CODE_UNIT_FINISHED - Workflow:OpportunityLineItem
CODE_UNIT_STARTED - OpportunityTrigger (before update)
CODE_UNIT_FINISHED - OpportunityTrigger (before update)
CODE_UNIT_STARTED - Validation:Opportunity
CODE_UNIT_FINISHED - Validation:Opportunity
CODE_UNIT_STARTED - OpportunityTrigger (after update)
CODE_UNIT_FINISHED - OpportunityTrigger (after update)
CODE_UNIT_STARTED - OpportunityCompetitionTrigger (after update)
CODE_UNIT_FINISHED - OpportunityCompetitionTrigger (after update)
CODE_UNIT_STARTED - Workflow:Opportunity
CODE_UNIT_FINISHED - Workflow:Opportunity
CODE_UNIT_FINISHED - Triggers

The pattern for the unsuccessful run is this:
CODE_UNIT_STARTED - Triggers
CODE_UNIT_STARTED - OpportunityProductTrigger (before insert)
CODE_UNIT_FINISHED - OpportunityProductTrigger (before insert)
CODE_UNIT_STARTED - OpportunityProductTrigger (after insert)
CODE_UNIT_FINISHED - OpportunityProductTrigger (after insert)
CODE_UNIT_STARTED - Workflow:OpportunityLineItem
CODE_UNIT_FINISHED - Workflow:OpportunityLineItem
CODE_UNIT_STARTED - OpportunityTrigger (before update)
CODE_UNIT_FINISHED - OpportunityTrigger (before update)
CODE_UNIT_FINISHED - Workflow:OpportunityLineItem
CODE_UNIT_FINISHED - Triggers

I've attached screen shots. 

User-added imageUser-added image
Best Answer chosen by Megan Moody 10
surasura
it is very difficult to find and issue based on the exection order of triggers and workflows because there execution order is mainly adhoc. Your debug log changes based on the workflows becuase your workflow rules invoke your before update and after update tirggers for each field update. so amount of code  run depend on the number of workflows you have.

please refer exection order for information
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm