Bulk Processing for Large Datasets – Triggers and Bulk Processing – Salesforce Certified Platform Developer I Study Guide

7.3  Bulk Processing for Large Datasets

Collections like lists, sets, and maps are essential for bulk processing. They allow you to group and manipulate records efficiently. Fetch required data outside the loop and use collections to match and process records. Utilize bulk DML operations to update, insert, or delete multiple records in a single transaction.

In the following example, the trigger creates subsidiary accounts for new parent accounts where the BillingAddress fields are null. The provided trigger is designed to handle bulk processing efficiently.

trigger CreateSubsidiaryAccounts on Account (after insert) {

List<Account> newSubsidiaryAccounts = new List<Account>();

for (Account acc : Trigger.new) {

  • Check if BillingAddress components are empty for new accounts if (acc.BillingStreet == null && acc.BillingCity == null && acc.BillingState == null && acc.BillingPostalCode == null && acc.BillingCountry == null) {
  • Create a new subsidiary account newSubsidiaryAccounts.add(new Account(

Name = ‘Subsidiary Account’, // Adjust as needed ParentId = acc.Id

));

}

}

  • Insert new subsidiary accounts outside the loop if (newSubsidiaryAccounts.size() > 0) {

insert newSubsidiaryAccounts;

}

}

\ 1.\ It Operates on Collections: The loop processes multiple accounts in a single iteration, which is more efficient than processing one record at a time.

\ 2.\ It Doesn’t Perform DML Operations Inside the Loop:

The creation of subsidiary accounts is collected in a list (newSubsidiaryAccounts), and the DML operation (insert) is performed outside the loop, which helps avoid hitting governor limits on DML statements.

\ 3.\ The Trigger Is Focused on a Specific Task: creating subsidiary accounts without unnecessary complexity, making it more maintainable.

To ensure its effectiveness with bulk data, always test triggers in a sandbox environment with representative data volumes before deploying to production.

Note! For small triggers with simple logic, like the examples we discussed for demonstration purposes earlier, it is acceptable to write the logic directly inside the trigger without creating a separate class. This approach can be convenient and straightforward, particularly for small-scale or one-off triggers.

But it’s important to keep in mind that as your codebase grows and becomes more complex, it is generally recommended by following best practices to separate the trigger logic into a separate class. This helps improve code maintainability, reusability, and testability.

Here’s how you can create a separate Apex class to provide the same functionality as in our last example but to handle the logic for creating subsidiary accounts separately.

Step 1: Create a new Apex class createSubsidiaryAccounts in Developer Console:

public class SubsidiaryAccountHandler { private static Boolean isExecuting = false;

public static void createSubsidiaryAccounts(List<Account> newAccounts) {

if (!isExecuting) {

isExecuting = true;

List<Account> newSubsidiaryAccounts = new List<Account>();

for (Account acc : newAccounts) {

  • Check if BillingAddress components are empty for new accounts if (acc.BillingStreet == null && acc.BillingCity ==

null && acc.BillingState == null && acc.BillingPostalCode == null && acc.BillingCountry == null) {

  • Create a new subsidiary account newSubsidiaryAccounts.add(new Account(

Name = ‘Subsidiary Account’, // Adjust as needed ParentId = acc.Id

));

}

}

  • Insert new subsidiary accounts outside the loop if (newSubsidiaryAccounts.size() > 0) {

insert newSubsidiaryAccounts;

}

isExecuting = false;

}

}

}

Step 2: Go to Setup ➤ Object Manager tab ➤ Account ➤ Triggers ➤ CreateSubsidiaryAccounts and click the “Edit” button.

trigger CreateSubsidiaryAccounts on Account (after insert) { SubsidiaryAccountHandler.createSubsidiaryAccounts(Trigger.new);

}

Our CreateSubsidiaryAccounts trigger has been updated now. If you need to perform similar logic in another context, you can easily reuse the SubsidiaryAccountHandler class.