You need to sign in to do that
Don't have an account?
Azusfdc
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>
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 ?
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>
All Answers
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)
Your VF page should still be okay, even with this change.
Hope that helps.
Try this!