2

I am running a scheduled batch job daily on 25,000 records and growing. I Want to be able to leverage Salesforce Flow for processing the data because it gives more control to the Admin for manipulating outputs.

I checked the salesforce developer documentation and found the Flow.Interview class.

https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/flow_interview_class.htm

In the documentation, they pass a single record into the flow, which from my understanding is not bulkified and would result in me having to launch a loop for each record in the batch. Unfortunately, this is not scalable and will lead to governor limit issues.

So my question is how can we call the Flow.Interview class and ensure that the way we pass in inputs is bulkified?

2 Answers 2

2

First, a relevant question/answer on this topic re: Flow.Interview

I can imagine a couple of options that might work for some applications:

Option 1 - Use a Schedule-triggered Flow instead of the batch job

You'll get a minimum of 250K interviews per day and it will be bulkified

Option 2 -- Use Apex DML with accompanying Flow to do the work

You could have your batch job's execute() update a toggle on the object in question.

When the toggle is true, you have a record-triggered flow do the "work". SFDC will bulkify the scope's flow interviews

At the conclusion of each interview, toggle the field back to its initial state.

1
  • Amazing solution... Thank you! Commented Dec 4, 2021 at 7:05
1

Record-Trigger by cropredy is very innovative and the only way to run bulk flows for a long time.

Winter '23 introduced the Invocable.Action methods which enable Apex to launch a collection of flows. The flow logic is bulkified automatically. Your flow can be designed from the perspective of a single request, then called from the batch job like this:

(It assumes the flow has an SObject variable 'record' that is marked [X] Available for input.)

public void execute(Database.BatchableContext context, List<SObject> records)
{
    Invocable.Action impl;
    impl = Invocable.Action.createCustomAction('flow', 'Create_Expense');
    impl.clearInvocations(); // prepare the action using name of flow
    
    for (SObject record : records)
    {
        impl.addInvocation(); // add each interview
        impl.setInvocationParameter('record', record);
    }

    // bulk run
    impl.invoke();
}

The batch example stages a collection of interview requests and starts them simultaneously. It uses Flow's bulk engine to handle interviews in the same request. Eg, if your batch scope size is 200, then 200 invokes of any 'Create Records' step will use only 1 DML.

Here are the docs for the Invocable.Action classes and methods: developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_class_Invocable_Action

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .