0

I am trying to make a simple APEX Class that returns a records related attachments. When I do the following SOQL query in the Query Editor I get the 3 expected results:

SELECT Id
FROM ContentDocumentLink
WHERE LinkedEntityId = 'a36Ec00000001s9IAA'`

However, when I do the same query in my apex class, nothing is returned in the list. I have been stuck on this for a while and any help would be appreciated. The recordId value is displaying in the debug as it should. Here is my entire Apex class so far:

global class GetRelatedAttachments {
    
    @InvocableMethod(label='Get Related Attachments')
    global static List<ContentDocumentLink> getRelatedAttachments(List<FlowInputs> requests){
        
        String recordId = requests[0].recordId;
        
        List<ContentDocumentLink> IdList = [
            SELECT ContentDocumentId
            FROM ContentDocumentLink
            WHERE LinkedEntityId =: recordId
        ];
        
        System.debug('recordId: '+ recordId);
        System.debug('List: '+ IdList);
        return IdList;
    }
    
    global class FlowInputs{
       
        @InvocableVariable
        global Id recordId;
        
    }

}
2
  • is it the same running user (query editor vs invocable apex)? do you observe the invocable error during flow debugging or via apex testmethods?
    – cropredy
    Jan 18 at 23:32
  • @cropredy I believe it is the same running user. When I run the flow debugger and get to the apex class I get the error: "Error Occurred: The number of results does not match the number of interviews that were executed in a single bulk execution request." Jan 18 at 23:41

2 Answers 2

1

OK, your problem is that you need to return a List of lists

global static List<List<ContentDocumentLink>> getRelatedAttachments(List<FlowInputs> requests){

    ...
    return new List<List<ContentDocumentLink>> {idList};
}

Apex Actions are bulkified by Flow. If n Flow Interviews for a transaction reach an apex action, their input args are collected into a list that the invocable processes in bulk and the return from the invocable must be a list of the same size and same order as the incoming list.

This is true even in Screen Flows which typically pass to an invocable apex an input argument collection of size = 1 and should return, if they return at all, a list of size 1.

Notes

  • I'd seriously suggest you rename the variable idList to be contentDocumentLinks as that is what the SOQL fetches

  • I'd also suggest renaming the class FlowInputs to FlowInput or better, Request. Classnames should not be plural in almost all situations.

1
  • This worked. Thanks so much! Jan 19 at 0:55
0

I think you need to return a List<Id> instead of a List<ContentDocumentLink> for an invocable method. You can also wrap the return type in a class with an invocable variable. Refer https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm

1
  • The documentation you point do says that a list of an SObject type is allowed as a return type, so I don't think this is the issue.
    – Derek F
    Jan 19 at 0:16

You must log in to answer this question.

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