Triggers Interview Questions
Triggers Interview Questions
Exact Blueprint
I have worked on Trigger Handler Pattern. The basic idea behind the Trigger
Handler pattern is to separate the trigger logic into a separate class from the
trigger itself. The trigger class is responsible for registering the trigger with the
appropriate events and for delegating the actual logic to the handler class.
Trigger Events
Before Insert
After Insert
Before Update
After Update
Before Delete
After Delete
After Undelete
We'll follow the blueprint steps for inserting new child records based on
Parent Record.
// 5. DML Operation
if (!contactsToCreate.isEmpty()) {
insert contactsToCreate; // Insert the new Contacts into the database
}
}
}
12. Update Account Rating to ‘Hot ‘on account when opportunity stage
equals ‘closed one’
We need to write a trigger on the Opportunity object (the child) that updates
the Account (the parent) when the Opportunity's stage is set to 'Closed Won'.
The account's rating should be updated to 'Hot'.
13. Write a trigger that updates the "City" field in an Account when the
same field is updated in an Opportunity.
1. Store Child Record IDs: Identify and store the IDs of Opportunities where
the City field has changed.
2. SOQL on Parent Records: Query the parent Accounts related to these
Opportunities.
3. For Loop to Process Parent Records: Update the City field in the related
Accounts based on the Opportunity's City.
4. DML Operation: Perform an update operation on the modified Accounts.
Mind Map
Now that our blueprint is ready. Let us make a mind map of what we will do.
1. Store Child Record IDs and Account IDs to Update: We use a map
(accountCityMap) to store Account IDs as keys and the new City values as
values. This map helps us efficiently update the related Account's City field.
Note: We are using map as it says we need to update account city same as
opportunity city field so we need to save it apart from the id so that we can
utilize it to update account city in later steps.
3. For Loop to Process Parent Records: We iterate over the fetched Account
records and update their BillingCity with the new City value from the
Opportunities.
4. DML Operation: Finally, we update the modified Account records in the
database.