+ Start a Discussion
SFManiSFMani 

Getting Error while adding AggregateList values to another List

Hello Board,

 

I am trying to add the values from the AggregateList to another list, but I get the following error:

 

Error: Controller Compile Error: Invalid initial expression type for field Parent, expecting: SOBJECT:Account (or single row query result of that type)

 

Here is my code:

 

List<AggregateResult> agr=[select id ID,Parent.id GrandParent, sum(Total_Service_Collections__c) totalserv from
                                    Account where id in:accIds group by id,Parent.id];
        List<Account> AccountList = new List<Account>(); 
                       
        for(AggregateResult a:agr)
        { 
           ID AId= string.valueOf(a.get('ID'));
           ID parent=string.valueOf(a.get('GrandParent'));
           
          Account acc=new Account(ID=AId,Parent=parent); // Error in This line
           acc.CountPick__c=double.valueOf(a.get('totalserv'));
          
           AccountList.add(acc);
         
          
        }

 

I would appreciate if someone can help me out on how to resolve the problem.

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
SFManiSFMani

I got the solution. I was using the wrong API name. It should have been ParentId rather than Parent.

All Answers

clouduserclouduser

can you try using the different field name like parent=parent1 because apex is case-insensitive.

SFManiSFMani

I have tried giving some other name, but still the error persists.

SFManiSFMani

I got the solution. I was using the wrong API name. It should have been ParentId rather than Parent.

This was selected as the best answer