Friday, February 21, 2025

Master Interview questions on apex triggers in salesforce Part-1

1) What is trigger in Salesforce and what are trigger events?

A Salesforce trigger is a piece of Apex code executed before or after a specific action is performed on a Salesforce object, such as insert, update, or delete. Triggers are primarily used for automating tasks like validation, preventing incorrect data entry, or updating related records.

Trigger Events:

Before Insert: Fires before a record is inserted.

Before Update: Fires before an existing record is updated.

Before Delete: Fires before a record is deleted.

After Insert: Fires after a record is inserted.

After Update: Fires after a record is updated.

After Delete: Fires after a record is deleted.

After Undelete: Fires after a record is undeleted.

Each of these events corresponds to a specific stage in the lifecycle of a record, allowing you to implement logic tailored to that stage.

 2) What are trigger context variables in Salesforce? 

Following are the available Trigger Context Variables:

trigger.isbefore()

Checks if the trigger is in before mode. If the trigger is in before mode, it will return true.

trigger.isafter()

Checks if the trigger is in after mode. If the trigger is in after mode, it will return true.

trigger.isupdate()

Checks if the trigger is in update mode. If the trigger is in update mode, it will return true.

trigger.isdelete()

Checks if the trigger is in delete mode. If the trigger is in delete mode, it will return true.

trigger.isinsert()

Checks if the trigger is in insert mode. If the trigger is in insert mode, it will return true.

trigger.isundelete()

Checks if the trigger is in undelete mode. If the trigger is in undelete mode, it will return true.

trigger.isexecuting()

Checks if the Apex class method is being called from an Apex trigger. If it is being called, it will return true.

trigger.new()

Stores a new list of records.

trigger.newmap()

Stores new records with their IDs.

trigger.old()

Stores the old list of records.

trigger.oldmap()

Stores old records with their IDs.

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.

size

The total number of records in a trigger invocation, including both old and new records.

3) What is the difference between Trigger.new and Trigger.old and Trigger.newmap and 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.

4) Is the id of record changes if we undelete a deleted record?

No, It has the same id.

5) What is the use of trigger.isexecuting?

Suppose we have a method in apex class and we want this method to run only when the method is getting called from apex trigger than we can make use of trigger.isexecuting in apex class to check if the method is getting called from trigger .

Tags: #Interview questions on triggers in salesforce with answers, #Scenario based Interview Questions on triggers in Salesforce

6) How to restrict trigger to fire only once(Recursive trigger)? 

Refer article : How to avoid recursive trigger in Salesforce?

No comments:

Post a Comment