1

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...? If I only use 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 "Attempt to de-reference a null object" error.

@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;
}
2
  • 1
    Think you need to work out how the List<Address__c> addressInput argument affects what you are to return. Right now you could replace the for loop with wrapper.add(productsAvailable); to avoid the error but there would only be one item in the outer list which I assume is not what you need.
    – Keith C
    Mar 3 '17 at 21:43
  • Correct, I'll need the full list returned. I do realize that I'm not doing anything with the input yet - wanted to get the basic concept working before adding business logic complexity.
    – EMC
    Mar 3 '17 at 22:08
1

List<Create_Product__c> responseList = new List<Create_Product__c>(); collection is declared inside for loop and without adding an element to collection, 0th element is being accessed, which will throw List out of bound exception.

You can probable try modifying code to

List<Create_Product__c> productsAvailable = [SELECT Product_Code__c, Starting_Price__c FROM Create_Product__c]; wrapper.addAll(productsAvailable);

Reference Links
List Class, addAll method
System.ListException: List index out of bounds: 0

0

If you're getting a null object error from within your flow, I'm going to guess that could be similar to a null pointer exception (depends on what's considered an object in the flow).

That tells me that in your loop below, either Product_Code__c or Starting_Price__c could be empty or null. It would seem that you need to add a check to your code to make certain that both of those values are populated before adding responseList[0] (NOT responseList) to your wrapper.

You're not iterating withing each Product_Code__r and Starting_Price__r to see if a list of either of those exists (I assume that's possible, but perhaps not), instead only checking for a single value. As such, you only want to add responsList[0] provided both the product_code and the starting_price have values.

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;

    if(!products.Product_Code__c.isEmpty() && !products.Starting_Price__c.isEmpty()){
        wrapper.add(responseList[0]);
    }
}
3
  • 1
    OP has confusing variable name in the for loop - should be for (Create_Product__c p : productsAvailable)
    – cropredy
    Mar 4 '17 at 18:20
  • I have problem of getting list. I have set up so nested List<List<ObjectName>> but in flow UI it thinks the returning object is single object instead of collection May 28 '20 at 0:13
  • @ShinyaKoizumi You're comments on a 3 yr old post should be posted as a new question. Feel free to reference this question if you think it would be helpful.
    – crmprogdev
    May 29 '20 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.