Search Here

Top Salesforce Triggers Interview Questions


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 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.

Trigger Events
Trigger Events

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.

  1. One Trigger Per Objec
  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 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

In Salesforce, when a merge event occurs (i.e., when duplicate records are merged), triggers can execute multiple times. The exact number of times a trigger executes during a merge event depends on the specifics of the trigger context and the records involved. Here's a breakdown of how triggers execute during a merge event:

  1. Before Delete Triggers:

    • If there are "before delete" triggers on the records that are being merged, those triggers will fire for each record involved in the merge. This means that "before delete" triggers execute twice: once for each of the two records before they are deleted.
  2. After Delete Triggers:

    • "After delete" triggers execute after the records are deleted. Similar to "before delete" triggers, "after delete" triggers fire twice: once for each of the two records after they are deleted.
  3. After Undelete Triggers:

    • If the records were undeleted as part of the merge process, "after undelete" triggers may execute. They execute once for each record that is undeleted.


How many times trigger will be executed?
Statement-level triggers execute once for each transaction. For example, if a single transaction inserted 500 rows into the Customer table, then a statement-level trigger on that table would only be executed once.

9. Order Of Execution For Trigger

In Salesforce, the order of execution for triggers is crucial to understand because it determines the sequence of events when records are inserted, updated, deleted, or undeleted. The following is the standard order of execution for trigger events in Salesforce:

  1. Before Triggers:

    • Before Insert: Trigger logic is executed before the records are inserted into the database.
    • Before Update: Trigger logic is executed before the records are updated in the database.
    • Before Delete: Trigger logic is executed before the records are deleted from the database.
    • Before Undelete: Trigger logic is executed before the records are undeleted.
  2. System Validation Rules:

    • All system validation rules, such as data type check, field length check, unique record check, custom unique field check, and required field check, are run to ensure data integrity.
  3. Custom Validation Rules:

    • Custom validation rules defined by the organization (Validation Rules) are executed.
  4. Before Triggers (again):

    • If the record was modified by the validation rules, the before triggers are re-executed.
  5. Duplicate Rules:

    • Duplicate rules and duplicate record items are checked. If duplicates are detected, the duplicate record items are created.
  6. After Triggers:

    • After Insert: Trigger logic is executed after the records are inserted into the database.
    • After Update: Trigger logic is executed after the records are updated in the database.
    • After Delete: Trigger logic is executed after the records are deleted from the database.
    • After Undelete: Trigger logic is executed after the records are undeleted.
  7. Assignment Rules:

    • If the record was modified in the after triggers, assignment rules are executed to assign records to specific users or queues.
  8. Auto-Response Rules:

    • Auto-response rules can be triggered for cases or leads to send automated responses to customers.
  9. Workflow Rules:

    • Workflow rules are executed, which can include field updates, email alerts, outbound messages, and tasks.
  10. Processes:

    • Processes, created using the Process Builder, are executed.
  11. Escalation Rules:

    • Escalation rules are evaluated, which can be used to automatically escalate cases based on defined criteria.
  12. Post-Commit Logic:

    • This includes any logic that occurs after the transaction is committed to the database. For example, sending outbound messages, making callouts to external services, and other post-commit actions are executed.

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

  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 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.

  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

The SFDC Trigger Framework has an abstract TriggerHandler base class that handles execution and prevents recursion. Child classes inherit from the TriggerHandler class, and override one or more of the trigger context methods (like beforeInsert, beforeUpdate, or afterUpdate).

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. 

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.