Process Builder Bulkified

To create a bulkified process in Process Builder using an Apex class, we'll need to ensure that the Apex class and the invocable method within it are capable of handling bulk records efficiently.

Example

public class MyBulkifiedClass {

    @InvocableMethod(label='Process Bulk Records' description='Process bulk records efficiently')
    public static void processBulkRecords(List<MyObject__c> records) {
        // Your bulkified logic here
        List<SomeOtherObject__c> objectsToUpdate = new List<SomeOtherObject__c>();
        
        // Iterate over the input records
        for (MyObject__c record : records) {
            // Perform your business logic on each record
            SomeOtherObject__c relatedObject = new SomeOtherObject__c();
            relatedObject.Name = record.Name;
            relatedObject.Description__c = record.Description__c;
            // Add the modified related object to a list for bulk DML
            objectsToUpdate.add(relatedObject);
        }
        
        // Perform bulk DML operation
        if (!objectsToUpdate.isEmpty()) {
            try {
                insert objectsToUpdate;
            } catch (Exception e) {
                // Handle exception
                System.debug('Error occurred while inserting records: ' + e.getMessage());
            }
        }
    }

}

In this example:

We have an invocable method named processBulkRecords that accepts a list of MyObject__c records as input.

Inside the method, we iterate over the input records and perform some business logic on each record.

We then create instances of another object (SomeOtherObject__c) based on the data from MyObject__c records.

We add these modified related objects to a list (objectsToUpdate) for bulk DML operations.

Finally, we perform bulk DML insert operation on the list of related objects, handling any exceptions that may occur.

This Apex class with a bulkified invocable method can be used in Process Builder to process bulk records efficiently while ensuring compliance with Salesforce governor limits.



Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach