Master Interview questions on apex triggers in salesforce 2025 Updated

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

We basically use triggers to perform an operation such as validation(Preventing wrong data entry), update (updating related records).

Following are the events on which trigger fires,

 Before insert

Before update

Before delete

After insert

After update

After delete

After undelete

 2) What are trigger context variables in Salesforce? 

Following are the Trigger context variable available,

 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 getting called from apex trigger, If getting called return true.

trigger.new()

=> Stores new list of  records.

trigger.newmap()

=> Stores new records with id's

trigger.old()

=> Stores old list of records.

trigger.oldmap()

=> Stores old records with id's.

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, both old and new.

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?

7) How many trigger we should write on one object?

 We should always follow the best practice and consider writing one trigger per object. If we have more than one trigger per object on same event say before insert than we cannot guarantee the order of execution.  

8) What is the difference between database.insert and insert?

 Using insert if one record fails entire operation is stopped and none of the record is inserted into database, whereas with databse.insert partial insertion is supported.

9) Is it possible to call batch class from trigger.

Yes.

10) What are the the context variable available with before insert event?

Only Trigger.new is available.

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

11) What are the the context variable available with after insert event?

Trigger.new and Trigger.newMap.

12) What are the the context variable available with before update event?

Trigger.new, Trigger.old, Trigger.newmap and Trigger.oldmap

13) What are the the context variable available with after update event?

Trigger.new, Trigger.old, Trigger.newmap and Trigger.oldmap

14) What are the the context variable available with before delete event?

Trigger.old and Trigger.oldMap.

15) What are the the context variable available with after delete event?

Trigger.old and Trigger.oldMap.

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

16) What are the the context variable available with after undelete event?

Trigger.new and Trigger.newMap.

17) What is the condition to call a method from trigger which is making callout?

The callout should be asynchronous.

18) What will you do if a method inside apex class need to be executed only when it is getting called from trigger?

We will use trigger.isexecuting in apex class to check if the method inside apex class is getting called from trigger and will execute the method if getting called from trigger.

19) What is "Trigger Handler Framework Salesforce"?

Refer article: Trigger Handler Framework Salesforce

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


 

6 comments:

  1. Good explanation, Thank you!!

    ReplyDelete
  2. Good examples. Can you write more. Thanks

    ReplyDelete
  3. Thank you for making it clear and would be much more helpful if you could do some more on asynchronous and lds , lms concepts please..

    ReplyDelete
  4. nicely explained concepts .. very Helpful .. Thank you .

    ReplyDelete