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