+ Start a Discussion
Raghavendra M 13Raghavendra M 13 

number of opportunities sold by month using lightning component?


Hi
Month field :
Data Type: Formula  
CASE(MONTH(CloseDate), 1, "January", 2, "February", 3, "March", 4, "April", 5, "May", 6, "June", 7, "July", 8, "August", 9, "September", 10, "October", 11, "November", 12, "December", "None")

Apex Class:
public class NoofOpportunitiesbymonth{
    @auraenabled
    public static List<MonthWrapper> oppMonth(){
        List<MonthWrapper> wrapper = new List<MonthWrapper>() ; 
        AggregateResult[] Result = [SELECT Count(Id) Total ,
                                    Month__c Months FROM Opportunity GROUP BY Month__c limit 10];
        for (AggregateResult ar : Result) {
            wrapper.add(new MonthWrapper((String)ar.get('Months') ,(Integer)ar.get('Total')));
        }
        
        return wrapper ;
        
    }
    
    public class MonthWrapper{
        @auraenabled
        public String month{get;set;}
        @auraenabled
        public Integer count {get;set;}
        public MonthWrapper(String month, Integer count){
            this.month= month;
            this.count = count; 
            
        }
        
    }
    
}
Component:
<aura:component controller="NoofOpportunitiesbymonth" implements="force:appHostable" >
    
    <aura:attribute name="opportunities" type="List[]"/>
    <aura:handler name="init" value="{!this}" action="{!c.myAction}"/>
    
    <table border="1" class="table">
        <tr>
            <th>Month&nbsp;&nbsp;</th> 
            <th>Number of Opportunities&nbsp;</th>
        </tr><br/>
        <aura:iteration items="{!v.opportunities}" var="cust">
            <tr> 
                <td>{!cust.month}</td>
                <td>{!cust.count}</td>       
            </tr>
        </aura:iteration>
    </table>
    
</aura:component>
Controller:
({
    myAction : function(component, event, helper) {
        var action =component.get("c.oppMonth");
        console.log('The action value is: '+action);
         action.setCallback(this, function(a){ 
             
            component.set("v.opportunities", a.getReturnValue());
            console.log('The opps are :'+JSON.stringify(a.getReturnValue()));
          
        });
        $A.enqueueAction(action);
    }
})

Thanks