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
Nikunj VNikunj V 

fetching result from AggregateResult : soql

i am trying to fetch fields from a aggregteresult but it is giving me error :Invalid field Time_Account_Mapping__c for SObject AggregateResult
 
Time__c tes = [select Time_Account_Mapping__c from Time__c order by LastModifiedById desc limit 1];
     
        AggregateResult obj = [Select Time_Account_Mapping__c,sum(Hours__c) from time__c group by Time_Account_Mapping__c having Time_Account_Mapping__c in (:tes.Time_Account_Mapping__c)];

	Account ppla = new Account();
        ppla.id = obj.Time_Account_Mapping__c;
        ppla.Total_Client_Hour__c=obj.sum(Hours__c);
    //    ppla.Total_Client_Hour__c = test.Unknown_Field__1;
        update ppla;
Help me out here.
Thanks
Best Answer chosen by Nikunj V
NikunjVadiNikunjVadi
i am posting from other account but this solved my issue:

  Account acc = new Account();
        acc.id= ((ID)obj.get('Time_Account_Mapping__c'));
        acc.Total_Client_Hour__c =((decimal)obj.get('sumofhour'))    ;
        update acc;

 

All Answers

Jim JamJim Jam
Aggregate result object is more or less the same as a map. Try obj.get('time_account_mapping__c') .
Nikunj VNikunj V
thanks for the help
tried it already giving me error : Method does not exist or incorrect signature: [List<AggregateResult>.get(String)

and i would like to know how can i use sum(hours__c) , because when i am trying to execute this query in workbench it is giving me result under unkown_field__1 section
 
Jim JamJim Jam
Try obj[0].get('time_account_mapping__c') .. for the first row, loop through the obj list to retrieve all rows. Create an alias for the Sum value in your query and retrieve it in much the same way. So.. select time_account_mapping__c, sum(hours__c) sumOfHours from time__c ... and then obj[0].get('sumOfHours')
Nikunj VNikunj V
obj only have one row . so i dont require any loop and i changed it to obj[0] still no sucess .
Error:
Variable does not exist: Time_Account_Mapping__c

do you have any link to fetch the variables from aggregateresult ? 
 
Katia HageKatia Hage
Check out this doc to see how to work with aggregate results with SOQL: http://www.salesforce.com/us/developer/docs/apexcode/Content/langCon_apex_SOQL_agg_fns.htm

You can get the result either by the alias name or the name 'expr<n>'.
NikunjVadiNikunjVadi
i am posting from other account but this solved my issue:

  Account acc = new Account();
        acc.id= ((ID)obj.get('Time_Account_Mapping__c'));
        acc.Total_Client_Hour__c =((decimal)obj.get('sumofhour'))    ;
        update acc;

 
This was selected as the best answer
K.G.K.G.
You probably need to cast the obj[0].get('time_account_mapping__c') to an ID -- (Id)obj[0].get('time_account_mapping__c') -- I had to do that just today.
K.G.K.G.
(That is, presuming time_account_mapping__c is a lookup or master-detail field)
Deepika1007Deepika1007
Refer below for Step By Step Implementation of Aggregate Result
https://deepikamatam.blogspot.com/2019/07/aggregate-result.html