You are on page 1of 2

.

What is Trigger in Salesforce? In Salesforce, a trigger is a piece of Apex code that is executed before or after
specific data manipulation language (DML) events occur, such as insertions, updates, or deletions of Salesforce
records.
.
.
What are the two types of triggers in Salesforce? There are two types of triggers in Salesforce:
.
 Before Triggers: Executed before the record is saved to the database.
 After Triggers: Executed after the record is saved to the database.
.
What is the use of trigger class in Salesforce? A trigger class in Salesforce contains Apex code that is executed in
response to specific DML events on records in Salesforce.
.
.
What are different events available in Triggers? The different events available in triggers are:
.
 Insert
 Update
 Delete
 Undelete
.
When should we use trigger or automation? Triggers are used for complex business logic that cannot be handled
by standard Salesforce automation tools like workflow rules, process builder, or validation rules. Use triggers when
you need more flexibility and control over the logic.
.
.
Best practices and considerations for Triggers:
.
 Keep triggers focused and specific to one object.
 Follow the trigger handler pattern to maintain code readability and scalability.
 Bulkify your triggers to handle large data volumes efficiently.
 Test triggers thoroughly in a sandbox environment before deploying to production.
 Document your trigger logic and any dependencies clearly.
.
How many times does a trigger execute on an Upsert event? A trigger executes once per batch of records
affected by the upsert operation.
.
.
How many times does a trigger execute on a Merge event? A trigger executes once per master record in a merge
operation.
.
.
Order of execution for Trigger:
.
 Validation rules
 Before triggers
 System validation (such as required fields, uniqueness checks, etc.)
 After triggers
 Assignment rules
 Auto-response rules
 Workflow rules
 Escalation rules
 Processes
 Roll-up summary field calculations
 Post-commit logic (such as sending email)
.
When should you choose before event and when should you choose after event? Choose a before event when
you need to validate or modify data before it is saved to the database. Choose an after event when you need to
perform actions after the data has been saved to the database.
.
.
What is the difference between Trigger.New and Trigger.newMap?
.
 Trigger.New: Represents the new versions of the sObject records being inserted, updated, or
undeleted in a trigger context.
 Trigger.newMap: Represents a map of IDs to the new versions of the sObject records being
processed in a trigger context.
.
When should we use Trigger.Old? Use Trigger.Old when you need to access the old versions of records before
they are updated or deleted in a trigger context.
.
.
How to avoid recursion in Trigger? To avoid recursion in triggers, you can use static variables or custom settings to
control the trigger execution flow.
.
.
How to make a callout from a Trigger? To make a callout from a trigger, use the @future annotation or
queueable Apex to execute the callout asynchronously.
.
.
Can we call a batch job from a Trigger? Yes, you can call a batch job from a trigger by instantiating the batch class
and calling the Database.executeBatch method.

.
.
What is Trigger Handler pattern? The trigger handler pattern is a design pattern used to organize and manage
trigger logic in Salesforce. It involves separating trigger logic into separate classes to improve code readability,
maintainability, and scalability.
.
.
Have you used any trigger framework in Salesforce? Yes, there are several trigger frameworks available in
Salesforce, such as the Trigger Handler framework by Kevin O'Hara and the fflib Apex Common Library by
FinancialForce.
.
.
Difference between Validation rules and Triggers?
.
 Validation rules are declarative and are used to enforce data quality by specifying criteria that
records must meet.
 Triggers are written in Apex code and are used to perform complex business logic before or after
data manipulation events occur.
 Validation rules execute in the database layer, while triggers execute in the Apex runtime
environment.
 Triggers offer more flexibility and control over logic compared to validation rules.

You might also like