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
Randombard2Randombard2 

Variable Does not exist second .get

For(AggregateResult ARCY:[SELECT SUM(Amount__c) mySum, COUNT(id) myCount, Account__c
      	FROM Finance__c where Account__c =:Ac[0].id AND 
                                 (Group__c = 'TOTAL' 
                                 	OR Group__c = 'Total') AND 
                                 	(Code__c Like '%YTD%'
                                 	 OR Code__c Like :'%'+LastYear+'%'
                                 	 OR Code__c Like :'%'+Year2+'%'
                                 	 OR Code__c Like :'%'+Year3+'%'
                                 	 OR Code__c Like :'%'+Year4+'%')
                                 	GROUP BY Account__c])
               myCount = double.valueOf(ARCY.get('myCount'));
              mySum = double.valueOf(ARCY.get('mySum'));
The secont .get failes no matter what way around they are, feel like I have forgotten a limitation on AggregateResult
 
HARSHIL U PARIKHHARSHIL U PARIKH
Wouldn't it be something like..
 
AggregateResult[] aggRes = [SELECT SUM(Amount__c) mySum, COUNT(id) myCount, Account__c
      	FROM Finance__c where Account__c =:Ac[0].id AND 
                                 (Group__c = 'TOTAL' 
                                 	OR Group__c = 'Total') AND 
                                 	(Code__c Like '%YTD%'
                                 	 OR Code__c Like :'%'+LastYear+'%'
                                 	 OR Code__c Like :'%'+Year2+'%'
                                 	 OR Code__c Like :'%'+Year3+'%'
                                 	 OR Code__c Like :'%'+Year4+'%')
                                 	GROUP BY Account__c];
Double myCountFinal = 0.00;
Double mySumFinal   = 0.00;

For(AggregateResult EveryAR : aggRes )

{
   Double count = (Double)EveryAR.get('myCount');
   Double sum   = (Double)EveryAr.get('mySum');
   myCountFinal += count;
   mySumFinal += sum;
}
   System.debug(myCountFinal);
   System.debug(mySumFinal);

Hope it helps!
Robin Mead 6Robin Mead 6
Hi, Thanks for this,

Can you help me understand why it works with the first .get but not the second?

So the following works fine:
For(AggregateResult ARCY:[SELECT SUM(Amount__c) mySum, COUNT(id) myCount, Account__c
      	FROM Finance__c where Account__c =:Ac[0].id AND 
                                 (Group__c = 'TOTAL' 
                                 	OR Group__c = 'Total') AND 
                                 	(Code__c Like '%YTD%'
                                 	 OR Code__c Like :'%'+LastYear+'%'
                                 	 OR Code__c Like :'%'+Year2+'%'
                                 	 OR Code__c Like :'%'+Year3+'%'
                                 	 OR Code__c Like :'%'+Year4+'%')
                                 	GROUP BY Account__c])
               myCount = double.valueOf(ARCY.get('myCount'));

 
Robin Mead 6Robin Mead 6
Turns out I was beeing foolish following works fine:
 
For(AggregateResult ARCY:[SELECT SUM(Amount__c) mySum, COUNT(id) myCount, Account__c
      	FROM Finance__c where Account__c =:Ac[0].id AND 
                                 (Group__c = 'TOTAL' 
                                 	OR Group__c = 'Total') AND 
                                 	(Code__c Like '%YTD%'
                                 	 OR Code__c Like :'%'+LastYear+'%'
                                 	 OR Code__c Like :'%'+Year2+'%'
                                 	 OR Code__c Like :'%'+Year3+'%'
                                 	 OR Code__c Like :'%'+Year4+'%')
                                 	GROUP BY Account__c])
{
               myCount = double.valueOf(ARCY.get('myCount'));
              mySum = double.valueOf(ARCY.get('mySum'));
}