Skip to main content
12 votes
Accepted

How to Deserialize AggregateResults?

I would have expected the serialization to work... A workaround is to use Map<String, Object> instead (as an AggregateResult is usually consumed much like a Map) which will serialize and can be ...
Keith C's user avatar
  • 137k
12 votes
Accepted

SOQL AggregateResult Query throws error: System.ListException: Row with null Id at index: 0

You need to alias some field to Id in order for the Map<Id, SObject>(List<SObject>) constructor to work. SELECT OpportunityLineItemId Id, MIN(ScheduleDate) Starts FROM ...
Adrian Larson's user avatar
  • 151k
11 votes

How to read field values from AggregateResult

You need to call get from AggregateResult, not List<AggregateResult>. Instead of ar.get('cnt'), you could for instance do ar[0].get('cnt').
Adrian Larson's user avatar
  • 151k
10 votes

Is there a way I can mock an AggregateResult?

Check edit history for my initial approach. After revisiting this question and with a bit more experience, I would pursue this path: public class Aggregate { public static List<Aggregate> ...
Adrian Larson's user avatar
  • 151k
8 votes

How to read field values from AggregateResult

ar is of type AggregateResult[] (also known as List<AggregateResult>). You are aliasing correctly, but you must call get() on a single AggregateResult. Reference: Working with SOQL Aggregate ...
David Reed's user avatar
  • 93.7k
7 votes
Accepted

What is more effecient , using SOQL AVG() or to query all records and calculate their average

Well, the easiest way to figure that out is to do some benchmarking. Adrian Larson's Apex Limits Profiler can help out with that, but I'm feeling a bit lazy, and this seems like a simple enough test. ...
Derek F's user avatar
  • 64.1k
7 votes
Accepted

What is the benefit of using Aggregate Query in Salesforce

Anywhere that an array of SObjects is queried - e.g. your Select Id from Contacts - those data rows are included in the response and so have to be transferred and consume a bit of heap. An aggregate ...
Keith C's user avatar
  • 137k
6 votes
Accepted

Possible to include getSObjectType() in for loop parameters?

AggregateResult can't be reliably created or modified. They're a construct for querying aggregate data from the database. Instead, consider using a Map<Id, Object>: public static Map<Id, ...
sfdcfox's user avatar
  • 502k
6 votes
Accepted

Invalid field count for AggregateResult:

You didn't alias the field. Either add a field alias (count), or get the first expression (expr0). List<AggregateResult> aggregates = [SELECT count(Id) count FROM ...]; // OR Decimal count = (...
Adrian Larson's user avatar
  • 151k
6 votes
Accepted

How to convert Date Time field value to Date value in SOQL query

Use the DAY_ONLY SOQL Function. Select Count(id) c , DAY_ONLY(HLA_Expiration_Date__c) exp from Hardware_Loan_Agreement__c where HLA_Expiration_Date__c>=:Date.today() and ...
Eric's user avatar
  • 54.4k
6 votes
Accepted

Passed Aggregate Results and assigning to values

you should access fields from AggregateResult in different way, using get method: String mappingAlias = (String) oli.get('PackageAdId__c'); refer to documentation
Oleksandr Berehovskyi's user avatar
6 votes
Accepted

Aggregated Query Too many query rows

The number of rows aggregated count against the 50,000 record limit. The "Rows" value you're seeing are the number of aggregate rows that were returned; this has a limit of 2,000 rows per query; if ...
sfdcfox's user avatar
  • 502k
6 votes

What is the benefit of using Aggregate Query in Salesforce

Salesforce uses some confusing terminology around this topic. For clarity... An aggregate query (as far as governor limits and error messages seem to go) is a parent-child subquery (a.k.a. a "...
Derek F's user avatar
  • 64.1k
5 votes
Accepted

get MIN date with Id from aggregate result query

In this specific case, it'd be a lot easier to order by date ascending and limit by 1: MyObject__c minDOBRecord = [SELECT Id FROM MyObject__c WHERE Id = :setIds ORDER BY DOB__c ASC LIMIT 1]; In real ...
sfdcfox's user avatar
  • 502k
5 votes

Is there a way I can mock an AggregateResult?

I so much wanted @AdrianLarson original Json.deserialize answer to be right as it is easy to fabricate but while it works at compile time, at run time, the deserialized AggregateResult is empty ( ...
cropredy's user avatar
  • 73.3k
5 votes
Accepted

How to get aggregate results from generic map?

There is a getSobjects method for this purpose in the base Sobject class. See the documentation Sobject Class for detailed information. In your case: List<Sobject> orders = sObjectMap.get('...
Keith C's user avatar
  • 137k
5 votes
Accepted

Compile Error: Method does not exist or incorrect signature void add(AggregateResult)

AggregateResult is not well-documented but it is an object, so you cannot add it to a list of some other object like GMBLASERP__Inventory_Movement_Cube__c. You must iterate through your aggregate ...
David Cheng's user avatar
  • 7,434
5 votes
Accepted

How to get Map<Id, List<Contact>> from Contact SOQL query GROUP BY AccountId

That type of construct is not supported. You would end up with a Map<Id, AggregateResult> instead. Using a GROUP BY changes the query result in to an AggregateResult object. You may either build ...
sfdcfox's user avatar
  • 502k
5 votes
Accepted

Select aggregate into Integer using Database.query()

Yes. You can query using Database.countQuery(query) String QueryString = 'SELECT count() FROM Account'; Integer countAccounts = Database.countQuery(QueryString);
Hemavantha Rajesh Varma Mudunu's user avatar
5 votes
Accepted

APEX Aggregate Result vs Repeat with Variable

Yes, there is a difference. You should use Aggregate Result. Using an <apex:variable> and incrementing it in a loop isn't supported behavior, strictly speaking, and you should avoid putting ...
Adrian Larson's user avatar
  • 151k
5 votes
Accepted

Aggregate SOQL Query Cannot Aggregate

It's not giving you a sum because your first grouping is on the Id of the Product_Matrix__c: totalSummaryMatrix = [ Select Id,Product__c, SUM(Total_Sale__c)totalSalesSum, SUM(Order_Recieved__c)...
David Reed's user avatar
  • 93.7k
5 votes

Error: BillingAddress does not support aggregate operator COUNT

You need to aggregate each field individually. Also use count(Id) in your HAVING clause, just like you have it in your SELECT clause. SELECT COUNT(Id), BillingStreet, BillingCity, BillingState, ...
Adrian Larson's user avatar
  • 151k
5 votes
Accepted

Apex AggregateResult in Managed Packages

We learned that namespaces is really needed in few scenarios, so its better to be consistent and safe than spending time with creation of version to test namespace issues. Product2.Product_Family__c ...
Raul's user avatar
  • 18.9k
5 votes
Accepted

Dynamically Finding the column names and values in AggregateResult

Your question isn't entirely clear (are you looking for field names, or the values of all fields?) In the end though, it doesn't matter which. Turns out you can still use getPopulatedFieldsAsMap() (...
Derek F's user avatar
  • 64.1k
5 votes
Accepted

Batch reachs the limit of 50000 records

The limit says: The start method can return upto 50 million records. And the execute method can then process those in chucks of 200 by default or max till 2000. In this method List<AggregateResult&...
Nagendra Singh's user avatar
4 votes

Can anything be done to improve the speed of a Salesforce Communities pages

I would recommend that you pinpoint the exact performance problem that you're encountering before assuming that the aggregate SOQL queries are the problem area. Salesforce has a chrome plugin that is ...
Mark Pond's user avatar
  • 23.1k
4 votes
Accepted

AggregateResult looping over empty query?

Your query clearly did return results. You have Order_Invoice_Item__c records whose Product__c is equal to null.
Adrian Larson's user avatar
  • 151k
4 votes
Accepted

Assign queried Decimal to a Decimal variable

I agree with the commenter above that said that you have some SoQL-In-Loop problems (and you DO want to get them fixed ASAP) but, in the meantime, you can get the value like this RebateFactor = (...
Sebastian Kessel's user avatar
4 votes
Accepted

Modify the results of an aggregate result directly

No. If you attempt to modify the AggregateResult, you'll get a runtime error. Here's an example from my dev org: AggregateResult[] ar = [select name name from account group by name]; System.debug(ar[0]...
sfdcfox's user avatar
  • 502k
4 votes

Can aggregate result query in salesforce be forced to return records even when count is zero

If there's no rows for a given grouping, it won't return an AggregateResult row. There has to be at least a single record to place into a grouping. It's entirely possible an AggregateResult query will ...
sfdcfox's user avatar
  • 502k

Only top scored, non community-wiki answers of a minimum length are eligible