How can I use the “before trigger” and “after trigger”?

221    Asked by DominicPoole in Salesforce , Asked on Apr 9, 2024

I am currently working on a Salesforce project in which I need to enforce a custom validation rule before the process of inserting records into a specific object. Additionally, I need to update related records in another Object after the insertion of records into the first object. How can I do this by using “before trigger” and “after trigger”? 

Answered by David

 In the context of Salesforce, here is how you can use the “before trigger” and “after trigger” in Salesforce apex:-

Before trigger

You can use this trigger for enforcing a custom validation rule before the process of inserting records into a specific Object.

Here is an example of this trigger which would check a custom validation rule before inserting an account record:-

// Trigger Name: CustomObjectBeforeInsert
Trigger CustomObjectBeforeInsert on Custom_Object__c (before insert) {
    // Create a list to store records with validation errors
    List recordsWithErrors = new List();
    // Iterate over the records being inserted
    For (Custom_Object__c obj : Trigger.new) {
        // Check if Custom_Field__c contains the substring ‘Invalid’
        If (obj.Custom_Field__c != null && obj.Custom_Field__c.contains(‘Invalid’)) {
            // Add the record to the list of records with errors
            recordsWithErrors.add(obj);
        }
    }
    // Check if there are records with validation errors
    If (!recordsWithErrors.isEmpty()) {
        // Create an error message to display to the user
        String errorMessage = ‘The Custom_Field__c cannot contain the substring “Invalid”.’;
        // Iterate over the records with errors and add the error message
        For (Custom_Object__c objWithError : recordsWithErrors) {
            objWithError.addError(errorMessage);
        }
    }
}
After trigger
You can use a this particular trigger for updating related records in another Object after the insertion of records into the first object.
Here is an example of this trigger which would update related contact records after inserting account record:-
// Trigger Name: CustomObjectAfterInsert
Trigger CustomObjectAfterInsert on Custom_Object__c (after insert) {
    // Create a list to store related records that need to be updated
    List relatedRecordsToUpdate = new List();
    // Iterate over the inserted Custom_Object__c records
    For (Custom_Object__c customObj : Trigger.new) {
        // Check if a related record in Related_Object__c needs to be updated
        If (customObj.Related_Record_Id__c != null) {
            Related_Object__c relatedRecord = new Related_Object__c();
            relatedRecord.Id = customObj.Related_Record_Id__c; // Assuming a field named Related_Record_Id__c holds the related record’s ID
            // Perform the required update on the related record (e.g., set a field value)
            relatedRecord.Custom_Field__c = ‘Updated Value’;
            // Add the related record to the list for update
            relatedRecordsToUpdate.add(relatedRecord);
        }
    }
    // Perform the update on related records in Related_Object__c
    If (!relatedRecordsToUpdate.isEmpty()) {
        Update relatedRecordsToUpdate;
    }
}


Your Answer

Interviews

Parent Categories