Wednesday, January 18, 2023

Triggers in Salesforce

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
}

No comments:

Post a Comment

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC)

Understanding Wire vs Imperative Apex Method Calls in Salesforce Lightning Web Components (LWC) Introduction: Salesforce Lightning Web ...