I have a batch apex class that is facing CPU time limit exceeded error. Can someone please help me to by pass this error here? Please find my batch apex here.
global class Account_chk_batch implements Database.Batchable<sObject>{
public String query;
global Database.QueryLocator start(Database.BatchableContext BC){
List<Attachment> exist=new List<Attachment> ();
exist=[select ParentId,id from Attachment];
Map<Id, Attachment> existMap = new Map<Id, Attachment>();
for (Attachment att : exist) {
existMap.put(att.ParentId, att);
}
set<Id> keys = existMap.keySet();
query = 'select Id,(select Name from Call2_vod__r where Signature_Date_vod__c !=Null and Account_vod__c not in :keys limit 1) from Account';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<sObject> scope) {
List<Call2_vod__c> a= new List<Call2_vod__c>();
List<Attachment> p=new List<Attachment> ();
Map<Id,Call2_vod__c> signMap = new Map<Id, Call2_vod__c>();
for(sObject s : scope){Account call = (Account)s;
//for (Account call: [select Id,(select Name from Call2_vod__r where Signature_Date_vod__c !=Null and Account_vod__c not in :existMap.keySet() limit 1) from Account ])
for(Call2_vod__c c : call.Call2_vod__r){
if(call.Name== c.Name){
Attachment record = new Attachment(Name = Call.Name ,ParentId = call.Id, Body = EncodingUtil.base64Decode(c.Signature_vod__c), ContentType = 'image/jpeg');
if (record.parentId !=Null ) {
p.add(record);
}
}
}
try { insert(p);
} catch(System.DMLException e){
System.debug('The following exception has occurred: ' + e.getMessage());
}
}
}
global void finish(Database.BatchableContext BC){
}
}
Best Answer
The only work done in the
start
method should be the creation of the query locator and that should ideally not include related (child) records because the numbers of those cannot be controlled by the batching mechanism.So some major refactoring is needed to something like this (which is untested and may or may not be logically correct so review line by line):