0

When we are running the batchApex we are facing "CPU time limit exceeded". Please find my below Batch Apex which we are tried and let me know where we made the mistakes in current Logic.

Batch:

global class OpportunityDays implements Database.Batchable {

Public List<Opportunity> lstOpp;

global Database.QueryLocator start(Database.BatchableContext BC){  

    String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0 AND ( ((CW_in_Last_30_Days_Formula__c = True AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = True AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True)) )';

    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Opportunity> batch){ 

    lstOpp = new List<Opportunity>();

    for(Opportunity objOpp : batch){
        if(objOpp.CW_in_Last_30_Days_Formula__c == True) {
            objOpp.CW_in_Last_30_Days_Workflow__c = True;
        }  

        else{ //if(objOpp.CW_in_Last_30_Days_Formula__c == False){
            objOpp.CW_in_Last_30_Days_Workflow__c = False;
        }

        if(objOpp.CW_in_Last_730_Days_Formula__c == True) {
            objOpp.CW_in_Last_730_Days_Workflow__c = True;
        }   

        else{ //if(objOpp.CW_in_Last_730_Days_Formula__c == False) {
            objOpp.CW_in_Last_730_Days_Workflow__c = False;
        }
        lstOpp.add(objOpp);
    }   

    Database.SaveResult[] dbOppList = Database.Update(lstOpp, false);   
    // System.debug('Update CW ' + dbOppList[].Id);  

    for(Database.SaveResult sr : dbOppList) {
        if(sr.isSuccess()) {
            // Operation was successful, so get the ID of the record that was processed
            System.debug('Successfully Updated OPP ID: ' + sr.getId());
        }       
        else {
            // Operation failed, so get all errors                
            for(Database.Error err : sr.getErrors()) {
                System.debug('The following error has occurred.');                    
                System.debug(err.getStatusCode() + ': ' + err.getMessage());
                System.debug('Users fields that affected this error: ' + err.getFields());
            }
        }           
    }

}    

global void finish(Database.BatchableContext BC){        
}  

}

Thanks

13
  • How many record you have in system in Opportunity Oct 4, 2016 at 8:22
  • 1
    Have you tried running that query in the query editor? What is the cost of it in the query plan?
    – Nick C
    Oct 4, 2016 at 8:23
  • Hi all, we have around 5447 records are available.
    – Devi
    Oct 4, 2016 at 8:29
  • From where you are initiating this batch execution?
    – Reshma
    Oct 4, 2016 at 8:30
  • Do you have any code in opportunity trigger? if yes, can you inactive trigger and try executing batch
    – S B
    Oct 4, 2016 at 8:34

2 Answers 2

0

Please update the code as below

    global class OpportunityDays implements Database.Batchable<Sobject>
    {
           global Database.QueryLocator start(Database.BatchableContext BC){  

              String query = 'Select id, Name, StageName, Software_Product_Count__c, CW_in_Last_30_Days_Formula__c, CW_in_Last_30_Days_Workflow__c, CW_in_Last_730_Days_Formula__c, CW_in_Last_730_Days_Workflow__c from Opportunity Where StageName = \'S8- Closed Won\' AND Software_Product_Count__c > 0';
            return Database.getQueryLocator(query);
        }
    global void execute(Database.BatchableContext BC,List<Opportunity> lstOpp)
    {

    List<Opportunity> oppList = new List<Opportunity>();        
    for(Opportunity objOpp : lstOpp){

    boolean shouldUpdate = false;
                if(objOpp.CW_in_Last_30_Days_Formula__c && !objOpp.CW_in_Last_30_Days_Workflow__c) {
                    objOpp.CW_in_Last_30_Days_Workflow__c = TRUE;
                    shouldUpdate = true;
                }   
                else if(!objOpp.CW_in_Last_30_Days_Formula__c && CW_in_Last_30_Days_Workflow__c) {
                    objOpp.CW_in_Last_30_Days_Workflow__c = False;
                    shouldUpdate = true;

                }

                if(objOpp.CW_in_Last_730_Days_Formula__c  && !objOpp.CW_in_Last_730_Days_Workflow__c) {
                    objOpp.CW_in_Last_730_Days_Workflow__c = True;

                    shouldUpdate = true;

                }   
                else if (!objOpp.CW_in_Last_730_Days_Formula__c && objOpp.CW_in_Last_730_Days_Workflow__c){
                    objOpp.CW_in_Last_730_Days_Workflow__c = False;

                    shouldUpdate = true;

                }                                
    if(shouldUpdate==true)
{
            oppList.add(objOpp);
}
            }
    if(oppList.size()>0)
              List<Database.SaveResult> resultList  = Database.update(oppList, false);
           // update lstOpp;    
        }    

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

If there is no trigger or workflow on the opportunity, this should work.And please reduce the batch size .

20
  • ,Thanks for your help... we are using your code with My SOQL : But The problem is Batch is Running fine after completed the batch showing as Total batch = 0 and Batches Processed = 0.But records are not updating. can you please help me on that
    – Devi
    Oct 4, 2016 at 13:55
  • Can you please let me know the number of records under the query returned and also please put a debug statement under every if condition .Reduce the batch size to 10 records to check what exactly is wrong.
    – Preya
    Oct 5, 2016 at 7:23
  • Hi Preya, Actually what happening in SOQL we are using below Query, Where StageName = 'S8- Closed Won' AND Software_Product_Count__c > 0 AND ((CW_in_Last_30_Days_Formula__c = True AND CW_in_Last_30_Days_Workflow__c = False) OR (CW_in_Last_30_Days_Formula__c = False AND CW_in_Last_30_Days_Workflow__c = True)) OR ((CW_in_Last_730_Days_Formula__c = True AND CW_in_Last_730_Days_Workflow__c = False) OR (CW_in_Last_730_Days_Formula__c = False AND CW_in_Last_730_Days_Workflow__c = True)). Problem is Only for 30 days workflows are updating as expecting.don't know why it is not working to 730 days.
    – Devi
    Oct 5, 2016 at 8:44
  • Using the code i have given above, are you able to update for 30 days workflow without cpu limit error? Please check the condition for 730 days and check if the records exist for the condition
    – Preya
    Oct 5, 2016 at 9:55
  • ,Yes. Your code is working as expected for 30 days without CPU limit error.same logic i have applied for 730 days. Why it is not updating.Can you have a look on this issue.Records are showing around 4300. Thanks
    – Devi
    Oct 5, 2016 at 10:17
0

Given that you want your Workflow field to always match your Formula field I don't understand why you have all the if/else stuff going on. You could simply assign the Workflow field the value of the Formula field.

global void execute(Database.BatchableContext BC,List<Opportunity> lstOpp){ 
    for(Opportunity objOpp : lstOpp){

        objOpp.CW_in_Last_30_Days_Workflow__c = objOpp.CW_in_Last_30_Days_Formula__c; 
        objOpp.CW_in_Last_730_Days_Workflow__c = objOpp.CW_in_Last_730_Days_Formula__c;

    }
    List<Database.SaveResult> resultList  = Database.update(lstOpp, false);
   // update lstOpp;    
} 
2
  • ,Thanks for your suggestions Actually we are trying to update like 30 Days Formula = True then 30 days Workflow should be true AND 30 Days Formula = False then 30 days Workflow should be False Similarly 730 days also these all are Picklists. Even though i have tried your logic batch is running but records are not updating.Can you please help me on this, Thanks
    – Devi
    Oct 4, 2016 at 14:15
  • @Devi If these are all picklists check your Workflow fields to see if they are restricted or not. If so check to see if the Formula picklists values match exactly the Workflow ones.
    – dBeltowski
    Oct 4, 2016 at 17:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .