+ Start a Discussion

Is it possible to perform dml operation in after triggers?

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?
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
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
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.
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


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

Trigger Best Practices | Sample Trigger Example | Implementing Trigger Framework

Trigger Context Variables

Please let us know if this will help you

Thanks ,
Amit Chaudhary