1

I am returning a list of lists from Apex Invocable Method like below

Here is my Apex defined class which I am using it later to create an Apex defined object inside of the flow. This class is in its own file.

    public with sharing class CustomerAccountContactDto {
        public CustomerAccountContactDto() {
            //set defaults
            this.errorOcurred = false;
            this.passwordChangeIsRequired = false;
            this.passwordIsSet = false;
        }
    
        @AuraEnabled @InvocableVariable(required=true)
        public String contactName;
    
        @AuraEnabled @InvocableVariable(required=true)
        public String contactRoleType;
    
        @AuraEnabled @InvocableVariable(required=true)
        public Boolean errorOcurred;
    
        @AuraEnabled @InvocableVariable(required=false)
        public Boolean passwordChangeIsRequired;
    
        @AuraEnabled @InvocableVariable(required=false)
        public Boolean passwordIsSet;
    }

    @InvocableMethod(
        Label='getPINPassword'
        Description='get PIN or Password Authentication Result by Account number and Object Id'
    )
    public static List<List<CustomerAccountContactDto>> getPassword(List<Request> requestList) {
            CustomerAccountContactDto response = new CustomerAccountContactDto();
            CustomerAccountContactDto response1 = new CustomerAccountContactDto();
response.contactName = 'First Record Name';
response.passwordChangeIsRequired = false;
response.passwordIsSet = true;
            
response1.contactName = 'Second Record Name';
response1.passwordChangeIsRequired = false;
response1.passwordIsSet = true;
            
        List<List<CustomerAccountContactDto>> responseWrapper = new List<List<CustomerAccountContactDto>>();
                System.debug('Response -'+response);
                System.debug('Response -'+response1);
                responseWrapper[0].add(response);
                responseWrapper[1].add(response1);
                return responseWrapper;
    }

The debug statements do print out the expected values from response and response1.

I created an Apex defined data type in the flow like below which references the above created CustomerAccountContactDto class and after I use this variable as an output variable for the apex action enter image description here

enter image description here

And finally when I run the flow just after passing the ape action node it shows the below error-

 An Apex error occurred: System.ListException: List index out of bounds: 0
7
  • 2
    There's not enough code here for us to help you. Context is often important. I'd rather see too much code, which we can then trim down, than not enough, where we have to guess/make assumptions what you did.
    – sfdcfox
    Jan 5, 2023 at 20:14
  • We need to understand what response and response1 are, the size of the input parameter list (the size of the output list must match the size of the input list) and just how you want to structure that responseWrapper.
    – Phil W
    Jan 5, 2023 at 20:27
  • @sfdcfox I have added more details, let me know if it is good
    – sumchans
    Jan 5, 2023 at 20:28
  • @PhilW I have added more details, let me know if it is good
    – sumchans
    Jan 5, 2023 at 20:28
  • Are you expecting to return a pair of "responses" for each Request?
    – Phil W
    Jan 5, 2023 at 20:30

1 Answer 1

0

The issue was with not populating the list in the correct way.

I created two new lists -

List<List<CustomerAccountContactDto>> customerAccountcontacts = new List<List<CustomerAccountContactDto>>();
List<CustomerAccountContactDto> contacts = new List<CustomerAccountContactDto>();

Then I populated the contacts list with the responses and finally added the contacts list to the customerAccountcontacts list.

response.contactName = 'First Record Name';
response.passwordChangeIsRequired = false;
response.passwordIsSet = true;
contacts.add(response);         
response1.contactName = 'Second Record Name';
response1.passwordChangeIsRequired = false;
response1.passwordIsSet = true;
contacts.add(response1);

customerAccountcontacts.add(contacts);

This way the flow is no more showing the out of bounds error.

5
  • This code still doesn't conform to flow requirements; if the flow provides n Request records in the parameters you must return n lists.
    – Phil W
    Jan 5, 2023 at 21:15
  • It is one list of lists that I am returning to the flow. I was able to retrieve multiple records inside of a screen component. The records are returned as part of an api call.
    – sumchans
    Jan 5, 2023 at 21:18
  • 2
    Read up on invocable methods. Flows can and do bulkify calls to your method when multiple executions of your flow are running in parallel. You can therefore get multiple requests. And then you HAVE to return a value per request (where here a value is a list) and these must be in the same order as the related requests.
    – Phil W
    Jan 5, 2023 at 21:23
  • Okay sure. Will look into it..
    – sumchans
    Jan 5, 2023 at 21:26
  • see also related question/answer
    – cropredy
    Jan 5, 2023 at 22:45

You must log in to answer this question.

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