+ Start a Discussion
Vaibhab ShahVaibhab Shah 

Lightning error : Return type is a wrapper class


I am having an apex controller with the following method whose return type is again a different class. Below is my code:

public static AccountPagerWrapper getData (Decimal pageNumber ,Integer recordToDisply)
        Integer pageSize = recordToDisply;
        Integer offset = ((Integer)pageNumber - 1) * pageSize;
        String queryCount = 'SELECT count() FROM Account';
        String finalQuery = 'dynamic query';
        List<Account> returnList = new List<Account>();
        returnList = database.query(finalQuery);       
        AccountPagerWrapper obj =  new AccountPagerWrapper();
        obj.pageSize = pageSize;
        obj.page = (Integer) pageNumber;
        obj.total =  database.countQuery(queryCount);  
        obj.accounts = returnList;
        return obj;

public class AccountPagerWrapper {
    @AuraEnabled public Integer pageSize {get;set;}
    @AuraEnabled public Integer page {get;set;}
    @AuraEnabled public Integer total {get;set;}
    @AuraEnabled public List<Account> accounts {get;set;}

Can anyone tell how to assign the dynamic query results in accounts property of the AccountPagerWrapper. Line is marked with bold.

I am simply assigning the dynamic query results in the property but it is throwing an error while binding it with the table.

Boss CoffeeBoss Coffee
Could you provide the exact error it displays?
Ajay K DubediAjay K Dubedi
Hi Vaibhab,

If you need to assign a dynamic query then you can assign the query in the string
for example- 
String nameLike = '%test%';
        String finalQuery = 'select id, name from acount where name like ; + nameLike;

        List<Account> returnList = new List<Account>();
        returnList = database.query(finalQuery);

Whatever you want to query assign them into string format then concatenate it into final query string. The above example is for your understanding.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi