[SalesForce] Returning Key-value pair from a Map into a lightning component

I have a aggregate list which is returned only summary of 6 values. I want them to be displayed as percentage vales in a tables with 2 columns. First column is for the description of the summary value (Sting in the Map) and the second column is for the summary value (Value in the Map). This is my code. but I don't know how to return them into lightning component as a table. There are only 4 values to be iterated.

public class Wk_TraineePerformanceAuraController {
    @AuraEnabled
    public static List<Wk_PerformanceQueryResults> getTraineeProgress(){
        //Get looged in user details
        User loggedInUser = [SELECT Id,
                             ContactId,
                             Contact.AccountId,
                             Contact.Company_Contact__c
                             FROM User
                             WHERE ID=: UserInfo.getUserId() LIMIT 1];
        System.debug('=loggedInUser===='+loggedInUser);

        List<AggregateResult> agrResults=
            [Select 
             Sum(Achieved_Credits_to_Rpting_Yr_End__c) achivedCredit,
             Sum(PC_Credits_Total__c) pcCredit,
             Sum(Programme_Credits__c) progCredits,
             Count(X10_Credit_Minimum_No__c) tenCreditMin,
             Count(X1_Credit_Minimum_No__c) oneCreditMin,
             Count(Enrolled_in_Rpt_Year_No__c) enrRptYr
             FROM  Funding_Cycles_TA__c 
             WHERE Enrolled_in_Rpting_Yr__c=True  
             AND (Training_Agreement__r.Funding_Source__c ='IT - Industry Training' 
                  OR Training_Agreement__r.Funding_Source__c ='UF - Un-funded')
             AND Reporting_Year__c =THIS_YEAR
             AND Training_Agreement__r.Company_Id__c ='00130000010vWo6'];
 Map<String,decimal> agMap = new Map<String,decimal>();

        for(AggregateResult ar: agrResults){
            agMap.Put(('Progress'),(Decimal)ar.get('achivedCredit')*100/(Decimal)ar.get('progCredits'));
            agMap.Put(('Completions'),(Decimal)ar.get('pcCredit')*100/(Decimal)ar.get('progCredits'));
            agMap.Put(('Active'),(Decimal)ar.get('oneCreditMin')*100/(Decimal)ar.get('enrRptYr'));
            agMap.Put(('Progressive'),(Decimal)ar.get('tenCreditMin')*100/(Decimal)ar.get('enrRptYr'));
        }
}
}

I know how to code the component if I return the data as a table.

Thanks

Best Answer

Use following code for your requirement

<aura:component controller="Wk_TraineePerformanceAuraController">
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="myMap" type="Map" />
{!v.myMap.key1} 
{!v.myMap.key2}<br/>

({ doInit: function(component, event, helper) {

   var action = component.get('c.getTraineeProgress');

    action.setCallback(this,function(response){           
        component.set('v.myMap',response.getReturnValue());
    });
    $A.enqueueAction(action);
 }

})

public class Wk_TraineePerformanceAuraController { @AuraEnabled public static Map getTraineeProgress(){ //Get looged in user details User loggedInUser = [SELECT Id, ContactId, Contact.AccountId, Contact.Company_Contact__c FROM User WHERE ID=: UserInfo.getUserId() LIMIT 1]; System.debug('=loggedInUser===='+loggedInUser);

    List<AggregateResult> agrResults=
        [Select 
         Sum(Achieved_Credits_to_Rpting_Yr_End__c) achivedCredit,
         Sum(PC_Credits_Total__c) pcCredit,
         Sum(Programme_Credits__c) progCredits,
         Count(X10_Credit_Minimum_No__c) tenCreditMin,
         Count(X1_Credit_Minimum_No__c) oneCreditMin,
         Count(Enrolled_in_Rpt_Year_No__c) enrRptYr
         FROM  Funding_Cycles_TA__c 
         WHERE Enrolled_in_Rpting_Yr__c=True  
         AND (Training_Agreement__r.Funding_Source__c ='IT - Industry Training' 
              OR Training_Agreement__r.Funding_Source__c ='UF - Un-funded')
         AND Reporting_Year__c =THIS_YEAR
         AND Training_Agreement__r.Company_Id__c ='00130000010vWo6'];
    Map<String,decimal> agMap = new Map<String,decimal>();

    for(AggregateResult ar: agrResults){
        agMap.Put(('Progress'),(Decimal)ar.get('achivedCredit')*100/(Decimal)ar.get('progCredits'));
        agMap.Put(('Completions'),(Decimal)ar.get('pcCredit')*100/(Decimal)ar.get('progCredits'));
        agMap.Put(('Active'),(Decimal)ar.get('oneCreditMin')*100/(Decimal)ar.get('enrRptYr'));
        agMap.Put(('Progressive'),(Decimal)ar.get('tenCreditMin')*100/(Decimal)ar.get('enrRptYr'));
    }
    return agmap;
}

}

Thank you, Amol Salve Salesforce Developer

Related Topic