Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.