Salesforce Apex Trigger interview questions with answers, including scenario-based questions. These questions are categorized into beginner, intermediate, and advanced levels.
Beginner-Level Questions
1. What is an Apex Trigger?
- Answer:
An Apex Trigger is a piece of code executed before or after certain database events likeINSERT
,UPDATE
,DELETE
, orUNDELETE
on Salesforce records. Triggers allow custom actions to occur when a record is manipulated.
2. What are the types of triggers in Salesforce?
- Answer:
- Before Triggers: Execute before a record is saved to the database. Used for validation or setting default values.
- After Triggers: Execute after a record is saved to the database. Used for actions like sending emails or calling external APIs.
3. What are Trigger Context Variables?
- Answer:
Trigger context variables provide runtime information about the trigger's execution. Common variables include:Trigger.new
: Contains new versions of the records being modified.Trigger.old
: Contains old versions of the records (forUPDATE
orDELETE
).Trigger.isInsert
,Trigger.isUpdate
,Trigger.isDelete
, etc.: Boolean flags for trigger operations.
4. What are Governor Limits in Salesforce?
- Answer:
Salesforce imposes limits to ensure efficient use of resources. Examples include:- SOQL Queries: 100 per transaction.
- DML Statements: 150 per transaction.
- Heap Size: 6 MB (synchronous) / 12 MB (asynchronous).
Intermediate-Level Questions
5. Can you invoke a trigger on a custom object?
- Answer:
Yes, triggers can be created on custom objects. The syntax is the same as for standard objects, e.g.,
6. How do you prevent recursive trigger execution?
- Answer:
Recursive triggers can be controlled using a static Boolean variable in an Apex class:
7. What is the difference between Trigger.new
and Trigger.old
?
- Answer:
Trigger.new
: Contains the new version of records being inserted/updated. It’s available inINSERT
andUPDATE
triggers.Trigger.old
: Contains the old version of records. It’s available inUPDATE
andDELETE
triggers.
8. Scenario: Write a trigger to prevent deletion of an Account if it has related Contacts.
- Answer:
9. What is the purpose of the addError
method in triggers?
- Answer:
TheaddError
method prevents DML operations on a record and displays a custom error message. It’s used for validations and ensuring data integrity.
Advanced-Level Questions
10. How do you bulkify a trigger?
- Answer:
Bulkification ensures triggers handle multiple records efficiently. For example:- Avoid SOQL or DML statements inside loops.
- Use collections like
List
orMap
for batch processing. Example:
11. Scenario: Create a trigger to automatically update an Opportunity's StageName
to "Closed Won" when the related Account's Type
is changed to "Customer".
- Answer:
12. What are the limitations of Apex Triggers?
- Answer:
- Cannot make direct callouts to external services.
- Subject to governor limits (SOQL, DML, CPU time, etc.).
- Cannot run asynchronously.
13. Scenario: Write a trigger to calculate and update a custom field Total_Contacts__c
on Account whenever a Contact is added, updated, or deleted.
- Answer:
14. Scenario: How would you design a trigger framework for large-scale Salesforce implementations?
Answer:
A trigger framework organizes triggers to follow best practices. Key elements include:- Handler Class: Use a separate class to handle logic.
- One Trigger per Object: Avoid multiple triggers on the same object.
- Trigger Context Handling: Manage
before
andafter
contexts separately.
Example:
No comments:
Post a Comment