0

I'm writing an Apex Invocable method that should return a wrapper class data and my code is as below.

@InvocableMethod
    global static List<List<prodWrap>> getProducts() 
    {
        List<List<prodWrap>> wrapper = new List<List<prodWrap>>();
         List<Product2> productsAvailable = [SELECT Id, Name FROM Product2 WHERE Name LIKE '%Hoses%'];
        List<prodWrap> responseList=new List<prodWrap>();
        for (Product2 products: productsAvailable ){       
            PricebookEntry pe=[SELECT Id, Pricebook2Id, Product2.Name, UnitPrice FROM PricebookEntry where Product2Id=:products.Id order by createddate desc LIMIT 1];
            responseList.add(new prodWrap(products.Name, Integer.valueOf(pe.UnitPrice)));
        }
        wrapper.add(responseList);
        return wrapper;
    }
    
    public class prodWrap {
        public String name {get; set;}
        public Integer price {get; set;}
        public prodWrap(String name, Integer price){
            this.name = name;
            this.price = price;
        }
    }

the error that I get is InvocableMethod methods do not support return type of List<List<ClassName.prodWrap>>. Please let me know where am I going wrong and how can I fix this.

Thanks

1

As per the documentation, you can return:

A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.

If adding the annotations to the ProdWrap properties doesn't resolve, you may find you need to use a top level class, or you might need to structure the wrapper to contain the nested list.

1
  • Hey this helped. :-) May 31 '21 at 17:11
1

Well actually this time error is describing exactly what is happening. You cannot return List of Lists of the type you defined.

Citation from docs:

If the return type is not Null, the data type returned by the method must be one of the following: A list of a primitive data type or a list of lists of a primitive data type – the generic Object type is not supported. A list of an sObject type or a list of lists of an sObject type. A list of the generic sObject type (List) or a list of lists of the generic sObject type (List<List>). A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.

Looking at your code, if you are solid on list of lists, you can actually return List<List> as it is possible according to documentation and you can get name from the query itself. UnitPrice can be an issue but it depends if you can live with it on return and later convert (or it will be parsed automatically even) to Integer

1

InvocableMethod methods can return user-defined type variables. However as mentioned in the documentation:

A list of a user-defined type, containing variables of the supported types above or user-defined Apex types, with the InvocableVariable annotation. Create a custom global or public Apex class to implement your data type, and make sure that your class contains at least one member variable with the invocable variable annotation.

    public class prodWrap {
        @InvocableVariable
        public String name;

        @InvocableVariable
        public Integer price;

        public prodWrap(String name, Integer price){
            this.name = name;
            this.price = price;
        }
    }

Your apex wrapper needs to have InvocableVariables in them.

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.