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
David Zhu 🔥David Zhu 🔥 

What is the disadvantage of using After trigger?

I was asked what the disadvantage of using AFTER trigger in an interview for the following scenarios.
1.Object A and B have lookup relationship. Object A is parent object.
2. When owner of A is changed, all B records' owner must be changed to the same one on parent record A.

Q: What trigger is to be used? I said after update trigger.
Q: Can you put the logic in before Trigger? I said, it is workable but not recommened. Before trigger updates the values of the record (A) iselft and After trigger updates the values of object records related to record A.
Q: What is the disadvantage of using After Trigger in this scenario?

As far as I know, SFDC does not recommend putting the logic in before trigger in this scenario. 

Can someone help me explain the disadvantage?
 
Best Answer chosen by David Zhu 🔥
Andrew GAndrew G
Hi David

I would see two possible issues with the After Update trigger,

1.  Record A is saved, then the updates to record B occur.  If record B throws an error due to some validation, any error results in the operation being rolled back, with the exception of any Apex Callouts.
reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_transactions.htm

2.  With Record A completing it's DML, if there is a trigger or other automation in record B that writes to record A, you run the risk of getting into a loop with the triggers. 

Having said that, I have always used the rule of thumb, that if i'm updating or validating the record that fires the trigger (in this case A), then it's a Before trigger and if i'm updating a related record, it's an After trigger.


Regards
Andrew

All Answers

Andrew GAndrew G
Hi David

I would see two possible issues with the After Update trigger,

1.  Record A is saved, then the updates to record B occur.  If record B throws an error due to some validation, any error results in the operation being rolled back, with the exception of any Apex Callouts.
reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_transactions.htm

2.  With Record A completing it's DML, if there is a trigger or other automation in record B that writes to record A, you run the risk of getting into a loop with the triggers. 

Having said that, I have always used the rule of thumb, that if i'm updating or validating the record that fires the trigger (in this case A), then it's a Before trigger and if i'm updating a related record, it's an After trigger.


Regards
Andrew
This was selected as the best answer
David Zhu 🔥David Zhu 🔥
Thanks Andrew for the reply.
You are right. These are two points to be considered at developing the triggers. But I don't think they are disadvantages