[SalesForce] How invocable method and bulk behavior in Process Builder work

I am trying to understand how invocable method called from Process builder behaves.
I have Process Builder process on Account which has apex action.
Apex action needs to be executed for record inserts.

Below are my questions,

  1. if 10 account records are inserted "at the same time", Process builder process is called 10 times, separately for each record that is being inserted.
    or only once ?

  2. if process has apex action and if 10 records are inserted at the same time,
    is the apex action called 10 times separately or only once for all 10 records ?

  3. Process builder calls apex action method which takes ID as the parameter.
    can I write invocable method that takes ID as the parameter instead of List ?
    I saw some examples where invocable methods are taking List or List as parameter.
    Is it possible that apex action invoked from process builder is called for multiple records at the same time ?

Any insight into process builder working would be very helpful.

Best Answer

You can also create an @InvocableMethod that takes in a List of a custom class. By doing this, you can pass in parameters in the Process Builder

public with sharing class ActionForProcessBuilder {

    @InvocableMethod(label='Do Action')
    public static void doAction(Request[] requests) {
        // Process requests
        // Example: Query parents in bulk
        Map<Id, Parent__c> parents = new Map<Id, Parent__c>();

        for(Request request : requests) {
            parents.put(request.parentId, null);
        }

        parents = new Map<Id, Parent__c>([
            SELECT Id, Due_Date__c
            FROM Parent__c
            WHERE Id IN :parents.keySet()
        ]);

        for(Request request : requests) {
            final Parent__c parent = parents.get(request.parentId);
            if(request.adjustedDueDate != null && parent.Due_Date__c < request.adjustedDueDate) {
                // Do something
            }
        }
    }

    public with sharing class Request {
        @InvocableVariable(label='Record ID' required=true)
        public Id recordId;

        @InvocableVariable(label='Parent ID' required=true)
        public Id parentId;

        @InvocableVariable(label='Adjusted Due Date' required=false)
        public Date adjustedDueDate;
    }
}
Related Topic