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
sai babu 1sai babu 1 

AggregateResult[] groupedResults = [SELECT AVG(Amount)aver FROM Opportunity]; Object avgAmount = groupedResults[0].get('aver');

AggregateResult[] groupedResults = [SELECT AVG(Amount)aver FROM Opportunity]; Object avgAmount = groupedResults[0].get('aver');
in this code why aver from groupedResult is not accessed directly as
avgAmount = groupedResult.get('aver');

and what is the purpose of casting aggregateresult values.
Best Answer chosen by sai babu 1
Alisha Mehta 9Alisha Mehta 9
Hi,

AggregateResult object contains the results returned by SOQL. You have used AggregateResult[] as an array type, so the SOQL returns results as a list and which is why you need to access it as groupedResults[0].get('aver').
If you want to access it using this - "groupedResult.get('aver')", 
try - 
AggregateResult groupedResults =   [SELECT AVG(Amount)aver FROM Opportunity];
Object avgAmount = groupedResults.get('aver');

All Answers

Alisha Mehta 9Alisha Mehta 9
Hi,

AggregateResult object contains the results returned by SOQL. You have used AggregateResult[] as an array type, so the SOQL returns results as a list and which is why you need to access it as groupedResults[0].get('aver').
If you want to access it using this - "groupedResult.get('aver')", 
try - 
AggregateResult groupedResults =   [SELECT AVG(Amount)aver FROM Opportunity];
Object avgAmount = groupedResults.get('aver');
This was selected as the best answer
sai babu 1sai babu 1
thank you