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
JoshTonksJoshTonks 

Illegal assignment from List<AggregateResult> to List<Contact>

Hi Folk
Im looking for a little help, I was just putting something together for cost saved which we record on individual contacts
List<Contact> CostSaved;
        public List<Contact> getCostSaved() {
               	CostSaved = [SELECT Account_ID__c, SUM(Savings_per_week__c)
                			 FROM Contact, Contact.Account
                			 WHERE Account.Name = 'ram test'
                			 GROUP BY Account_ID__c];
            	return CostSaved;
        }
I am getting Illegal assignment from List<AggregateResult> to List<Contact> im obviously missing something I just cant figure out what.
 
Best Answer chosen by JoshTonks
Rajesh Varma MudunuriRajesh Varma Mudunuri
Hi Tonks,
Your'e getting the error because you are using GROUP By which return List<AggregateResult > rather than LIST<SOBJECT>. Please go through this below link.
http://sfdc.arrowpointe.com/2010/02/10/using-aggregate-functions/
Thanks.

All Answers

Rajesh Varma MudunuriRajesh Varma Mudunuri
Hi Tonks,
Your'e getting the error because you are using GROUP By which return List<AggregateResult > rather than LIST<SOBJECT>. Please go through this below link.
http://sfdc.arrowpointe.com/2010/02/10/using-aggregate-functions/
Thanks.
This was selected as the best answer
johnseelewis42johnseelewis42
Tonks,

The groupby would be meaningless in a list anyway, unless you just wanted it sorted.  In that case, try SORT BY Account_ID__c

That said, I think you might be better off returning a list of Accounts with Contacts in a subquery. Try this:

Select id, name, (SELECT Account_ID__c, Savings_per_week__c FROM Contacts )FROM Account WHERE Name = 'ram test' 

You'd still have to add up the savings manually.
 
JoshTonksJoshTonks
John,

Sorry i hadn't realised id copied pasted over an older version, I actually meant to remove the where Account.Name = 'ram test' and replace it with Account.Type = : Customer