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
Andrew Lewis 9Andrew Lewis 9 

AggregateResult issue

Hi all,

I seem to have hit a snag with my work. I have used this technique before but for some reason I am gettting this error, Method does not exist or incorrect signature: [List<AggregateResult>].get(String). 

List<Contact> l_ConToUpdate = [SELECT ID,INV_YTD_Entertainment_Spend__c,INV_PYTD_Entertainment_Spend__c FROM Contact where Id in :Set_Id];
        For (Contact con:l_ConToUpdate){
            List<AggregateResult> l_EventToCountCY = [SELECT SUM(INV_Cost__c) Total FROM Event WHERE WhoId = :con.Id AND CALENDAR_YEAR(ENDDATETIME) = :System.Today().year() AND INV_Type__c = 'Entertainment'];
            CY_Sum = Double.valueOf(l_EventToCountCY.get('Total')); <-- error on this line

Any hints?
Thanks!

Akhil ReddyAkhil Reddy
I think 
Double.valueOf(l_EventToCountCY.get('Total'))

Should be
 
Double.valueOf(l_EventToCountCY[0].get('Total'))

since it is a  list and you are not iterating it for a perticula value.
rajat Maheshwari 6rajat Maheshwari 6
Hi Andrew,

please use below code snippet, which will help you definitely :)
 
List<Contact> l_ConToUpdate = [SELECT ID,INV_YTD_Entertainment_Spend__c,INV_PYTD_Entertainment_Spend__c FROM Contact where Id in :Set_Id];

    For (Contact con:l_ConToUpdate){
        
          for(AggregateResult agg : [SELECT WhoId, SUM(INV_Cost__c) Total
            FROM Event WHERE WhoId = :con.Id AND CALENDAR_YEAR(ENDDATETIME) = :System.Today().year() AND INV_Type__c = 'Entertainment' group by WhoId])
            {
                
            CY_Sum = (Double)agg.get('Total'); 
            }
   }

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com