Salesforce Developer Interview Questions – Apex Triggers
APEX – TRIGGERS
Table of Contents
64. What is a trigger?
Answer:
Apex triggers allows us to perform custom actions before or after changes to salesforce records, such as insertion, updates or deletions.
Apex trigger is an apex code that executes before or after changes to the salesforce records , such as insertion, updates or deletions.
65. Explain the different types of triggers ?
Answer:
There are two types of triggers:
Before Triggers : It is used to update or validate record values before they’re saved to the database.
After triggers : It is used to access field values that are set by the system (such as a record’s id or lastModifedDate field) , and to affect changes in other records, like updating the records of the same object or other objects. The records that fire the after trigger are read-only
66. What are the different types of trigger events in salesforce?
Answer:
Before insert
Before update
Before delete
Before undelete
After insert
After update
After delete
After undelete
Note : Before undelete event does not exist.
67. Can you write trigger syntax and an example?
Answer:
Syntax
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/67%20Can%20you%20write%20trigger%20syntax%20and%20an%20example%201636a6aac3e613_lg.png)
where trigger_events can be a comma-separated list of one or more events
Example
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/67%20Can%20you%20write%20trigger%20syntax%20and%20an%20example%202636a6b2344db9_lg.png)
68. What are Trigger context variables?
Answer:
All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the system.Trigger Class.
Variable
Usage | |
isExecuting | Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call. |
isInsert | Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API. |
isUpdate | Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API. |
isDelete | Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API. |
isBefore | Returns true if this trigger was fired before any record was saved. |
isAfter | Returns true if this trigger was fired after all records were saved. |
isUndelete | Returns true if this trigger was fired after a record is recovered from the Recycle Bin. This recovery can occur after an undelete operation from the Salesforce user interface, Apex, or the API. |
new | Returns a list of the new versions of the sObject records.This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers. |
newMap | A map of IDs to the new versions of the sObject records.This map is only available in before update, after insert, after update, and after undelete triggers. |
old | Returns a list of the old versions of the sObject records.This sObject list is only available in update and delete triggers. |
oldMap | A map of IDs to the old versions of the sObject records.This map is only available in update and delete triggers. |
operationType | Returns an enum of type System.TriggerOperation corresponding to the current operation.Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. If you vary your programming logic based on different trigger types, consider using the switch statement with different permutations of unique trigger execution enum states. |
size | The total number of records in a trigger invocation, both old and new. |
Sample Trigger for trigger context variables
![](https://s3.us-east-1.amazonaws.com/contents.newzenler.com/20039/library/68%20What%20are%20Trigger%20context%20variables636a6c066073d_lg.png)
69. What is the difference between Trigger.New and Trigger.Old?
Answer:
Trigger.new returns a list of the new versions of the sObject records whereas Trigger.Old returns a list of the old versions of the sObject records.
Trigger.new is available in insert, update, and undelete triggers whereas Trigger.Old is only available in update and delete triggers.
70. What is the difference between Trigger.New and Trigger.NewMap?
Answer:
Trigger.new returns a list of the new versions of the sObject records whereas Trigger the newMap returns a map of IDs to the new versions of the sObject records.
71. Availability matrix for Trigger.New, Trigger.NewMap, Trigger.Old and Trigger.Old Map?
Answer:
Trigger Event | Trigger.New | Trigger.Old | Trigger.NewMap | Trigger.OldMap |
Before Insert | Yes | No | No | No |
Before Update | Yes | Yes | Yes | Yes |
Before Delete | No | Yes | No | Yes |
After Insert | Yes | No | Yes | No |
After Update | Yes | Yes | Yes | Yes |
After Delete | No | Yes | No | Yes |
After Undelete | Yes | No | Yes | No |