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
Vaibhab ShahVaibhab Shah 

Error : Wrapper class in apex

Hi,

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

@AuraEnabled
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.
Soyab HussainSoyab Hussain
Hi Vaibhab Shah,

Please change the string with the actual query, use this below code.
 
public class AccountContact{
    @AuraEnabled
    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 = 'SELECT Id FROM Account';
        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;}
    }
}

Regards,
Soyab
Deepali KulshresthaDeepali Kulshrestha
Hi Vaibhab,

I have gone through your problem. To assign the dynamic query results in the accounts property of the AccountPagerWrapper.
In my code myTestString is a string for applying condition in the database query. 
 
Please go through the code given below.

public class WrapperClass {
    @AuraEnabled
public static AccountPagerWrapper getData (Decimal pageNumber ,Integer recordToDisply)
    {
        Integer pageSize = recordToDisply;
        Integer offset = ((Integer)pageNumber - 1) * pageSize;
        
        String queryCount = 'SELECT count() FROM Account';
        
        String myTestString = 'TestName';
        List<Account> returnList = new List<Account>();
        returnList = database.query('Select ID,Name from Account where Name= : myTestString');       
        
        AccountPagerWrapper obj =  new AccountPagerWrapper();
        obj.pageSize = pageSize;
        obj.page = (Integer) pageNumber;
        
        obj.total =  database.countQuery(queryCount);  
        obj.accounts = returnList;
        System.debug('obj.accounts --->'+obj.accounts );
        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;}
   }
}

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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com