0

I have a object called Business transactions which is related Account(Planner). I need to calculate the total of one field called commissions for each Account and show them in total. I'm able to calculate the total for all the planners in that org, but I also want to have the totals for individual accounts.

For example I have 10 accounts ( planner) who have many business transcations related to them each business transactions would have commission on that which is a field. I want to calculate commission for each Account as well as total for all the accounts. I'm able to calculate total for all the accounts(planner) not for individual.

Here is my code snippet.

set <String> AccountID = new Set <String>();
Set<Id> setRecordType = new Set <Id>();
setRecordType.add(Schema.SObjectType.Account.getRecordTypeInfosByName().get('Planner').getRecordTypeId());
System.debug('Set record by ID : '+setRecordType); 

for(Account Acc : [SELECT Id from Account WHERE RecordTypeId IN: setRecordType]){
    AccountID.add(acc.Id);
}
System.debug('Acc ID : '+AccountID);
Map<String,Decimal> MyBalance = New Map<String,Decimal>();
Decimal balTotal = 0;
String CurrentMonthDate = '2019-11-30';
for(AggregateResult ar : [SELECT sum(Commission__c)tot, Business__c 
                                  FROM Business_Transactions__c 
                                  WHERE  Date__c <:Date.valueOf(CurrentMonthDate) AND Planner__c IN:AccountID
                                  GROUP BY Business__c]) {

                                      balTotal +=  ((Decimal) ar.get('tot') == null ? 0 : (Decimal) ar.get('tot'));

                                  } 
        if(!MyBalance.isEmpty()){
            MyBalance.put('Total',balTotal);

        }
System.debug('Balance total'+balTotal);

Planner__c in query is nothing but the account.Any help is much appreciated.

3
  • Your question is similar to this one salesforce.stackexchange.com/questions/22967/… Commented May 21, 2020 at 9:39
  • 1
    Why are you grouping by Business__c and not Planner__c ? How is Business__c related to this data model?
    – EranV
    Commented May 21, 2020 at 13:50
  • Hi @EranV , I did group it by Planner__c and then added the values in map, after debugging and making changes in queries finally got what I needed. :) Commented May 21, 2020 at 14:37

1 Answer 1

1

So you want to hold a total value of Business_Transactions__c.Commission__c for all Business_Transactions__c under a single Account?

If so, create a Map<Id, Decimal>, iterate and total each Business_Transactions__c, check if Account Id of the current Var is in keySet, if not put it, if yes, do a get and add to the value.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .