Given the following class with an @InvocableMethod
:
public class InvocableFileCountAction {
public class FileCount {
@InvocableVariable
public Id recordId;
@InvocableVariable
public Integer fileCount;
public FileCount(Id recId, Integer count) {
this.recordId = recId;
this.fileCount = count;
}
}
@InvocableMethod(label='Get File Count' description='Gets the number of Files entries for a record' iconName='slds:standard:dashboard' callout='false')
public static List<FileCount> getFileCount(List<Id> recIds) {
// cast incoming IDs to a set of Strings to query text field FauxPolymorphicLookup__c
Set<String> textIds = new Set<String>((List<String>)recIds);
List<FileCount> counts = new List<FileCount>();
AggregateResult[] FileCounts = [SELECT FauxPolymorphicLookup__c recordId, Count(Id) fileCount
FROM FileContainerObject__c
WHERE FauxPolymorphicLookup__c IN :textIds
GROUP BY FauxPolymorphicLookup__c];
for (AggregateResult ar : FileCounts) {
FileCount fc = new FileCount((Id)(ar.get('recordId')),(Integer)ar.get('fileCount'));
counts.add(fc);
}
return counts;
}
}
How should I be handling the list of returned objects in the Flow? I'm calling the method from a record-triggered flow on Update to populate a field on the object. I assign the fileCount
value returned from the Apex Action to the field in a single Update Records element for the 'record that triggered the Flow'. It works fine when a single record is updated, but trying to update multiple records gives the An unhandled fault has occurred in this flow
error (it's just a test flow for trying the Action, so haven't put fault processing in).
What comes back to the Flow isn't a Collection, so I can't loop over it to find the object with the matching ID. Should the signature for the InvocableMethod be
public static List<Integer> getXFilesCount(List<Id> recIds)
but then return a List with a single element, the fileCount
from fileCounts[0]
, the first AggregateResult
? Or a List with all the fileCount
s? with Does Salesforce magically associate the right result with the right record somehow in the bulkified scenario?
recIds
is represented as the 0thFileCount
in the invocable results; same for 1st, 2nd, ... The invocable apex logic has to do this. Then SFDC will deliver the results to the correct Flow Interview. If the invocable is supposed to return a collection of values to each flow interview, then you return a list of listsList<Integer>
as long as I ensure it's in the same order as the inputList<Id>
?