ad2

Top 5 Trigger Scenarios in Salesforce: Best Practices with Trigger, Handler, and Helper Code

 Introduction:

Triggers play a crucial role in Salesforce for automating business logic and ensuring data integrity. By leveraging trigger frameworks and following best practices, you can build efficient and maintainable triggers. In this blog post, we will explore the top five trigger scenarios and provide code examples with a trigger, handler, and helper classes to demonstrate best practices.



1. Scenario: Validation before Inserting or Updating Records

Description: Often, you need to enforce certain validation rules before allowing records to be inserted or updated. Here's an example of a trigger scenario that validates the uniqueness of a custom field on the Contact object before inserting or updating a record.


Trigger Code:

trigger ContactValidationTrigger on Contact (before insert, before update) {

    ContactTriggerHandler.handleBeforeInsertUpdate(Trigger.new);

}


Trigger Handler Code:


public class ContactTriggerHandler {

    public static void handleBeforeInsertUpdate(List<Contact> contacts) {

        ContactHelper.validateUniqueEmail(contacts);

        // Other business logic or data manipulation

    }

}

Helper Code:

public class ContactHelper {

    public static void validateUniqueEmail(List<Contact> contacts) {

        Set<String> emailSet = new Set<String>();

        for (Contact contact : contacts) {

            if (contact.Email != null && emailSet.contains(contact.Email)) {

                contact.addError('Email must be unique.');

            } else {

                emailSet.add(contact.Email);

            }

        }

    }

}


2. Scenario: Cross-Object Field Update

Description: Sometimes, you need to update related records when a specific field is modified on a particular object. Here's an example of a trigger scenario that updates the "Last Activity Date" field on the Account object when a related Task is inserted or updated.


Trigger Code:

trigger TaskTrigger on Task (after insert, after update) {

    TaskTriggerHandler.handleAfterInsertUpdate(Trigger.new);

}


Trigger Handler Code:


public class TaskTriggerHandler {

    public static void handleAfterInsertUpdate(List<Task> tasks) {

        TaskHelper.updateLastActivityDate(tasks);

        // Other business logic or data manipulation

    }

}


Helper Code:


public class TaskHelper {

    public static void updateLastActivityDate(List<Task> tasks) {

        Set<Id> accountIds = new Set<Id>();

        for (Task task : tasks) {

            accountIds.add(task.WhatId);

        }

        List<Account> accountsToUpdate = [SELECT Id, Last_Activity_Date__c FROM Account WHERE Id IN :accountIds];

        for (Account account : accountsToUpdate) {

            account.Last_Activity_Date__c = Date.today();

        }

        update accountsToUpdate;

    }

}



3. Scenario: Roll-up Summary Calculation

Description: Roll-up summary calculations help summarize data from child records onto parent records. Here's an example of a trigger scenario that updates the "Total Opportunities" field on the Account object when a related Opportunity is inserted, updated, or deleted.


Trigger Code:


trigger OpportunityTrigger on Opportunity (after insert, after update, after delete) {

    OpportunityTriggerHandler.handleAfterInsertUpdateDelete(Trigger.new, Trigger.old, Trigger.operationType);

}


Trigger Handler Code:


public class OpportunityTriggerHandler {

    public static void handleAfterInsertUpdateDelete(List<Opportunity> newOpportunities, List<Opportunity> oldOpportunities, System.TriggerOperation triggerOperation) {

        OpportunityHelper.updateTotalOpportunities(newOpportunities, oldOpportunities, triggerOperation);

        // Other business logic or data manipulation



    }

}


Helper Code:

public class OpportunityHelper {

    public static void updateTotalOpportunities(List<Opportunity> newOpportunities, List<Opportunity> oldOpportunities, System.TriggerOperation triggerOperation) {

        Set<Id> accountIds = new Set<Id>();

        if (triggerOperation != System.TriggerOperation.DELETE) {

            for (Opportunity opportunity : newOpportunities) {

                accountIds.add(opportunity.AccountId);

            }

        } else {

            for (Opportunity opportunity : oldOpportunities) {

                accountIds.add(opportunity.AccountId);

            }

        }

        List<Account> accountsToUpdate = [SELECT Id, Total_Opportunities__c, (SELECT Id FROM Opportunities) FROM Account WHERE Id IN :accountIds];

        for (Account account : accountsToUpdate) {

            account.Total_Opportunities__c = account.Opportunities.size();

        }

        update accountsToUpdate;

    }

}



4. Scenario: Field Value Update Based on Related Record Changes

Description: When a related record is modified, you might need to update specific fields on other records. Here's an example of a trigger scenario that updates the "Last Contacted Date" field on the Contact object when a related Opportunity is updated.


Trigger Code:

trigger OpportunityTrigger on Opportunity (after update) {

    OpportunityTriggerHandler.handleAfterUpdate(Trigger.new, Trigger.oldMap);

}


Trigger Handler Code:


public class OpportunityTriggerHandler {

    public static void handleAfterUpdate(List<Opportunity> newOpportunities, Map<Id, Opportunity> oldOpportunitiesMap) {

        OpportunityHelper.updateLastContactedDate(newOpportunities, oldOpportunitiesMap);

        // Other business logic or data manipulation

    }

}


Helper Code:


public class OpportunityHelper {

    public static void updateLastContactedDate(List<Opportunity> newOpportunities, Map<Id, Opportunity> oldOpportunitiesMap) {

        Set<Id> contactIds = new Set<Id>();

        for (Opportunity opportunity : newOpportunities) {

            if (opportunity.StageName != oldOpportunitiesMap.get(opportunity.Id).StageName) {

                contactIds.add(opportunity.ContactId);

            }

        }

        List<Contact> contactsToUpdate = [SELECT Id, Last_Contacted_Date__c FROM Contact WHERE Id IN :contactIds];

        for (Contact contact : contactsToUpdate) {

            contact.Last_Contacted_Date__c = Date.today();

        }

        update contactsToUpdate;

    }

}



5. Scenario: Preventing Record Deletion Based on Business Rules

Description: In certain cases, you might want to prevent users from deleting records based on specific business rules. Here's an example of a trigger scenario that prevents the deletion of an Account record if it has related Opportunities.


Trigger Code:


trigger AccountTrigger on Account (before delete) {

    AccountTriggerHandler.handleBeforeDelete(Trigger.old);

}



Trigger Handler Code:


public class AccountTriggerHandler {

    public static void handleBeforeDelete(List<Account> accounts) {

        AccountHelper.preventDeletionWithOpportunities(accounts);

        // Other business logic or data manipulation

    }

}



Helper Code:


public class AccountHelper {

    public static void preventDeletionWithOpportunities(List<Account> accounts) {

        List<Id> accountIds = new List<Id>();

        for (Account account : accounts) {

            accountIds.add(account.Id);

        }

        List<Opportunity> relatedOpportunities = [SELECT Id FROM Opportunity WHERE AccountId IN :accountIds];

        if


 (!relatedOpportunities.isEmpty()) {

            for (Account account : accounts) {

                account.addError('Cannot delete Account with related Opportunities.');

            }

        }

    }

}

Conclusion:

These top five trigger scenarios demonstrate common use cases in Salesforce and highlight the importance of separating trigger logic from business logic by using trigger handlers and helper classes. By following best practices and modularizing your code, you can build robust and scalable triggers that adhere to Salesforce development standards.


Remember to customize the code examples to fit your specific requirements and perform thorough testing before deploying to production. By leveraging triggers effectively, you can automate complex processes and maintain data integrity within your Salesforce org.

Post a Comment

0 Comments