You need to sign in to do that
Don't have an account?
Tom 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?
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(); } }
1) http://amitsalesforce.blogspot.com/2016/02/batch-apex-in-salesforce-test-class-for.html
Try to update your code like below
Let us know if this will help you