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
KitagawaSan85KitagawaSan85 

Aggregate Function Type

I am trying to create a visualforce page that will show what % of a sales reps total funnel is represented by a given opportunity. 

 

I have been working on the controller for this, but keep getting an error: 

Error: Compile Error: Return value must be of type: Decimal at line 14 column 9

 

public class OpportunityExt {

    private final Opportunity opp;
    
    public OpportunityExt(ApexPages.StandardController stdController) {
        this.opp = (Opportunity)stdController.getRecord();
    }

    public integer getOpenTasks() {
        return [SELECT count() FROM task WHERE whatid = :opp.id and IsClosed=false];
    } 
    
    public decimal getFunnel() {
        return [SELECT Sum(Amount) FROM opportunity WHERE ownerid = :opp.ownerid];
    }
}

 I have also tried Integer, but I was under the impression that currency fields were stored as decimal... 

 

Has anybody run into something like this before? 

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

Aggregate Results are objects and you need to "get" the fields and cast the properly. The below does not account for an empty result

 

An example:

 

public decimal getFunnel() {
        AggregateResult ar = [SELECT Sum(Amount)s FROM opportunity WHERE ownerid = :opp.ownerid];
return (decimal)ar.get('s'); }

All Answers

SFDC-SDSFDC-SD

      

      Pleaes try to use the below code and let me know if that works. FYI... didn't test this out, but just  thought AggregateResults  

     would work.

 

      public decimal getFunnel() {

                AggregateResult groupedResults = [SELECT Sum(Amount) FROM opportunity WHERE ownerid = :opp.ownerid];

                return double.valueof(groupedResults);

      }

     
       

KitagawaSan85KitagawaSan85

Thanks! 

 

Still getting "Content cannot be displayed: Invalid double: [AggregateResult (expr0:25118.0)]" in the visualforce page though...

Starz26Starz26

Aggregate Results are objects and you need to "get" the fields and cast the properly. The below does not account for an empty result

 

An example:

 

public decimal getFunnel() {
        AggregateResult ar = [SELECT Sum(Amount)s FROM opportunity WHERE ownerid = :opp.ownerid];
return (decimal)ar.get('s'); }
This was selected as the best answer
KitagawaSan85KitagawaSan85

Thank you so much! 

 

Would it then be possible to perform another math function to that? 

 

I am trying to reference 'Funnel' later in the code but it is not finding it. 

SFDC-SDSFDC-SD

  return (decimal)ar.get('expr0'));

 

Replace last statement with this...