You are currently viewing Salesforce Developer Interview Questions Part6

Salesforce Developer Interview Questions Part6

Salesforce Developer Interview Questions – Apex Triggers

APEX – TRIGGERS

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

where trigger_events can be a comma-separated list of one or more events

Example


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
isExecutingReturns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.
isInsertReturns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdateReturns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDeleteReturns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBeforeReturns true if this trigger was fired before any record was saved.
isAfterReturns true if this trigger was fired after all records were saved.
isUndeleteReturns 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.
newReturns 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.
newMapA 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.
oldReturns a list of the old versions of the sObject records.This sObject list is only available in update and delete triggers.
oldMapA map of IDs to the old versions of the sObject records.This map is only available in update and delete triggers.
operationTypeReturns 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.
sizeThe total number of records in a trigger invocation, both old and new.

Sample Trigger for trigger context variables


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 EventTrigger.NewTrigger.OldTrigger.NewMapTrigger.OldMap
Before InsertYesNoNoNo
Before UpdateYesYesYesYes
Before DeleteNoYesNoYes
After InsertYesNoYesNo
After UpdateYesYesYesYes
After DeleteNoYesNoYes
After UndeleteYesNoYesNo

Leave a Reply