You need to sign in to do that
Don't have an account?
Neil Kim
Avoid 'Apex CPU time limit exceeded' with Batch Process
Hi exports. I encountered this error and have trouble with performance tuning.
I tried asyncronous approach, but Apex CPU time limit exceeded occurs continuously.
Anybody can make it more efficient?
Here is code and size of each Lists from static method is about 15,000.
Please help.
Thanks in advance.
I tried asyncronous approach, but Apex CPU time limit exceeded occurs continuously.
Anybody can make it more efficient?
Here is code and size of each Lists from static method is about 15,000.
Please help.
static List<Schedule__c> getScheduleList(){ return [SELECT ID, Opportunity__c, StartTime__c FROM Schedule__c WHERE Result__c = 'Success' ORDER BY Opportunity__c]; } global Database.QueryLocator start(Database.BatchableContext BC) { query = 'SELECT ID FROM CASE'; return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope) { List<Opportunity> allopps = getOppList(); List<Schedule__c> schedules = getScheduleList(); List<Schedule__c> filtered = new List<Schedule__c>(); Datetime dt; Boolean check; Boolean status; for ( Opportunity opp : allopps ){ dt = null; check = false; status = false; for (Schedule__c schedule : schedules ){ if( check == false && status == true ) break; if( schedule.Opportunity__c == opp.id ){ check = true; filtered.add(schedule); if( dt == null || schedule.StartTime__c < dt) dt = schedule.StartTime__c; i++; } else if( check == true && status == false){ check = false; status = true; } } if( filtered.size() >= 1){ opp.SuccessDate__c = dt; opp.StageName = 'Success'; } else if( filtered.size() == 0 && opp.StageName == 'Success'){ opp.SuccessDate__c = null; opp.StageName = 'Initial Stage'; } } System.debug('schedule size : ' + schedules.size()); System.debug('size : ' + allopps.size() + ' count : ' + i); /* try{ update allopps; }catch( Exception e){ System.debug('Error : ' + e.getMessage()); } */ }
Thanks in advance.
Try this below Code:
Mark this as best answer if you find it positive.
Thanks
Jolly Birdi
All Answers
Use Map instead of List for all opps and Schedules, It will provide you the quick results.
Thanks
Jolly Birdi.
Of cource, I consider using Map. However, I cannot imagine it can be reduce CPI usage.
If possible, please write some code making map and loop for this map with my variables?
Try this below Code:
Mark this as best answer if you find it positive.
Thanks
Jolly Birdi
Thank you. I got your approach using Map and it works.
Thanks!