Triggers in Salesforce

By | December 16, 2019

Triggers are the automated actions which are performed when ever DML operations are performed on the object.

Trigger are classified in to two types:

  1. Before triggers : These triggers are fired before the new data is saved to Sobjects (or) database.
  2. After triggers : These triggers are fired after the records are saved to Sobjects (or) database.

Trigger Events:

These are the events that will fire the trigger when DML is performed.

  1. Before insert
  2. Before update
  3. Before delete
  4. After insert
  5. After update
  6. After delete
  7. After undelete

Trigger context variables:

  • These are variables/properties which are defined in trigger class.
  • These variables will hold data required during the run-time of trigger.

Variables:

  1. Trigger.isinsert: (a)It is a Boolean value (b)It is set as true if trigger fired due to the DML operation of insert
  2. Trigger.isupdate: (a)It is a Boolean value ( b) It is set as true if the trigger fired due to the DML operation of update
  3. Trigger.isdelete: (a) It is a Boolean value ( b) It is set as true if the trigger fired due to the DML operation of delete
  4. Trigger.isundelete: (a) It is a Boolean value ( b) It is set as true if the trigger fired due to the DML operation of delete
  5. Trigger.isBefore: (a) It is a Boolean value (b) It is set as true if the trigger fired before saving the data to Sobject.
  6. Trigger.isAfter: (a) It is a Boolean value ( b) It is set as true if the trigger fired After saving the data to Sobject.
  7. Trigger.new: (a) It is list of Sobjects ( b) This will contain new version of records involved in DML statements.
  8. Trigger.old: (a) It is list of Sobjects ( b) This will contain old version of records involved in DML statements.
  9. Trigger.oldmap: (a) It is a map of records where id of the records store as key & records as value ( b) This will contains old version of records involved in DML operation
  10. Trigger.newmap: (a) It is a map of records where id of the records store as key & records as value (b) This will contains new version of records involved in DML operation

SYNTAX:

trigger TriggerName ON Sobject (event){
//implement logic here
}

Share this...
Share on Facebook
Facebook
Email this to someone
email

Leave a Reply

Your email address will not be published. Required fields are marked *