Find your content:

Search form

You are here

Trigger behaviour with insert and update in same execution context

 
Share

I am writing an after insert, after update on Task object trigger. Both the insert code and the update code will make changes to records for a custom object which get updated.

I would like to know what happens if a task insert and a task update happen in the same execution context - does the update customObjList get called twice?

trigger TaskUpdateCustomObject on Task(after insert, after update) {

    List<CustomObject__c> customObjList = new List<CustomObject__c>();

    if(Trigger.IsInsert) {
        // possibly add custom objects to update
    }

    if(Trigger.IsUpdate) {
        // possibly add custom objects to update
    }

    update customObjList;
}

This might seem like a stupid question, but I just want to get clear in my head that the trigger code will run on 2 separate occasions. Once on insert and once on update.

Thanks


Attribution to: Joe

Possible Suggestion/Solution #1

The Update customObjList statement will only execute once if the trigger runs once, this is true, but I think there's some misunderstandings here about what an execution context is - isn't it just the entire run of Apex code?

No, there will never be a case where trigger.isUpdate and trigger.isInsert are both true, but you can have cascading trigger effects that cause separate instances of the trigger to execute twice, once as Insert and once as an Update. In that case the update customObjList statement -would- execute twice but - as someone else pointed out - the list would be initialized two separate times as well.

If you -want- a list that will survive amongst various trigger executions within the same execution context, you can put it in a Global class as a global variable, giving all classes everywhere access; This will allow you to aggregate information across many triggers and classes (just don't make them sloppily dependent on one another)


Attribution to: William Saunders

Possible Suggestion/Solution #2

Your trigger.isInsert and trigger.isUpdate blocks will be mutually exclusive as I understand it. (i.e. the insert and update operations will have independent execution contexts)

isInsert Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.

It doesn't seem plausible for the value of Trigger.isInsert and Trigger.isUpdate to be true at the same time. Based on this, my understanding has been that a Trigger, even though both Update and Insert, will only get called for either of insert or update in a single execution context.

If you wanted to be transparent to the behaviour, you could use an upsert, rather than an update, which will cater for both Inserts and Updates. (Also as long as your insert / update behaviour is completely within the respective if blocks, it shouldn't really matter.)


Attribution to: techtrekker

Possible Suggestion/Solution #3

its not documented anywhere but yes the list will initialized twice but they will still remain in same transaction and to add the governor limit will apply on the whole transaction not on each trigger events.

To try this out you can call insert a record from class and in the very second line try to update the record. Both insert and update event will fire in that transaction. First insert event will fire then again a update will follow the insert event.


Attribution to: Avidev9
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4813

My Block Status

My Block Content