[SalesForce] Flow Error – The number of results does not match the number of interviews that were executed

Calling Flow from Process Builder and getting error. The Flow has Invocable Apex solely to collect the "bulkified" records that Salesforce gathers, it was my understanding this is how we gathered them to use later.

The Apex does no processing other than to create a List<List<Sobject>> and returns this to the Flow Sobject Collection variable.

The debug logs show 5 transactions started and 1 returned which is essentially what I expect as the 1 should be a collection with the 5 records. Tried adding a 0 min Wait before and after the Invocable Apex which didn't work.

Flow Error – The number of results does not match the number of interviews that were executed

public with sharing class ApexToBulkifyRecords_Confirmation {

    @InvocableMethod(label='Collect Bulkified Records')
    public static List<List<hdone__Confirmation__c>> doAction(Request[] requests) {
        // Process requests
        // Example: Query parents in bulk
        Map<Id, hdone__Confirmation__c> parents = new Map<Id, hdone__Confirmation__c>();

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

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

        // Flows must return List<List> for Sobject Collections
        List<List<hdone__Confirmation__c>> wrapper = new List<List<hdone__Confirmation__c>>();

        List<hdone__Confirmation__c> responseList = new List<hdone__Confirmation__c>();
        responseList.addAll(parents.values());

        System.debug('Size of ResponseList ' + responseList.size());

        wrapper.add(responseList);
        System.debug('Return wrapper size ' + wrapper.size());
        return wrapper;
    }

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

        @InvocableVariable(label='Object API Name' required=false)
        public String objectName;

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

Best Answer

I did resolve this by adding a check in Flow to check which returned Sobject Collections are empty and which are not. If using this Apex Class to submit 10 records, they will be bulkified to return 1 Collection with 10 records and 9 that have 0 records. The Flow needs all 10 to finish the transaction. Adding a check in Flow to end the Flow if SobjectCollection Count = 0, and continue when Count > 0 has resolved this.