Salesforce Interview Questions on Triggers

In this post, we will talk about Salesforce Interview Questions on Triggers. Checkout our latest Salesforce Interview Questions on Triggers for 2024

Salesforce Interview Questions on Triggers

  1. What is Trigger in Salesforce?
  2. What are the two types of triggers in Salesforce?
  3. What is the use of trigger class in Salesforce?
  4. What are different events available in Triggers?
  5. When we should use trigger or automation?
  6. Best practice and consideration for Trigger?
  7. How many times trigger execute on an Upsert event
  8. How many times trigger execute on an Merge event
  9. Order of execution for Trigger
  10. When you will choose before event and when you will chose after event?
  11. What is the different between Trigger.New and Trigger.newMap?
  12. When we should use Trigger.Old
  13. How to void recursion in Trigger.
  14. How make callout from Trigger?
  15. Can we call a batch job from Trigger?
  16. What is Trigger Handler pattern?
  17. Have you used any trigger framework in Salesforce?
  18. Difference between Validation rules and Triggers?

If you are new check our session on Apex Trigger in Salesforce.

1. What is Trigger in Salesforce?

Trigger is a procedure in database which automatically invokes whenever a special events in Database occurs. Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions

Apex Triggers
Apex Triggers

2. What are the two types of triggers in Salesforce?

There are two types of triggers in Salesforce.

  • Before triggers are used to perform a task before a record is inserted or updated or deleted in Salesforce. These are used to update or validate record values before they are saved to the database.
  • After triggers are used, we can use the information set by the Salesforce system and make changes in the other records. The records that fire the after trigger are read-only.

3. What is the use of trigger class in Salesforce?

Use the Trigger class to access run-time context information in a trigger, such as the type of trigger or the list of sObject records that the trigger operates on.

4. What are different events available in Triggers?

An Apex trigger is a set of the statement which can be executed on the following events. We can add the below events with comma-separated. Here is a list of trigger events in Salesforce.

Trigger Events
Trigger Events

5. When should we use trigger or automation?

Use Salesforce flow for most of the automation in salesforce and use Apex Trigger for complex logic which can’t be done using Flow.

6. Best practice and consideration for Trigger?

Here is a list of all apex trigger best practices that we should while creating the trigger in Salesforce.

  1. One Trigger Per Object
  2. Logic-less Triggers
  3. Context-Specific Handler Method
  4. Bulkify your Code
  5. Avoid SOQL Queries or DML statements inside FOR Loops 
  6. Using Collections, Streamlining Queries, and Efficient For Loops 
  7. Querying Large Data Sets
  8. Use @future Appropriately
  9. Avoid Hardcoding IDs

7. How many times does the trigger execute on an Upsert event.

Upsert trigger fires on 4 different events :- before(insert, update), after (insert, update)

8. How many times does the trigger execute on a Merge event.

Merge triggers are fired on both events on the delete.

9. Order of execution for Trigger

Check out our Order of execution in the Salesforce post on the same.

10. When will you choose before the event, and when will you choose after the event?

Use it before the event to update the record that executes the Apex Trigger. Use After the event to use related or child records.

11. What is the difference between Trigger.New and Trigger.newMap?

Trigger.New return the list of SObject which invoke the trigger. Trigger.newMap returns the Map of Id and SObject.

12. When we should use Trigger.Old?

It is always good and recommended to check the old and new values before making any updates in Trigger. You can use Trigger.Old to check the old value of the record. It can help you to stop recursion in Trigger.

13. How to void recursion in Trigger?

There are different ways to stop the recursion in Trigger

  1. Use Static Boolean Variable: Create a class with a static Boolean variable with a default value of true.
    • This is good for less than 200 records.
    • If we update 200+ records, then it will only process the first set of records and skip the others.
  2. Use Static Set to Store Record Id: Use static set in class to store all processed record IDs.

14. How do you make a callout from Trigger?

No, we cannot. The callout is an Asynchronous process, whereas the Trigger is Dynamic / Synchronous. Callouts would hold up the database transaction until the callout is completed, which can be up to 120 seconds from a limited perspective.

But using @Future annotation we can convert the Trigger into an Asynchronous Class and we can use a Callout method. Learn more about the future method in Salesforce.

15. Can we call a batch job from Trigger?

Batch Apex can be invoked using an Apex trigger. But the trigger should not add more batch jobs than the limit

16. What is the Trigger Handler pattern?

Please check this post to learn about Handler pattern and code. Let us talk about what is the advantage of Trigger Handler Patter.

  1. Apex Class that handles trigger logic
  2. Allows code to be called from other code or tests
  3. Uses specific Trigger contexts and Trigger variables for routing
  4. Keep Triggers Simple
  5. Allow for greater flexibility
  6. Make code reusable
  7. Unit tests are much easier

17. Have you used any trigger framework in Salesforce

Check out the apex hours Trigger Framework in the Salesforce session for the same.

18. What is the Difference between Validation rules and Triggers?

Validation rules are used to confirm that the data entered into a record meets various data quality/business rules before letting the user save it.  Triggers can be used for various different things and can be executed at different times – e.g. when initiating a new record, before saving it, or when a record is deleted.  Validation rules are very easy to create and virtually anyone can learn how to create these.  Triggers are more complex and generally require some development skills.

Summary

I hope this Salesforce Interview Questions on Triggers will help you. Please check our developer interview questions here.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 474

4 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *