0

I have resolved this error in the past when the input is more records than the output (i.e. sending 10 recordIds and returning a single List) however, here I am getting error for autolaunched flow when only input is a partial query string.

If I set Apex to "LIMIT 1" this works. Anything greater and it throws the error The number of results does not match the number of flow interviews which I do not understand since I am returning a single List to the flow. It appears to be evaluating the # of items inside the list. Something must be wrong with my Results method?

This class is just using the generic Sobject to perform a query and return the queried records. For now the input is just the fields to query. The class available on UnofficialSF for this has much too much extra stuff for my needs.

public without sharing class SobjectCreateClass {
@InvocableMethod(label='Collect Bulkified Records' category='Bulkify Records')
public static List<Results> execute(List<Request> requestList) {

  try{
    //Generate set of string for record fields to use in dynamic SOQL
    Set<String> fieldSet = new Set<String>();
    for (Request curRequest : requestList) {
      fieldSet.addAll(curRequest.fieldList);
    }
    //Get Sobject Type
   Schema.SobjectType sObjectType = Schema.hdone__Address__c.getSobjectType();
    
    String queryFields = String.join(new List<String>(fieldSet), ','); //Set not iterable so join does not work. Problem outlined on SFSE casting to Iterable
    List<SObject> outputList = Database.query(
      'SELECT ' +
      queryFields +
      ' FROM ' +
      sObjectType +
      ' WHERE hdone__SubmitForCertification__c = TRUE AND hdone__AddressRelated_External__c = NULL LIMIT 1'
    );


    // Add all of the values to an initial List
    List<Results> responseList = new List<Results>();
    Results results = new Results();

    for (SObject curSobject : outputList) {
      if (curSobject != null) {
        Map<String, Object> objMap = curSobject.getPopulatedFieldsAsMap();
        for (String field : fieldSet){
          if (!objMap.containsKey(field)){
            curSobject.put(field,null);
          }
        }
        results.outputMember.add(curSobject);
        responseList.add(results);
        System.debug('Size of ResponseList ' + responseList.size());
      }
    }


    return responseList;
  }
  catch(Exception e){
    throw new SobjectBulkifyException(e.getMessage());
  }

public class Request {      
  @InvocableVariable(required=true)
  public List<String> fieldList;

}

// An extra step added with this wrapper class to allow this inferface to support bulkification
public class Results { }
  @InvocableVariable
  public List<SObject> outputMember;

}

public class SobjectBulkifyException extends Exception {}

}

2 Answers 2

0

responseList.size() must match requestList.size().

You need to adjust your filters so you get all the right values, and you need to change the code so that each response matches each request element.

This may involve using a map or other value. If your intent is to just do this callout for all records, then the alternative is to set up a series of Response values that match the request size:

List<Results> responseList = new List<Results>();
while(responseList.size() < requestList.size()) {
  responseList.add(new Results());
}

I'm not entirely sure how to optimize your code in the face of multiple incoming requests (particularly the query), but you do need to ensure that the input and output sizes match.

2
  • What you posted is the situation where the flow is submitting X records and the Apex is returning < X. This situation is the flow is submitting 1 (query string) and the Apex is returning a List[X] values. This shouldn't be an error since there is just 1 List so X = X; however I have something incorrect so it's looking at the # of records in the List vs just the List.
    – ddeve
    Feb 16, 2021 at 21:05
  • @ddeve No, your current code returns more than 1 if you don't LIMIT 1, which is the problem.
    – sfdcfox
    Feb 17, 2021 at 0:30
0

Problem was in query as commented. Moved query inside loop and all is fine.

You must log in to answer this question.

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