You need to sign in to do that
Don't have an account?
Chelsea Lukowski
Batch job takes forever to run
I have a batch job that seems to take a long time to process. Is there anyway I can fix the job so that it does not take so long to update, is there something I can change to fix this?
global class batchAccountUpdate implements Database.Batchable<sObject>,Database.Stateful{ String query; Public static void RunMe(){ batchAccountUpdate fbatch = new batchAccountUpdate(); Id batchInstanceId = Database.executeBatch(fbatch, 200); } global Database.QueryLocator start(Database.BatchableContext BC) { //String query = 'select id,Account_Column_Key__c,Account__c,Previous_Year_Volume_YTD__c from Commission_Account__c '; return Database.getQueryLocator('select id,Account_Column_Key__c,Account__c,Previous_Year_Volume_YTD__c from Commission_Account__c'); } global void execute(Database.BatchableContext BC, List<Commission_Account__c> scope) { List<Commission_Account__c> commAcctList=new List<Commission_Account__c>(); for(Commission_Account__c commAcct:scope){ try{ decimal ii=0; List<Titan_Invoice__c> invList=[select id,Commission_Key__c,quantity__c,invoice_date__c from Titan_Invoice__c where Commission_Key__c=:commAcct.Account_Column_Key__c]; for(Titan_Invoice__c inv:invList){ date d=date.valueof('2015-01-01'); integer day=system.now().day(); integer month=system.now().month(); system.debug('=11===='+day); string ytdDate='2015-'+month+'-'+day; date e=date.valueof(ytdDate); if(inv.Invoice_Date__c>=d && inv.Invoice_Date__c<=e){ ii=ii+inv.quantity__c; } } commAcct.Previous_Year_Volume_YTD__c=ii; commAcctList.add(commAcct); }catch(exception e){ } } database.update(commAcctList,false); } global void finish(Database.BatchableContext BC) { } }
Right off the bat I can say that it is generally bad practice to place a query inside of a for loop. This is due to governor limits on queries. You can read more on those here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm). That being said modifications had to be done so accordingly. Since i dont know how many records of each object there are and the amount of impact triggers have in this org i had to make a best guess assumptions on how the batch is going to be run. Looking at your code I saw that it is run in batches of 200 which is the default so whith that in mind this is the modified code. Let me know if this helps. I made light comments to explain my reasoning. Questions are welcome.
If your triggers allow it you can try on bigger batch sizes in execute anonomouse like so:
Hope this helps!
AM
All Answers
Right off the bat I can say that it is generally bad practice to place a query inside of a for loop. This is due to governor limits on queries. You can read more on those here (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm). That being said modifications had to be done so accordingly. Since i dont know how many records of each object there are and the amount of impact triggers have in this org i had to make a best guess assumptions on how the batch is going to be run. Looking at your code I saw that it is run in batches of 200 which is the default so whith that in mind this is the modified code. Let me know if this helps. I made light comments to explain my reasoning. Questions are welcome.
If your triggers allow it you can try on bigger batch sizes in execute anonomouse like so:
Hope this helps!
AM