How to count associated child records in visualforce page using aggregate function?

1.0K    Asked by AmyAvery in Salesforce , Asked on Sep 2, 2020
Answered by Amy Avery

public class exampleAggregate {

    public List Result {get;set;}

    public List SummaryList {get;set;}

    public List AcctList;

    public Map IdAccount;

    List Ids;

    public void show() {

        SummaryList = new List();

        Result = new List();

        Ids = new List();

        AcctList = new List();

        IdAccount = new Map();

        Result = [SELECT Count(Id) Total , AccountId FROM Contact WHERE AccountId != null GROUP BY AccountId];

        for(AggregateResult a : Result) {

            Ids.add((Id)a.get('AccountId'));

        }

        AcctList = [SELECT Name ,industry FROM Account WHERE Id IN : Ids];

        system.debug(AcctList);

        acctlist.sort();

        System.debug('Account List' + AcctList);

        for(Account a : AcctList) {

            IdAccount.put(a.Id,a);

        }

        System.debug('Ids and Accounts are ' + IdAccount);

        for(AggregateResult a : Result) {

            Account TempAcct = new Account();

            TempAcct = IdAccount.get((Id)(a.get('AccountId')));

            system.debug('Account Name is ' + TempAcct.Name);

            SummaryList.add(new WrapperAggregate(a, TempAcct.Name));

            SummaryList.add(new WrapperAggregate(a, TempAcct.Name));


        }

    }

}



   

   

     

         

           

           

       

   





Your Answer

Interviews

Parent Categories