1. What is a Salesforce trigger?
A Salesforce Trigger is a component of Apex code that runs before or after specified data manipulation language (DML) events, such as before object records are inserted into the database, even after records are deleted, and so on. Triggers are used to execute custom actions before or following changes to Salesforce records.
2. What is a Trigger Code?
A Trigger code in Salesforce is an element of Apex code that runs before or after particular data manipulation language (DML) events. These events include Salesforce records insert, update, deletes, and undelete. The trigger code is used to implement custom logic, such as data validation, automation, or modification, in response to various DML events.
3. What are the types of Salesforce Triggers?
There are two types of triggers in Salesforce:
Before Triggers: These are called before the DML process on the database is completed. They are commonly used to validate or change data before it is saved.
After Triggers: These triggers are executed after the DML operation and data temporarily saved into database. They can be used when accessing system-set field values (such as a recordID or LastModifiedDate field) or modifying other documents based on the initial record’s actions.
4. Can you explain the trigger execution order in Salesforce?
Salesforce executes the following in order:
- Executes before triggers.
- Executes validation rules.
- Executes after triggers.
- Assignment rules, auto-response rules, and workflow rules are processed.
- Escalation rules and entitlement rules are applied.
5. What is the Trigger.new and Trigger.old context variable?
Trigger.new: It holds the list of new records to be inserted or updated.
Trigger.old: It has the list of old records values before they were updated or deleted.
6. Can triggers be bulkified in Salesforce?
Yes, triggers should always be written with bulk processing in mind, meaning they should handle multiple records at once. Using loops or SOQL queries inside loops can cause issues with governor limit, so developers need to optimize triggers for bulk operations.
7. What are recursive triggers, and how can you avoid them?
A recursive trigger shows up when a trigger calls itself, that leads to an infinite loop. You can avoid recursion by using a static boolean variable to track whether the Trigger has already run.
8. What is the use of Trigger.isExecuting?
Trigger.isExecuting is a boolean that returns true if the current context is a trigger, that will eventually help to check whether your code is running in a trigger context.
9. State the difference between Trigger.new and Trigger.newMap.
Trigger.new is a list of records with new values. On the other hand, Trigger.newMap is a map of IDs to records. This is useful when accessing records using their IDs for processing.
10. Can you use DML operations in triggers?
Yes, you can use DML operations in triggers. However, the best practice is to limit the use of DML operations to avoid hitting Salesforce governor limits. Using collections to handle bulk records in DML is recommended.
Salesforce Trigger Interview Questions for Professionals
11. How would you stop a Trigger from executing multiple times?
You can utilize static variables to prevent a trigger from being executed more than once. A static variable serves as a flag. You can set this flag after the Trigger has been executed. You can skip the logic on subsequent recursive calls to the Trigger by checking the flag’s value.
This process ensures that the Trigger is not executed repeatedly within the same transaction, preventing undesirable behavior or failures.
12. Can you explain the concept of a Trigger framework? Why is it used?
Trigger framework is a Salesforce Apex design pattern allowing more modular and organized trigger logic management. It usually entails utilizing a handler class that includes the logic and the Trigger calling the relevant function from the handler class. The primary advantages of adopting a trigger framework are:
- Reduces code duplication.
- Improves code reuse.
- It simplifies the trigger logic by keeping the code clean and maintainable.
13. How do you ensure that your Triggers are bulk-safe?
Avoid performing DML actions (insert, update, and delete) or SOQL searches within loops to ensure your triggers are bulk-safe. This can soon exceed Salesforce governor restrictions, mainly when dealing with many records. Alternatively, you should:
- Collect records or data into collections (like lists or maps).
- Perform DML or query operations on the entire collection outside of the loop.
So, Trigger can handle mass activities efficiently without experiencing performance difficulties.
14. What are context variables in Salesforce Triggers?
Context variables in Salesforce triggers give critical information about the state of the records being processed and the runtime context in which the Trigger is executed. Some standard context variables are:
Trigger. New: Contains the most recent versions of the records inserted or changed.
Trigger. Old: Contains previous versions of the records being updated or destroyed.
Trigger.isInsert, Trigger.isUpdate, and Trigger.Delete: Indicate the type of DML activity that prompted the Trigger to run.
Trigger.isBefore, Trigger.isAfter: Determine whether the Trigger executes before or after the DML action.
These variables enable developers to handle multiple scenarios efficiently within a single Trigger.
15. Can you control multiple Triggers for the same object?
Salesforce platform permits you to have numerous Triggers on the same object. However, the hierarchy in which the various triggers are executed is not guaranteed. This can lead to unpredictability in how the triggers interact.
Salesforce recommends that each item have only one Trigger to avoid potential complications. A Trigger Handler class allows you to regulate the sequence and execution of your logic easily.
Scenario-Based Salesforce Trigger Interview Questions
16. Write a trigger that updates a field in a parent Account record when a Contact record is updated.
In this case, I will use the after-update Trigger on the Contact object to update the parent Account. Also, we can loop through the Trigger.new records, check the changes, and then update the associated Account record.
17. Write a trigger to stop the Account deletion record with “Active” status.
I would use a before-delete trigger and add logic to check if the status is “Active.” If it is, I’ll add an error to Trigger.old, preventing the deletion of those records.
18. Write a trigger that creates a follow-up task automatically when an Opportunity is marked as “Closed Won.”
In this scenario, firstly, I’d use an after-update trigger on Opportunity. I will check if the stage has been changed to “Closed Won,” if so, I will create a new task related to that Opportunity.
19. How would you prevent a trigger from running multiple times on the same record?
I would implement a static boolean variable to ensure the Trigger runs only once during a specific transaction. This helps prevent recursion and infinite loops.
20. Can you write a trigger that checks if a Contact has a valid email format during insert/update?
Use a before insert and before update trigger to check if the email follows a valid format using a regular expression. If not, add an error to the Contact record to prevent saving invalid data.