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
Malli GMalli G 

aggregatefunction i have an error please rectify this

I wrote this code
 i am getting error Attempts to de reference to null object


public class sample2 {

    public List<AggregateResult> Result {get;set;}    
    public List<Summary> SummaryList {get;set;}
    public List<Account> AcctList;
    public Map<Id, Account> IdAccount;
    List<Id> Ids;
    
    public void show() {
        SummaryList = new List<Summary>();
        Result = new List<AggregateResult>();
        Ids = new List<Id>();
        AcctList = new List<Account>();
        IdAccount = new Map<Id, Account>();
        
        Result = [SELECT Count(Id) Total , AccountId FROM Contact GROUP BY AccountId];               
        
        for(AggregateResult a : Result) {   
            Ids.add((Id)a.get('AccountId'));
        }
        
        AcctList = [SELECT Name FROM Account WHERE Id IN : Ids];
        
        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.acctName);
            SummaryList.add(new summary(a));                
        }    
    }
    
    public class Summary {
        public Integer Total {get;set;}
        public string AcctName {get;set;}
        
        public Summary(AggregateResult a, string AccountName) {
            Total =  (Integer)a.get('Total');
            AcctName = AccountName;
        }
    }

}
sslodhi87sslodhi87
Hi Malli,

I am not able to find the cause of error you are facing by seening your code but updated you code as mentioned below.
 
public class sample2 
{
	public List<Summary> SummaryList {get;set;}
   
    public void show() 
	{
        SummaryList = new List<Summary>();
    
        for(AggregateResult objAR : [SELECT Count(Id) total , AccountId accid, Account.Name accname FROM Contact GROUP BY AccountId, Account.Name])
		{
			SummaryList.add(new Summary(objAR.get('total'), objAR.get('accname')));
		} 
    }
    
    public class Summary {
        public Integer Total {get;set;}
        public string AcctName {get;set;}
        
        public Summary(Integer total, string AccountName) {
            this.Total =  total;
            this.AcctName = AccountName;
        }
    }
}
Please copy paste this code and check if this works for you.

Thanks
 
Malli GMalli G
hai 

still iam getting error constructor not defined on line no " 11";
Malli GMalli G
Constructor not defined: [sample2.Summary].<Constructor>(Object, Object)
this error was comes to me
sslodhi87sslodhi87
public class sample2 
{
	public List<Summary> SummaryList {get;set;}
   
    public void show() 
	{
        SummaryList = new List<Summary>();
    
        for(AggregateResult objAR : [SELECT Count(Id) total , AccountId accid, Account.Name accname FROM Contact GROUP BY AccountId, Account.Name])
		{
			SummaryList.add(new Summary((Integer)objAR.get('total'), objAR.get('accname') != null ? (String)objAR.get('accname') : ''));
		} 
    }
    
    public class Summary {
        public Integer Total {get;set;}
        public string AcctName {get;set;}
        
        public Summary(Integer total, string AccountName) {
            this.Total =  total;
            this.AcctName = AccountName;
        }
    }
}

Check this