Skip to main content
Edited for clarity
Source Link
crmprogdev
  • 41k
  • 9
  • 58
  • 116

I have an Object called Customer which has related object Purchase, Now every Purchase has Account owner. Under one Customer there can be many purchase from different Account. I need to calculate 3 fields Gross_Commission__c , Debit__c ,Credit__c from purchase under one account owner and create one record of Purchase and attach it back to same Customer. My batch is able to create calcualtions but while attaching it back to record is only creating one record and giving all the calculations under one Account owner. For examle 1 Customer has 15 Purchase in which 10 Purchase has Account 1 as owner and 5 purchase has Account 2 as owner. Now I want to calculate sum of 3 fields from purchase which has same owner and create one single record and attach it back under customer. i.e. there will be 2 new records with calculations of 10 purchase under Account owner 1 and calculation of 5 purchase under account 2 as owner. owner is custom field.

I have an Object called Customer which has related object Purchase. Every Purchase has Account owner. Under one Customer there can be many purchase from different Account. From each purchase I need to create one record of Purchase under one account owner where the Purchase gets attached back to the same Customer. I calculate 3 fields for the purchase and account:

  • Gross_Commission__c
  • Debit__c
  • Credit__c

My batch is able to create calcualtions but while attaching it back to the Purchase record, it is only creating one single record and giving all the calculations under one Account owner.

For example 1 Customer has 15 Purchases in which 10 Purchases has Account 1 as owner and 5 purchase has Account 2 as owner. Now I want to calculate sum of 3 fields from purchase which has same owner and create one single record and attach it back under customer. i.e. there will be 2 new records with calculations of 10 purchases under Account owner 1 and calculation of 5 purchases under account 2 as owner. Also, owner is a custom field.

I have an Object called Customer which has related object Purchase, Now every Purchase has Account owner. Under one Customer there can be many purchase from different Account. I need to calculate 3 fields Gross_Commission__c , Debit__c ,Credit__c from purchase under one account owner and create one record of Purchase and attach it back to same Customer. My batch is able to create calcualtions but while attaching it back to record is only creating one record and giving all the calculations under one Account owner. For examle 1 Customer has 15 Purchase in which 10 Purchase has Account 1 as owner and 5 purchase has Account 2 as owner. Now I want to calculate sum of 3 fields from purchase which has same owner and create one single record and attach it back under customer. i.e. there will be 2 new records with calculations of 10 purchase under Account owner 1 and calculation of 5 purchase under account 2 as owner. owner is custom field.

I have an Object called Customer which has related object Purchase. Every Purchase has Account owner. Under one Customer there can be many purchase from different Account. From each purchase I need to create one record of Purchase under one account owner where the Purchase gets attached back to the same Customer. I calculate 3 fields for the purchase and account:

  • Gross_Commission__c
  • Debit__c
  • Credit__c

My batch is able to create calcualtions but while attaching it back to the Purchase record, it is only creating one single record and giving all the calculations under one Account owner.

For example 1 Customer has 15 Purchases in which 10 Purchases has Account 1 as owner and 5 purchase has Account 2 as owner. Now I want to calculate sum of 3 fields from purchase which has same owner and create one single record and attach it back under customer. i.e. there will be 2 new records with calculations of 10 purchases under Account owner 1 and calculation of 5 purchases under account 2 as owner. Also, owner is a custom field.

Source Link

Fields Calculations in Batch Apex

I have an Object called Customer which has related object Purchase, Now every Purchase has Account owner. Under one Customer there can be many purchase from different Account. I need to calculate 3 fields Gross_Commission__c , Debit__c ,Credit__c from purchase under one account owner and create one record of Purchase and attach it back to same Customer. My batch is able to create calcualtions but while attaching it back to record is only creating one record and giving all the calculations under one Account owner. For examle 1 Customer has 15 Purchase in which 10 Purchase has Account 1 as owner and 5 purchase has Account 2 as owner. Now I want to calculate sum of 3 fields from purchase which has same owner and create one single record and attach it back under customer. i.e. there will be 2 new records with calculations of 10 purchase under Account owner 1 and calculation of 5 purchase under account 2 as owner. owner is custom field.

global class ArchiveAggregateAL_Batch implements database.Batchable<SObject> {

global String CustomerRecordID ;
global set<String> AccountId = new Set <String>();

global  Double CommissionTotals = 0;
global Double debitTotals = 0;
global Double creditTotals = 0;

global  Id CT = Schema.SObjectType.purchase__c.getRecordTypeInfosByName().get('Commission Transactions').getRecordTypeId();
//Manual Adjustment 

global  Id MA = Schema.SObjectType.purchase__c.getRecordTypeInfosByName().get('Manual Adjustment').getRecordTypeId();    


public ArchiveAggregateAL_Batch(String CusID, Set<String> AdvId){
    this.CustomerRecordID = CusID;
    this.AccountId = advId;
    
}

global database.QueryLocator Start(Database.BatchableContext bc){
    
    System.debug('In Start Method');
    String query = 'Select Gross_Commission__c, Debit__c, Credit__c ,RecordtypeId ,Advisor__c  from purchase__c  WHERE '+
        ' RecordtypeId = :CT AND Advisor__c = :AccountId '+ 
        ' AND Customer__c = :CustomerRecordID AND Object_Backup__c = TRUE';
    
    return Database.getQueryLocator(query);
} 

global void execute(Database.BatchableContext bc, List<purchase__c> scope){
    
    List<purchase__c> ALList = new List<purchase__c>();
    System.debug('In execute Method');
    //   purchase__c AggAdv = new purchase__c();
    String advId;
    for (purchase__c AL : scope) {
        if(AL.Gross_Commission__c!=NULL){
        CommissionTotals +=  AL.payout__Gross_Commission__c;
            }
         if(AL.Debit__c!=NULL){
        debitTotals +=  Al.payout__Debit__c ;
         }
        if(AL.Credit__c!=NULL){
        creditTotals +=  AL.payout__Credit__c;
        }
        advId = AL.Advisor__c;
        
        
        
    }
    
    for (purchase__c AL : scope) {
        purchase__c AggAdv = new purchase__c();
        AggAdv.Gross_Commission__c = CommissionTotals;
        AggAdv.Debit__c = debitTotals;
        AggAdv.Credit__c = creditTotals;
        AggAdv.RecordTypeId = MA;
        AggAdv.Customer__c = CustomerRecordID;
        AggAdv.Advisor__c = AL.payout__Advisor__c;
        ALList.add(AggAdv);
    } 
    insert ALList;
}

public void finish(Database.BatchableContext BC){
}

}