3

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;
    }
}

2 Answers 2

0

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.

4
  • 1
    I don't understand what this solution means, though. How did you build the check in flow?
    – Chance
    Commented Jan 12, 2022 at 15:35
  • Havent had to do this in a while anymore but I had a count of items in Collection after the apex ran. If count = 0 then that path would exit flow. If > 0 then continue. The Apex was returning 10 SobjectCollections and 1 of them would have 10 records and the other 9 had 0 so we just want the 1 to continue.
    – ddeve
    Commented Jan 12, 2022 at 16:03
  • I ended up with the solution I posted here: salesforce.stackexchange.com/questions/321274/…
    – Chance
    Commented Jan 13, 2022 at 17:51
  • The use cases were different then which is fine. The solution I have here is when attempting to bulkify the records so having 100 contacts and want to output an Sobject Collection with all 100. The solution you reference would return 100 Sobject Variables all with 1 record.
    – ddeve
    Commented Jan 14, 2022 at 18:40
0

I had ran into this issue when i was using James Simones' Apex Rollup(definitely check it out) attached flow error i was getting. [Flow Error2This

This is correct way of handling invocable returns to prevent error.

Correct Way of Handling Invocable Returns

New contributor
Rynn AethelWulf is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

You must log in to answer this question.

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