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
AzusfdcAzusfdc 

The requirement is for every account need a count of contacts and I have tried below is the code and getting error for AccountName String ?

User-added image
Public Class AccountRelatedContactCount
{
    Public List<Wrapper> WrapperList{Set;get;}
    
    List<AggregateResult> arlist=new List<AggregateResult>();
    
    Public AccountRelatedContactCount(){
    arlist=[select Account.Name,count(AccountID) Total from contact group by Account.Name];
    }
    
    Public List<Wrapper> getResult()
    {
    List<Wrapper> WrapperList=new List<Wrapper>();
    for(AggregateResult ar:arlist)
    {
    Wrapper wrplist=new Wrapper(ar);
    WrapperList.add(wrplist);
    }
    return WrapperList;
    }
    
    Public Class Wrapper
    {
    Public String AccountName{Set;get;}
    Public Integer Total{Set;get;}
    Public Wrapper(AggregateResult ar){
    AccountName=(String)ar.get('Account.Name');
    Total=(Integer)ar.get('Total');
    }
    }
}
=============================================
<apex:page controller="AccountRelatedContactCount">
    <apex:pageBlock >
    <apex:pageBlockTable value="{!Result}" var="r">
    <apex:column value="{!r.AccountName}" headervalue="Account Name"/>
    <apex:column value="{!r.Total}" headervalue="Number of Contacts"/>
    </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>
Best Answer chosen by Azusfdc
AzusfdcAzusfdc
Thank You very much James Loghry its awesome with the help of you i am done !!!

All Answers

James LoghryJames Loghry
AggregateResults behave differently than regular sObjects.  Without aliasing, you can reference the fields using an ordered expr0, expr1, expr2 syntax.  For instance, in Apex, I could iterate through your list and use the following:

for(AggregateResult ar : arlist){
    System.debug(ar.get('expr0'));
}

This will print the Account.Name field to the debug log.  On the otherhand, I could do the same thing with aliasing (by adding some text after my field in my SOQL query:

 arlist=[select Account.Name acctName,count(AccountID) Total from contact group by Account.Name];

for(AggregateResult ar : arlist){
    System.debug(ar.get('acctName');
}

Below, I just took your class and added some aliasing to it (aliasing Account.Name to accountName)

 
Public Class AccountRelatedContactCount
{
    Public List<Wrapper> WrapperList{Set;get;}
    
    List<AggregateResult> arlist=new List<AggregateResult>();
    
    Public AccountRelatedContactCount(){
    arlist=[select Account.Name accountName,count(AccountID) Total from contact group by Account.Name];
    }
    
    Public List<Wrapper> getResult()
    {
    List<Wrapper> WrapperList=new List<Wrapper>();
    for(AggregateResult ar:arlist)
    {
    Wrapper wrplist=new Wrapper(ar);
    WrapperList.add(wrplist);
    }
    return WrapperList;
    }

    Public Class Wrapper
    {
        Public String AccountName{Set;get;}
        Public Integer Total{Set;get;}
        Public Wrapper(AggregateResult ar){
        AccountName=(String)ar.get('accountName');
        Total=(Integer)ar.get('Total');
    }
}


Your VF page should still be okay, even with this change.

Hope that helps.

 
Deepak GulianDeepak Gulian
AccountName=(String)ar.get('Name');
Try this!
AzusfdcAzusfdc
Thank You very much James Loghry its awesome with the help of you i am done !!!
This was selected as the best answer