+ Start a Discussion
ministe_2003ministe_2003 

insert and update triggers - together or seperate

Hi,

wondering if anyone can clarify something.  Triggers obviously run on batches of records. If a trigger fires for "before insert, before update", and I perform an upsert on several records which includes some updates and inserts, are two instances of that trigger created - one containing the updates and another containing the inserts, or does one trigger fire which contains both the inserts and updates?

 

The reason I'm asking is because I want to perform some conditional actions based on .isInsert and .isUpdate and depending on which of these two ways a trigger really works, I'm going to have to iterate differently.

 

I'm assuming that two triggers are created.  I want to collate a list of IDs, but only if a record is being inserted.  I assume that trigger.isInsert says that every single record in this trigger instance is being inserted; but I just want to make sure that it's not record dependent, and that trigger.isInsert can not be different, per record, in a single trigger fire.

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

You are correct - there will be two triggers invoked.  

 

For example, in a before insert trigger, the trigger.old isn't available, which guarantees that only records being inserted have been supplied to the trigger, thus it wouldn't be possible to mixin updates.

 

[editied as I got trigger.new and old the wrong way around :)]

All Answers

bob_buzzardbob_buzzard

You are correct - there will be two triggers invoked.  

 

For example, in a before insert trigger, the trigger.old isn't available, which guarantees that only records being inserted have been supplied to the trigger, thus it wouldn't be possible to mixin updates.

 

[editied as I got trigger.new and old the wrong way around :)]

This was selected as the best answer
ministe_2003ministe_2003

thanks for the confirmation, just wanted to be sure!