I need to fetch the AggregateResult 'sett' for each Opportunity in a loop, from a map and convert them into decimals.

I've seen Ariel's solution on this post but when I use it, it causes an error

variable does not exist: decimal

And I'm not sure what the alternative is?

   Map<Id,AggregateResult> oppMRFSett = new Map <Id,AggregateResult>
       ([SELECT Opportunity__c Id, SUM(Monthly_Settlement1__c) sett
           FROM Monthly_Revenue_Forecast__c 
          WHERE Id IN :newMRFs
          GROUP BY Opportunity__c]);       

    for(Opportunity o : upOpps){

        Decimal monthlySett = o.Annual_Settlement_Value__c / forecastDuration.get(o.Id);
        Decimal rmningSett = monthlySett - decimal.valueOf(oppMRFSett.get('sett'))  + '';


When I adopt Eric's answer (below)

    Map<Id,AggregateResult> oppMRFSett = new Map <Id,AggregateResult>();
    oppMRFSett.put('sett', [SELECT Opportunity__c, SUM(Monthly_Settlement1__c) sett
           FROM Monthly_Revenue_Forecast__c 
          WHERE Id IN :newMRFs
          GROUP BY Opportunity__c]);   

    for(Opportunity o : upOpps){

          Decimal rmningSett = monthlySett - (decimal)oppMRFSett.get({o.Id}).get('sett');

It causes an error

expecting a right parentheses, found '{'

  • Where does upOpps come from? Is there code missing? – crmprogdev Jun 20 '15 at 19:40
  • @crmprogdev apologies, I missed your comment, it's a list of Opportunities, there's a lot more code but I've extracted this piece for clarity. Your answer did stop any errors by the way, did you delete it because it didn't include a way to fetch the AggregateResult from the map, using the Opportunity Id as the key? – Alex S Jun 20 '15 at 20:00
  • You need to replace {Id} with the actual ID of the record. You Map is using 'sett' but that is not a valid ID. so it will not compile either. – Eric Jun 20 '15 at 20:10
  • 1
    remove the { and } they were just there to indicate a placeholder. In addition you map key is not valid above when you used 'sett' as the map key. – Eric Jun 20 '15 at 20:11
  • 1
    @AlexS in your coder the Key is an ID you are using a string oppMRFSett.put('sett',xxxx) This is not a valid ID – Eric Jun 20 '15 at 22:22

Your map is getting the Aggregate result not the actual sett value

You need to change

Decimal rmningSett = monthlySett - decimal.valueOf(oppMRFSett.get('sett'))  + '';


Decimal rmningSett = monthlySett - (decimal)oppMRFSett.get({IDOFRECORD}).get('sett')

You should add some checks to ensure it is not null as well.

As an example, this works just fine in dev console:

Map<String,AggregateResult> tmp = New Map<String, AggregateResult>();
tmp.put('A',[Select Count(ID)cnt From Account]);
//The next line throws an error Variable does not exist Decimal
//Decimal d = 100 - decimal.valueOf(tmp.get('A').get('cnt'));
//the next line works fine
Decimal d = 100 - (decimal)tmp.get('A').get('cnt');

decimal.valueOf() can not accept an Object which, I think, is why it throws the error variable does not exist decimal. Although the error IMHO should be method does not exist decimal.valueOf(Object)

  • 1
    Thanks but that causes an error 'Incompatible types since an instance of AggregateResult is never an instance of Decimal.' – Alex S Jun 20 '15 at 19:35
  • Thats what you should be getting from the code in your original post not what I suggested. Updating with another way – Eric Jun 20 '15 at 19:36
  • So for both options, I get 'expecting a right parentheses, found '{'. Does the Id Of Record need to be the Opportunity Id (in the map) or the Monthly_Revenue_Forecast__c Id? – Alex S Jun 20 '15 at 19:40
  • Yes the {IDOFRECORD} was a placeholderer for you to put in the appropriate id – Eric Jun 20 '15 at 19:41
  • I'm still getting the "expecting a right parentheses error".. – Alex S Jun 20 '15 at 19:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.