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
ryanhcaryanhca 

Trigger execution - order of operations and variable life span

Are triggers synchronous or asynchronous?

 

If I make an "insert" call in an Apex method, and that insert triggers a "before" and/or an "after" trigger for that record, should I expect that those triggers complete before the next line of Apex code?

 

I think the answer is "no" - meaning that triggers are asynchronous, but I just wanted to check...

snugglessnuggles
i've always thought of them as synchronous just based on behavior i've observed.  lets say, for example, you have a trigger that does some custom validation and adds errors to records to prevent them from being saved if they meet some criteria.  Then lets say you do an insert of an array into that object from apex.  Your database.SaveResult[] that gets returned by your insert operation will contain those errors for the records that set off your custom validation trigger.  To do this that trigger must have finished running?  I think the only way to go asynch is through the @future annotation, even in trigger context.
Anup JadhavAnup Jadhav

Hi ryancha,

 

All triggers are by default synchronous. If you update a record, the corresponding after/before trigger will fire and the control won't be relinquished till the trigger completes. 

If the trigger fails for any reason, the error is returned the calling method where the record is being updated.

 

- AJ