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
CvrKCvrK 

Is it possible to perform dml operation in after triggers?

Hi 
1) Is it possible to update a child record when its associated master record is updated with a particular value on after update trigger?if possible which trigger context variable best suits the requirement?
For Example: when ever a field FA1 is updated with a X value in the master object then a field FB1 should be updated with X value on after update event.
2) similarly is it possible to delete child record when a master record is updated on after update event?
pconpcon
Yes, you can preform DML in after triggers as well as before triggers.  Most of the time if you are creating child records you'll have to do it in an after because you will need the Id set.  Both of your scenarios are very common
JeffreyStevensJeffreyStevens
Also - if all you need is FB1 to have the value of X - you could do a formula field in the child, and set it's value to MasterObject.FA1.
srlawr uksrlawr uk
Patrick is quite right, and in after update/insert triggers, you will want to do your processing on the
 
trigger.new
context variable, as this one will hold the up-to-date version of your parent object (including - as pcon said again - the Id field if it is an insert.

You shouldn't typically iterate over the contents of the trigger.old map values - you should mostly use this for referencing old values using the map.get() method to compare to the new values. trigger.new is your power-list.
 
CvrKCvrK
Actually the following link made me post this question which says "it is not possible to perform DML operation in after triggers"so i was little confused

https://developer.salesforce.com/forums/?id=906F0000000Az72IAC

 
pconpcon
This is and example of why you should be careful with it.  And keep in in mind.  If have a trigger on Object A and you insert Object B and that updates Object A which then could cause a new Object B to be created...
srlawr uksrlawr uk
I believe triggers will recurse 8 times before they will chuck you out? Not sure if that's a transaction rolling-back level error or just a "we will stop here" event though.
pconpcon
It's an uncatchable exception that rolls the whole transaction back (unless of course it occurs in an @future call then it only rolls that transaction back)
Amit Chaudhary 8Amit Chaudhary 8
Yes, you can preform DML in after triggers as well as before triggers. In this case your trigger can  become the recusive.
Please check below post how to stop recursive Trigger

how to stop recursive trigger in salesforce
http://amitsalesforce.blogspot.in/2015/03/how-to-stop-recursive-trigger-in.html

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework
http://amitsalesforce.blogspot.in/2015/06/trigger-best-practices-sample-trigger.html

Trigger Context Variables
http://amitsalesforce.blogspot.in/2015/10/trigger-context-variables.html

Please let us know if this will help you

Thanks ,
Amit Chaudhary