function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Tom SimmonsTom Simmons 

Help in converting Trigger to Batch class

All, here is my trigger and I want to convert this trigger into a batch class. I have 3 objects on this trigger. Account, Margin__C and commission__c.
Use case: This trigger currently run on Margin__C Object. When Margin__C is updated or inserted, it looks at date range between yr_StartDate__c and yr_endDate__c and compares it with object 'commission__c's field ‘Final_Date__c’. If ‘Final_Date__c’ falls between any of these dates, it sums the amount in ‘Gross_Commission__c’ field and updates it on Account record under Gross__C. The trigger runs well however when I try to convert it to batch class, it calculate all the amount in the  commission__c and puts it on the Account record. I only need it calculate when Gross_Commission__c falls between yr_StartDate__c and yr_endDate__c. Can someone pls help me batch class for trigger below?

 
trigger CalculateMargins on Margin__c (before update, before insert) {
    
    Map <id,commission__c> AdvMap = New Map  <id,commission__c>  ();
      Map <id,Account> AccMap = New Map  <id,Account>  ();
  
    Set <id> MarginSets = New Set <id> ();
    For (Margin__c Pay : Trigger.New) {
        MarginSets.add(Pay.Id);
     	
    }
    
    List <commission__c> CommsList = [SELECT ID, Gross_Commission__c,Margin__c,Accounts__c FROM commission__c WHERE Margin__c IN : MarginSets];
			  system.debug('list of CommsList: '+ CommsList);
    

            Map<Id, Account> c= new Map<Id, Account>();
        
        for (commission__c ps : CommsList)
        { 
            Account Acc = new Account(Id = ps.Accounts__c);
            Acc.Gross__C = 0;
            AccountMap.put(ps.Accounts__c, Acc);
        }
            
             
                For (Margin__c Margin : Trigger.New) {
                    
                    Date Startdate = Margin.yr_StartDate__c;
                    Date enddate = Margin.yr_EndDate__c;

                    system.debug(Startdate + '@@@' +  enddate);

            
        for (commission__c     ps : [select Accounts__c, Gross_Commission__c, Margin__c
                                     from commission__c
                                     where    Final_Date__c >= :Startdate  
                                              AND Final_Date__c <= :enddate])
        {
            Account accs = AccountMap.get(ps.Accounts__c);

            if (ps.Gross_Commission__c != null && ps.Accounts__c != null && ps.Margin__c != null)
            {
                accs.Gross__C += ps.Gross_Commission__c;
                
            }
        }
       
       update AccountMap.values();
      
       
    }    
}

 
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for batch job
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html

Try to update your code like below
global class AccountUpdateBatchJob implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT all field from Margin__c where addFilter';
       
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<Margin__c> scope)
    {

		Map <id,commission__c> AdvMap = New Map  <id,commission__c>  ();
		  Map <id,Account> AccMap = New Map  <id,Account>  ();
	  
		Set <id> MarginSets = New Set <id> ();
		For (Margin__c Pay : scope) {
			MarginSets.add(Pay.Id);
			
		}
    
		List <commission__c> CommsList = [SELECT ID, Gross_Commission__c,Margin__c,Accounts__c FROM commission__c WHERE Margin__c IN : MarginSets];
			  system.debug('list of CommsList: '+ CommsList);
    

            Map<Id, Account> c= new Map<Id, Account>();
        
        for (commission__c ps : CommsList)
        { 
            Account Acc = new Account(Id = ps.Accounts__c);
            Acc.Gross__C = 0;
            AccountMap.put(ps.Accounts__c, Acc);
        }
            
             
                For (Margin__c Margin : scope) {
                    
                    Date Startdate = Margin.yr_StartDate__c;
                    Date enddate = Margin.yr_EndDate__c;

                    system.debug(Startdate + '@@@' +  enddate);

            
        for (commission__c     ps : [select Accounts__c, Gross_Commission__c, Margin__c
                                     from commission__c
                                     where    Final_Date__c >= :Startdate  
                                              AND Final_Date__c <= :enddate])
        {
            Account accs = AccountMap.get(ps.Accounts__c);

            if (ps.Gross_Commission__c != null && ps.Accounts__c != null && ps.Margin__c != null)
            {
                accs.Gross__C += ps.Gross_Commission__c;
                
            }
        }
       
       update AccountMap.values();
      


    }
    global void finish(Database.BatchableContext BC) {
    }
}

Let us know if this will help you