We use triggers to perform the operation before and after record changes.Before trigger are mainly used for validation purpose.After trigger are used for updating related records or for creating new records.
Following are the events on which trigger fires,
- Before insert
- Before update
- Before delete
- After insert
- After update
- After delete
- After undelete
trigger.isbefore()
=>Check if the trigger is in before mode, If the trigger is in before mode it will return true.
trigger.isafter()
=>Check if the trigger is in after mode, If the trigger is in after mode it will return true.
trigger.isupdate()
Check if the trigger is in update mode, If the trigger is in update mode it will return true.
trigger.isdelete()
Check if the trigger is in delete mode, If the trigger is in delete mode it will return true.
trigger.isinsert()
Check if the trigger is in insert mode, If the trigger is in insert mode it will return true.
trigger.isundelete()
Check if the trigger is in undelete mode, If the trigger is in undelete mode it will return true.
trigger.isexecuting()
Check if the apex class method is getting called from apex trigger, If getting called return true.
trigger.new()
Stores new records.
trigger.newmap()
Stores new records with id's
trigger.old()
Stores old records.
trigger.oldmap()
Stores old records with id's.
=>Check if the trigger is in before mode, If the trigger is in before mode it will return true.
=>Check if the trigger is in after mode, If the trigger is in after mode it will return true.
Check if the trigger is in update mode, If the trigger is in update mode it will return true.
Check if the trigger is in delete mode, If the trigger is in delete mode it will return true.
Check if the trigger is in insert mode, If the trigger is in insert mode it will return true.
Check if the trigger is in undelete mode, If the trigger is in undelete mode it will return true.
Check if the apex class method is getting called from apex trigger, If getting called return true.
Stores new records.
Stores new records with id's
Stores old records.
Stores old records with id's.
Now,what is the difference between trigger.new,trigger.newmap,trigger.old,trigger.oldmap.
Trigger.new returns new records and Trigger.old return data before updates were done.
Trigger.newmap returns new records with id's and Trigger.oldmap return data before updates were done with id's.
Trigger.new returns new records and Trigger.old return data before updates were done.
Trigger.newmap returns new records with id's and Trigger.oldmap return data before updates were done with id's.
Sample trigger for contact name validation before insert, If contact first name is 'test' update it with 'test1'.
trigger contactnamevalidation on Contact(before insert)
{
for(contact obj:trigger.new)
{
if(obj.FirstName=='test')
{
obj.FirstName='test1';
}
}
}
No comments:
Post a Comment