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
John Angerami 16John Angerami 16 

Apex Data from Full Year to Fiscal Quarter

I was able to find code for a Guage report that I can put on a VisualForce Page but it is capturing Full Year and I need it to capture just Quarter.
Any suggestions?
Thanks!
 
public class GaugeChartController {
    public String userId {get;set;}
    
    public GaugeChartController(ApexPages.StandardController controller){
        userId=userinfo.getuserid(); 
    }
    
    public List<gaugeData> getData() {
          Integer TotalOpptys = 0;
          Integer TotalAmount = 0;
          Integer thisMonth = date.Today().month();

          AggregateResult ClosedWonOpptys = [select SUM(Amount) totalRevenue, CALENDAR_MONTH(CloseDate) theMonth, COUNT(Name) numOpps
                                   from Opportunity
                                   where OwnerId =: userID
                                   and StageName = 'Closed Won' 
                                   and CALENDAR_MONTH(CloseDate) =: thisMonth 
                                   GROUP BY CALENDAR_MONTH(CloseDate) LIMIT 1];
          
          List<gaugeData> data = new List<gaugeData>();
          data.add(new gaugeData(Integer.valueOf(ClosedWonOpptys.get('numOpps')) + ' Opptys', Integer.valueOf(ClosedWonOpptys.get('totalRevenue'))));
          return data;
     }

    public class gaugeData {
        public String name { get; set; }
        public Integer size { get; set; }

        public gaugeData(String name, Integer data) {
            this.name = name;
            this.size = data;
        }
    }



 
Best Answer chosen by John Angerami 16
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Updated the query to get only those opportunities where CloseDate in within start and end of this quarter.
public class GaugeChartController {
    public String userId {get;set;}
    
    public GaugeChartController(ApexPages.StandardController controller){
        userId=userinfo.getuserid(); 
    }
    
    public List<gaugeData> getData() {
          Integer TotalOpptys = 0;
          Integer TotalAmount = 0;
          Integer thisMonth = date.Today().month();
          Period periodRecord = [Select EndDate, StartDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER];
          Date endDate = periodRecord.EndDate;
          Date startDate = periodRecord.StartDate;

          AggregateResult ClosedWonOpptys = [select SUM(Amount) totalRevenue, CALENDAR_MONTH(CloseDate) theMonth, COUNT(Name) numOpps
                                   from Opportunity
                                   where OwnerId =: userID
                                   and StageName = 'Closed Won' 
                                   and CloseDate<=:EndDate and CloseDate>=:StartDate
                                   GROUP BY CALENDAR_MONTH(CloseDate) LIMIT 1];
          
          List<gaugeData> data = new List<gaugeData>();
          data.add(new gaugeData(Integer.valueOf(ClosedWonOpptys.get('numOpps')) + ' Opptys', Integer.valueOf(ClosedWonOpptys.get('totalRevenue'))));
          return data;
     }

    public class gaugeData {
        public String name { get; set; }
        public Integer size { get; set; }

        public gaugeData(String name, Integer data) {
            this.name = name;
            this.size = data;
        }
    }
 }

Please mark it as Best Answer if it helps.

All Answers

SalesFORCE_enFORCErSalesFORCE_enFORCEr
Can you share the vf code as well?
John Angerami 16John Angerami 16
<apex:page standardController="Opportunity" extensions="GaugeChartController">
<apex:outputPanel style="padding-left: 3em;"  >
    <div class="menuButton" id="Actions"><apex:outputPanel layout="none">
        <div class="menuButtonButton" id="ActionsButton"><span class="menuButtonLabel" id="ActionsLabel">Actions</span></div>
 
        <div class="menuButtonMenu" id="ActionsMenu"><apex:outputPanel layout="none">
            <apex:outputLink value="https://c.cs81.visual.force.com/apex/Sales_Rep_DB_QTR">Quarter</apex:outputLink>
            <apex:outputLink value="https://c.cs81.visual.force.com/apex/Sales_Rep_DB_Year">Year</apex:outputLink>
        </apex:outputPanel></div>
    </apex:outputPanel></div>
</apex:outputPanel>
 
<script type="text/javascript">new MenuButton('Actions', false);</script>

<apex:form >
<apex:pageBlock >
    <script>
            myChart.on('beforeconfig', function(config) {
            config.axes[0].margin=-20; 
        });
    </script>
    <apex:pageBlockSection title="My Won Opportunities - Full Year" columns="2" collapsible="true">
    <apex:chart name="Sales v Goal" height="300" width="400" animate="true" data="{!data}">
        <apex:axis type="Gauge" position="gauge" title="Closed Won Opportunities -Amount"  minimum="0" maximum="30000" steps="5"/>
        <apex:gaugeSeries dataField="size" donut="40" colorSet="#78c953,#ddd"/>
    </apex:chart>
        <apex:chart name="ARR v Goal" height="300" width="400" animate="true" data="{!data}">
        <apex:axis type="Gauge" position="gauge" title="Closed Won Opportunities - ARR"  minimum="0" maximum="25000" steps="5"/>
        <apex:gaugeSeries dataField="size" donut="40" colorSet="#78c953,#ddd"/>
    </apex:chart>
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

 
SalesFORCE_enFORCErSalesFORCE_enFORCEr
As per your query, I can see opportunities where close date is in this month. I don't see any opportunities other than this.
John Angerami 16John Angerami 16
How do I change it to 'this quarter' from this month?
SalesFORCE_enFORCErSalesFORCE_enFORCEr
Updated the query to get only those opportunities where CloseDate in within start and end of this quarter.
public class GaugeChartController {
    public String userId {get;set;}
    
    public GaugeChartController(ApexPages.StandardController controller){
        userId=userinfo.getuserid(); 
    }
    
    public List<gaugeData> getData() {
          Integer TotalOpptys = 0;
          Integer TotalAmount = 0;
          Integer thisMonth = date.Today().month();
          Period periodRecord = [Select EndDate, StartDate From Period Where type = 'Quarter' and StartDate = THIS_FISCAL_QUARTER];
          Date endDate = periodRecord.EndDate;
          Date startDate = periodRecord.StartDate;

          AggregateResult ClosedWonOpptys = [select SUM(Amount) totalRevenue, CALENDAR_MONTH(CloseDate) theMonth, COUNT(Name) numOpps
                                   from Opportunity
                                   where OwnerId =: userID
                                   and StageName = 'Closed Won' 
                                   and CloseDate<=:EndDate and CloseDate>=:StartDate
                                   GROUP BY CALENDAR_MONTH(CloseDate) LIMIT 1];
          
          List<gaugeData> data = new List<gaugeData>();
          data.add(new gaugeData(Integer.valueOf(ClosedWonOpptys.get('numOpps')) + ' Opptys', Integer.valueOf(ClosedWonOpptys.get('totalRevenue'))));
          return data;
     }

    public class gaugeData {
        public String name { get; set; }
        public Integer size { get; set; }

        public gaugeData(String name, Integer data) {
            this.name = name;
            this.size = data;
        }
    }
 }

Please mark it as Best Answer if it helps.
This was selected as the best answer