In this post, we will talk about Salesforce Interview Questions on Triggers.
Salesforce Interview Questions on Triggers
- What is Trigger in Salesforce?
- What are the two types of triggers in Salesforce?
- What is the use of trigger class in Salesforce?
- What are different events available in Triggers?
- When we should use trigger or automation?
- Best practice and consideration for Trigger?
- How many times trigger execute on an Upsert event
- How many times trigger execute on an Merge event
- Order of execution for Trigger
- When you will choose before event and when you will chose after event?
- What is the different between Trigger.New and Trigger.newMap?
- When we should use Trigger.Old
- How to void recursion in Trigger.
- How make callout from Trigger?
- Can we call a batch job from Trigger?
- What is Trigger Handler pattern?
- Have you used any trigger framework in Salesforce?
- 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
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 if we want to use the information set by Salesforce system and to 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.
5. When we should 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.
- One Trigger Per Objec
- Logic-less Triggers
- Context-Specific Handler Method
- Bulkify your Code
- Avoid SOQL Queries or DML statements inside FOR Loops
- Using Collections, Streamlining Queries, and Efficient For Loops
- Querying Large Data Sets
- Use @future Appropriately
- Avoid Hardcoding IDs
7. How many times trigger execute on an Upsert event
Upsert trigger fires on 4 different events :- before(insert, update), after (insert, update)
8. How many times trigger execute on an 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 you will choose before the event and when you will choose after the event?
Use before the event to update the record which 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 return 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 way to stop the recursion in Trigger
- 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.
- Use Static Set to Store Record Id: Use static set in class to store all processed record IDs.
14. How make 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.
- Apex Class that handles trigger logic
- Allows code to be called from other code or tests
- Uses specific Trigger contexts and Trigger variables for routing
- Keep Triggers Simple
- Allow for greater flexibility
- Make code reusable
- 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. Difference between Validation rules and Triggers?
Validation rules are used to confirm that the data entered into a record meet 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.
I hope this Salesforce Interview Questions on Triggers will help you. Please check our developer interview questions here.