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

get id from aggregateResult query

I have a SOQL query that is using SUM and Group By, but I need to also get the Id of a field that doesn't use an aggregate function nor is it used in the Group By clause.

select Field1__c, GroupByField__r.Name, SUM(MyField2__c) sumMyField2 from My_SObject__c GROUP BY GroupByField__r.Name

I need to include "Field1__c" in the query to get the ids from that field. Is it possible to include the field without putting it in the Group By clause or using an aggregate function on it?

Thanks for any help.
Best Answer chosen by bohemianguy100
Adnubis LLCAdnubis LLC
Aggregate Queries can not return IDs. When you do an aggregate query your results come to you in groupings and so each row returned can belong to multiple records with multiple IDs. When you do an Aggregate query you can either perform an Aggregate function on a field or Group it but you must do one of those two things.