I'm curious between Version 1 and Version 2, which of these trigger structures is more efficient? Or is there is another structure that is better?
When using a for loop I can check requirements of the record that is causing the trigger to fire. Such as making sure the picklist contains the right value or that a field is not empty. For this case is Version 1 or 2 preferred?
If there is a choice, is it better to work with a list(version 3) or an individual record(version 1 and 2)?
Version 1
trigger AcctTrigger on Account(before insert, before update)
{
for(Account a : trigger.new)
{
if(trigger.isBefore && trigger.isInsert)
{
//call handler(a)
}
if(trigger.isBefore && trigger.isUpdate)
{
//call handler(a)
}
}
}
Version 2
trigger AcctTrigger on Account(before insert, after insert, after update)
{
if(trigger.isBefore && trigger.isInsert)
{
for(Account a : trigger.new)
{
//call handler(a)
}
}
if(trigger.isBefore && trigger.isUpdate)
{
for(Account a : trigger.new)
{
//call handler(a)
}
}
}
Version 3
trigger AcctTrigger on Account(before insert, before update)
{
if(trigger.isBefore && trigger.isInsert)
{
//call handler(trigger.new)
}
if(trigger.isBefore && trigger.isInsert)
{
//call handler(trigger.new)
}
}