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
Rakesh K 60Rakesh K 60 

Can't I return a wrapper class for invocable method?

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
AbhinavAbhinav (Salesforce Developers) 
Hi Rakesh,

Consideration of return type for invocable method.

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<sObject>) or a list of lists of the generic sObject type (List<List<sObject>>).

> 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.

reference:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_InvocableMethod.htm

Thanks!