Apex Triggers
What is an Apex Trigger?
An Apex Trigger is a block of code that automatically runs before or after a record is created, updated, deleted, or restored in Salesforce.
|Think of a Trigger as a rule that says: “When this happens, do that.”
For example:
If a new Contact is created, a trigger can automatically send a welcome message or assign the contact to a default sales rep.
When Do We Use Triggers?
Triggers are helpful when:
• You want to automate business processes.
• You need to validate or manipulate data when users interact with records.
• You want to enforce custom rules beyond what standard Salesforce automation can handle (like Validation Rules o r Workflow Rules).
Basic Trigger Syntax
trigger TriggerName on ObjectName (events) {
// trigger logic here
}
Example:
trigger AccountTrigger on Account (before insert) {
for (Account acc : Trigger.new) {
acc.Name = acc.Name + ‘ – Verified’;
}
}
Explanation:
Every time a new Account is created, it adds “- Verified” at the end of the account name.
Example 1: Set Default Value on Insert
Use Case: When a new Contact is created, set the LeadSource field to “Web” if it’s empty.
trigger SetDefaultLeadSource on Contact (before insert) {
for (Contact c : Trigger.new) {
if (String.isBlank(c.LeadSource)) {
c.LeadSource = ‘Web’;
}
}
}
Explanation:
• This trigger runs before a new Contact is saved.
• It checks if the LeadSource field is empty (null or blank).
• If it’s blank, it sets the LeadSource to “Web”.
• Use Case: Helps assign a default value to keep data consistent when users forget to fill in a field.
Example 2: Prevent Deletion of Important Records
Use Case: Don’t allow deletion of Account records with the type “Customer”.
trigger PreventCustomerAccountDelete on Account (before delete) {
for (Account acc : Trigger.old) {
if (acc.Type == ‘Customer’) {
acc.addError(‘You cannot delete Customer accounts.’);
}
}
Explanation:
• This trigger runs before an Account is deleted.
• It checks if the Type of the Account is “Customer”.
• If yes, it blocks the deletion and shows an error message to the user.
• Use Case: Protects important customer records from being accidentally deleted.
Example 3: Auto-Update a Field on Update
Use Case: When a Case is updated, automatically change the Status to “In Progress” if it’s currently “New”.
trigger AutoUpdateCaseStatus on Case (before update) {
for (Case c : Trigger.new) {
Case oldCase = Trigger.oldMap.get(c.Id);
if (oldCase.Status == ‘New’) {
c.Status = ‘In Progress’;
}
}
}
Explanation:
• This trigger runs before a Case is updated.
• It checks if the previous status was “New”.
• If yes, it automatically changes the status to “In Progress”.
• Use Case: Automatically move cases forward in the workflow when any change occurs.
Example 4: Add Suffix to Name
Use Case: Add “- Verified” to every new Account name on creation.
trigger AddSuffixToAccount on Account (before insert) {
for (Account acc : Trigger.new) {
acc.Name = acc.Name + ‘ – Verified’;
}
Explanation:
• This trigger runs before a new Account is saved.
• It adds ” – Verified” at the end of the Account’s name.
• Use Case: Helps identify new accounts with a standard label for verification or processing.
Example 4: Add Suffix to Name
Use Case: Add “- Verified” to every new Account name on creation.
trigger AddSuffixToAccount on Account (before insert) { for (Account acc : Trigger.new) { acc.Name = acc.Name + ‘ – Verified’; } }
Explanation:
• This trigger runs before a new Account is saved.
• It adds ” – Verified” at the end of the Account’s name.
• Use Case: Helps identify new accounts with a standard label for verification or processing.
Use Case | Trigger Type |
---|---|
Auto-assign leads to a user | after insert |
Prevent deletion of VIP records before delete | |
Add default value if empty | before insert/update |
Log record changes | after update |
Conclusion:
Apex Triggers are a powerful feature that lets developers build automated and intelligent systems within Salesforce. They’re essential for any backend automation or complex validations.