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.
static List<Opportunity> getOppList(){
return [SELECT ID, StageName, SuccessDate__c FROM Opportunity];
}
static List<Schedule__c> getScheduleList(){
return [SELECT ID, Opportunity__c, StartTime__c FROM Schedule__c WHERE Result__c = 'Complete'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());
}
*/
}