6

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

3 Answers 3

8

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

0
2

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.

1
  • First insert event will fire then again a update will follow the insert event. this is what I observe as well, but is that particular order guaranteed/documented anywhere?
    – LVS
    Commented Dec 30, 2020 at 17:21
0

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)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .