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
dev perdev per 

After Update trigger execution order on DML actions

Hi ,

I have a question related to the "after update trigger" execution order when a DML update is performed on the same object. :
Lets says there are two contacts C1 and C2 and I need to update the field F2 on C2 based on some values in F1 on C1.

I check logic in first FOR loop and update F1 on C1 (upon a update trigger) then check few other logic and update F2 on C2 .

I know its not best practice to use after trigger for updating same object on which the trigger is invoked but in my case I dont have an option. Also, all i need to understand, how would after tirgger behave if there are 2 update statements for an object in the after update trigger of the same object.

So if update DML operation happens on C1(contact) first then will it  retrigger the "after update trigger" before continuing to the rest of the code  or will it continue on the second for loop and run the DML update operation on C2(contact) before recurrsively calling the same trigger again due to after update event? 

the pusedo code for the same is something like below:

trigger trg_contact_update on contact (before update) {

for  ( contact c : trigger.new ){
  doing some thing ...
    contactMap.add( c.id, c);
}

map< id, contact> c1Contact = new map<id,contact> ([select id , F1 where id in :contactMap.keys()]) 
for ( contact c1: c1Contact) {
  //do something
 String oldF1value = c1.F1;
c1.F1 = ' ';
c1List.add(c1);
}

if( c1List.size()> 0)
update c1List; 

for ( contact c2: c2Contact) {
  //do something
if(c1.F1 = ' ')
c2.F2 = oldF1value ;
c2List.add(c1);
}

if( c2List.size()> 0)
​update c2List; 
}

Please do not check for logic or issues in the above code, my intention is to understand how "after update trigger " behave in the case when there are 2 update DML operation. Please share your thoughts on the same.
 
Best Answer chosen by dev per
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Once you update record, it will fire trigger and triiger will have its own order of execution.
Please refer below link.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi dev per ,

I thin ,there is no need to call below code. As you trigger is on Contact object ,You manipulate field values in contact record ,rest trigger will take care automatically. i.e triger will update those contact records automatically.
​if( c2List.size()> 0)
​update c2List; 
}

Let us know if it helps.
dev perdev per
Hi Ashish,

Thanks for the prompt response. as mentioned earlier, I want to unerstand how the AFTER UPDATE TRIGGER control works in the case I presented.

I have also attached an image here pointing the possiblities of trigger control. Please let me know if the trigger follow blue or the green path and the reasons for the same to understand how the DML operations work in triggers.

Thanks,
User-added image
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Once you update record, it will fire trigger and triiger will have its own order of execution.
Please refer below link.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_order_of_execution.htm
This was selected as the best answer
dev perdev per
Sorry, I still didnt understand what is the order of execution in this case.
I checked the link which didnt talk much about recurssive trigger exection order.
So as per you comments, do you mean the order of excetion would follow blue highlighted lines in the image above?
Could you please elaborate a bit. 

Thanks in advance!