Error on flow execution – The number of results does not match the number of interviews that were executed

apexinvocable-methodvisual-workflow

I have a simple flow which is triggered from PB on record create.
Thw flow is calling to Invocable Apex class in order to calculate business days and than – update the record.

The process works just fine on 1 record, but when there are multiple records, i'm recieveing the following error message:
"Error – The number of results does not match the number of interviews that were executed"

This is the code that i'm using

global class SW_FunctionsFromFlow {
global class CalculateWorkDaysVarriables {
    @InvocableVariable(required=true)
    public String oppID;
    @InvocableVariable(required=true)
    public Datetime startDate;
    @InvocableVariable(required=true)
    public Integer workHours;
}

@InvocableMethod(label='Calculate and Populate Opportunity End Date')
public static void InvokeCalculationOfWorkDaysWithHolidays(List<CalculateWorkDaysVarriables> request){
    SW_GlobalHelper helper = new SW_GlobalHelper();
    List<Opportunity> oppToUpdate = new List<Opportunity>();

    for(CalculateWorkDaysVarriables req : request){

        Datetime endOfWork = helper.calculatingWorkDaysWithHolidays(req.startDate, req.workHours);
        Datetime endOfWorkCalculated = Datetime.newInstance(endOfWork.year(), endOfWork.Month(), endOfWork.Day(), 22, 01, 00).addDays(-1);
        Opportunity oppRecord = new Opportunity(Id = req.oppID, End_Date__c = endOfWorkCalculated);
        oppToUpdate.add(oppRecord);
    }

    if(!oppToUpdate.isEmpty()){
        update oppToUpdate;
    }
}

}

Does anyone know what am I doing wrong ?
Thanks !

Best Answer

I'm going to ask for clarification from the docs team, but it sounds like not having a return value is the root cause. The number of output result values must match the number of input values, as the error states. You could work around this by returning the updated records:

@InvocableMethod(label='Calculate and Populate Opportunity End Date')
public static List<Opportunity> InvokeCalculationOfWorkDaysWithHolidays(List<CalculateWorkDaysVarriables> request){
    SW_GlobalHelper helper = new SW_GlobalHelper();
    List<Opportunity> oppToUpdate = new List<Opportunity>();

    for(CalculateWorkDaysVarriables req : request){
        Datetime endOfWork = helper.calculatingWorkDaysWithHolidays(req.startDate, req.workHours);
        Datetime endOfWorkCalculated = Datetime.newInstance(endOfWork.year(), endOfWork.Month(), endOfWork.Day(), 22, 01, 00).addDays(-1);
        Opportunity oppRecord = new Opportunity(Id = req.oppID, End_Date__c = endOfWorkCalculated);
        oppToUpdate.add(oppRecord);
    }
    update oppToUpdate;
    return oppToUpdate;
}