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.
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.
All Answers
The Field name is 'Parent' which is a lookup field in the Account Object. So I can't use Parentid.
You should use API name, it is ParentId, not the label name ,...
doc:
http://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_account.htm
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:
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.
Cheers