
Salesforce Interview Guide: Triggers, LWC, and Best Practices
Salesforce Development: Complete Interview and Implementation Guide
A comprehensive guide covering Apex triggers, Lightning Web Components, security models, and integration patterns for Salesforce developers and administrators.
Triggers and Apex Fundamentals
Order of Execution in a Trigger Context
When a record is inserted, Salesforce follows a specific sequence:
System Validation Rules (required fields, field formats)
Before Triggers (executes before record is saved)
Custom Validation Rules (user-defined rules)
After Triggers (executes after record is saved)
Assignment Rules (for Cases and Leads)
Auto-response Rules (email notifications)
Workflow Rules (field updates, email alerts)
Process Builder / Flows (automation)
Escalation Rules (for Cases)
Post-commit Logic (email sending, async sharing calculations)
When to Use Before vs. After Triggers
Before Triggers: Use when you need to update or validate the same record that initiates the trigger.
After Triggers: Use when you need to access the record's ID or perform DML operations on related records.
Troubleshooting a Bulk Trigger Failure
Common issues include:
SOQL or DML operations inside loops
Lack of bulkification
Missing error handling
Recursive trigger execution
Trigger to Create a Contact When an Account is Created
An after insert trigger on the Account object can create a new Contact record and associate it with the new Account:
trigger CreateContactOnAccount on Account (after insert) {
List<Contact> contactsToCreate = new List<Contact>();
for (Account acc : Trigger.new) {
contactsToCreate.add(new Contact(
LastName = acc.Name + ' Contact',
AccountId = acc.Id
));
}
if (!contactsToCreate.isEmpty()) {
insert contactsToCreate;
}
}
Comments (0)
Leave a Reply