function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
EmilyCobbEmilyCobb 

Invocable Method returning List of List

I'm working with cloud flow designer and having some issues with the Apex class callout. I know I will need to return a List<List<CustomSobject>> from the invocable method in order to process the collection. But I am stuck overcomplicating the code in order to get what I would normally use a List for into the right format to return. I probably need an additional for loop but am not sure of the format. If I only return a List<CustomSobject> the Flow will not be able to process it as a collection returning multiple records. Below is my method, this current version compiles but the flow will throw an "List index out of bounds" error. Nor is it correct, as I want all the elements returned not just the first. Thanks in advance.
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];

    for (Create_Product__c products: productsAvailable ){
        List<Create_Product__c> responseList = new List<Create_Product__c>();

        responseList[0].Product_Code__c = products.Product_Code__c;
        responseList[0].Starting_Price__c = products.Starting_Price__c;

        wrapper.add(responseList);
    }

    return wrapper;
}

 
Best Answer chosen by EmilyCobb
Gulshan Raj 4Gulshan Raj 4
Refer my comment at line number 10 and 12 as follows:
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];

    for (Create_Product__c products: productsAvailable ){
		// This will only create list having no records
        List<Create_Product__c> responseList = new List<Create_Product__c>();
		//responseList is list having no record, therefore responseList[0] throws exception
        responseList[0].Product_Code__c = products.Product_Code__c;
        responseList[0].Starting_Price__c = products.Starting_Price__c;

        wrapper.add(responseList);
    }

    return wrapper;
}

I don't know in what structure you need List<List<Create_Product__c>>, but still I can provide you two correct approaches.

 First Correct approach:
 
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];
	List<Create_Product__c> responseList;
    for (Create_Product__c products: productsAvailable ){
        responseList = new List<Create_Product__c>();
        responseList.add(products);
        wrapper.add(responseList);
    }

    return wrapper;
}

Second correct approach:
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];
	List<Create_Product__c> responseList=new List<Create_Product__c>();
    for (Create_Product__c products: productsAvailable ){       
        responseList.add(products);       
    }
	wrapper.add(responseList);
    return wrapper;
}

Thanks
Gulshan Raj
 

All Answers

Gulshan Raj 4Gulshan Raj 4
Refer my comment at line number 10 and 12 as follows:
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];

    for (Create_Product__c products: productsAvailable ){
		// This will only create list having no records
        List<Create_Product__c> responseList = new List<Create_Product__c>();
		//responseList is list having no record, therefore responseList[0] throws exception
        responseList[0].Product_Code__c = products.Product_Code__c;
        responseList[0].Starting_Price__c = products.Starting_Price__c;

        wrapper.add(responseList);
    }

    return wrapper;
}

I don't know in what structure you need List<List<Create_Product__c>>, but still I can provide you two correct approaches.

 First Correct approach:
 
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];
	List<Create_Product__c> responseList;
    for (Create_Product__c products: productsAvailable ){
        responseList = new List<Create_Product__c>();
        responseList.add(products);
        wrapper.add(responseList);
    }

    return wrapper;
}

Second correct approach:
@InvocableMethod(label = 'Services Available')
public static List<List<Create_Product__c>> getServices(List<Address__c> addressInput){
    //created to hold return list of lists
    List<List<Create_Product__c>> wrapper = new List<List<Create_Product__c>>();

    //The data set to be returned
    List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c];
	List<Create_Product__c> responseList=new List<Create_Product__c>();
    for (Create_Product__c products: productsAvailable ){       
        responseList.add(products);       
    }
	wrapper.add(responseList);
    return wrapper;
}

Thanks
Gulshan Raj
 
This was selected as the best answer
EmilyCobbEmilyCobb
Thanks Gulshan! The second approach worked perfectly for me. Appreciate the assistance. 
Ginjupalli AviGinjupalli Avi
Thanks @Gulshan second approch is working....!