Skip to main content
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.
2 answers
  1. Nov 15, 2019, 9:38 AM
    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

     
0/9000