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
Raj R.Raj R. 

Does a workflow field update run before an apex trigger?

Hi,

I wanted to see if a workflow field update runs before an apex trigger (after update)?

Scenario:
I have a certain set of campaigns that when they are updated, I want to create events associated to those Campaigns. I am using an apex trigger to create the events, but I wanted only update those events when two Campaign fields are updated (StartDate and customField1). This means that if other campaign fields are updated then i do not want to update the events.

I was thinking of using a workflow field update using the following logic to determine whether the apex trigger should update the events. 
Workflow criteria:

ISCHANGED(StartDate) AND customField1=TRUE

Field update (set UpdateEvents__c field to true):

Campaign.UpdateEvents__c = TRUE

Then in my apex trigger, I would check if Campaign.UpdateEvents__c = TRUE, then only update the Events using the following logic below:
 
//assume cmps is all the campaigns being updated
List<Campaign> cmps = [Select Id, StartDate, UpdateEvents__c From Campaign];

for(Campaign cp : cmps) {
   if(cp.UpdateEvent__c = true) {

       //logic to update events associated to the campaign goes here

   }//end if
}//end for

Then once the events have been updated, I would like to set the Campaign.UpdateEvents__c to FALSE.

I was wondering if all this was possible and if the workflow field update would process before the apex trigger. 
 
Best Answer chosen by Raj R.
William TranWilliam Tran
Triggers process before Workflow.

The order of execution (with Apex) is as follows:

1. All before triggers execute
2. System validation occurs, such as verifying that all required fields have a non-null value, and running any user-defined
validation rules
3. All after triggers execute
4. Assignment rules execute
5. Auto-response rules execute
6. Workflow rules execute
7. Escalation rules execute
8. Post-commit logic executes, such as sending email

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks

All Answers

William TranWilliam Tran
Triggers process before Workflow.

The order of execution (with Apex) is as follows:

1. All before triggers execute
2. System validation occurs, such as verifying that all required fields have a non-null value, and running any user-defined
validation rules
3. All after triggers execute
4. Assignment rules execute
5. Auto-response rules execute
6. Workflow rules execute
7. Escalation rules execute
8. Post-commit logic executes, such as sending email

As a common practice, if your question is answered, please choose 1 best answer. 
But you can give every answer a thumb up if that answer is helpful to you. 

Thanks
This was selected as the best answer
Nisar799Nisar799

Hi rRup,

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm

Raj R.Raj R.
Hi William and Nisar,

Thank you for your responses. That clarifies a lot. 

it looks like i cannot do this the way i wanted to. What do you think would be the best way to check if the the start date value changed in a trigger.?

There is not really a "ISCHANGED(field)" in apex trigger, so I am wondering what would be the equivalent in apex trigger.
Nisar799Nisar799

Hi rRup

If you are in the Trigger then you can try the below code to trace if startDate is updated and customField1 is True.
 

// Assuming Trigger is firing for update case only and its on campaign object.
for(Campaign cp : Trigger.new) {
   boolean isStartDateChanged = cp.startDate != Trigger.oldMap.get(cp.id).startDate;
   if(isStartDateChanged && cp.customField1 == TRUE) {
       //logic to update events associated to the campaign goes here
   }//end if
}//end for

Feel Free to ask If you have any question regarding this.
Thanks,
799 Coder