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

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, GrandParent, sum(Total_Service_Collections__c) totalserv from
                                    Account where id in:accIds group by 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


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



Best Answer chosen by Admin (Salesforce Developers) 
Jia HuJia Hu
Account acc=new Account(ID=AId,Parentid =parent);

All Answers

Jia HuJia Hu
Account acc=new Account(ID=AId,Parentid =parent);
This was selected as the best answer

The Field name is 'Parent' which is a lookup field in the Account Object. So I can't use Parentid.

Jia HuJia Hu
If you can use, it means it is a Standard field.
You should use API name, it is ParentId, not the label name ,...


Jia Hu is correct. I'd just like to add that Parent is of the type SObject (of an Account); it is valid, but it is not a SObjectField; it contains an entire record (if queried) instead of just an ID value. Parent can be used when you do this:


SELECT Id,Parent.Id,Parent.Name...

When you do this, Parent.Id is the parent account's ID, and Parent.Name would be the parent account's name. This is normally unnecessary, especially given your use case, so Jia Hu's answer is the most correct for this case.


Thanks for the clarification, it did work.