Monday, 4 December 2017

AggregateResult in Salesforce Map

AggregateResult in Salesforce is used to find Sum, Min, Max and Avg. It is similar to Aggregate function in SQL.

Using Aggregate Result, we cannot fetch data. It is mainly used to find Sum, Min, Max and Avg.


Code:

List<Interaction__c> InteractionsList = [select id, name, Start_Date__c,
                   CreatedDate from Interaction__c ];

For( Interaction__c int1 : InteractionsList ){
            InteractioUniqueIds.add(int1.id);
 }

List<AggregateResult> IntExpenseList = [select Interaction__c, sum(Amount__c)
                                      from Expense__c
                                     where Spend_Type__c = 'Honoraria' AND Interaction__c
                                     IN: InteractioUniqueIds group by Interaction__c ];



Map<id,Decimal > ExpAmtMap = new Map<id,Decimal >();
     
for(AggregateResult exp: IntExpenseList){
    ExpAmtMap.put((Id)exp.get('Interaction__c'), (Decimal)exp.get('expr0'));
}

wrp is wrapper class:
wrp.ExpensesAmount = ExpAmtMap.get(Interaction.id);

No comments:

Post a Comment