You need to sign in to do that
Don't have an account?
Shantanu Srivastava
Apex Batch Job stuck in Processing state
I have an apex Batch Job which gets stuck in Processing status when run on more than 20 records, say 50, but runs well if run on 20 or less records.
If the same code is run via the execute anonymous window all the records are updated successfully.
There is no error in debug logs initially but eventually an Internal Salesforce Error can be seen in logs although the Batch Job stays in Processing status.
Salesforce support suggested using transient keyword, but even that didn't work. Keeping the variables as transient is also not solving the problem. I've now removed most of the functionality, have also removed Database.Batchable. I'm clearing the main list after every update.
But still the batch job is getting stuck in Processing. This is very weird as a batch of 50 records cannot exhaust all the resources causing the entire operation to get stuck.
Thanks,
Shantanu
If the same code is run via the execute anonymous window all the records are updated successfully.
There is no error in debug logs initially but eventually an Internal Salesforce Error can be seen in logs although the Batch Job stays in Processing status.
15:31:29.539 (2539559703)|FATAL_ERROR|Internal Salesforce.com ErrorCan someone please help me with what is causing this Salesforce Error?
Salesforce support suggested using transient keyword, but even that didn't work. Keeping the variables as transient is also not solving the problem. I've now removed most of the functionality, have also removed Database.Batchable. I'm clearing the main list after every update.
But still the batch job is getting stuck in Processing. This is very weird as a batch of 50 records cannot exhaust all the resources causing the entire operation to get stuck.
Thanks,
Shantanu
Can you share the code to see what was the issue . I am thinking that there might be chance to issue with code as well
Thanks,
Raj
Thanks for the quick response. Here is the code - there might be some unused variables and chance of optimization but still can't see what is causeing the issue. Any help would be appreciated.
global void execute(Database.BatchableContext BC, List <Opportunity> scope){
System.debug('>> Batch Job InactiveOpportunityBatch EXECUTE');
Boolean toBeClosed = true;
try {
for (Opportunity opp : scope){
/*
1- Get the daily date
2- Check if all activities are completed for an Opportunity
3- If any Activity is Open >> Check if due date is more than 30 days older than today
4- Close the Opportunity only if stage = In Negotiation or Proposal
5- If even 1 Pending Activity exists with Due Date less than 30 days from today, keep the Opportunity Open
6- If no Task/Activity exists on the Opportunity check if Created Date is more than 30 days old and then close.
*/
//Set toBeClosed as true by default(id any of the check fails set this as false and don't close the opp)
toBeClosed = true;
//Check if any Activity/Task exists for the Opportunity
if(opp.Tasks.size() > 0){
//Iterate thru all Tasks on this Opportunity
System.debug('>> Tasks fetched for this Opp : ' + opp.Tasks);
for (Task taskInstance : opp.Tasks){
//Check if Activity is still open
System.debug('>> taskInstance.IsClosed : ' + taskInstance.IsClosed);
if(!taskInstance.IsClosed){
//Check if Due date is more than 30 days old from today
System.debug('>> taskInstance.ActivityDate : ' + taskInstance.ActivityDate);
System.debug('>> system.today().addDays(-lastNDays) : ' + system.today().addDays(-lastNDays));
if(taskInstance.ActivityDate != null && taskInstance.ActivityDate > system.today().addDays(-lastNDays)){
toBeClosed = false;
// break;
}
}
}
}
//If no Activity/Task exists for the Opportunity-
//Check if Created Date is more than 30 days old from today
else{
System.debug('>> No Activity found for the Opp : ' + opp.Name);
System.debug('>> Opportunity.CreatedDate : ' + opp.CreatedDate);
System.debug('>> system.today().addDays(-lastNDays) : ' + system.today().addDays(-lastNDays));
if(opp.CreatedDate > system.today().addDays(-lastNDays)){
toBeClosed = false;
}
}
System.debug('>> Opp - ' + opp.Name + ' toBeClosed : ' + toBeClosed);
if(toBeClosed){
opp.StageName = 'Inactive - Closed Lost';
oppsToBeClosed.add(opp);
//oppIdNameMap.put(opp.Id, opp.Name);
}
}
System.debug('>> # of Opps to be closed : ' + oppsToBeClosed.size());
if (oppsToBeClosed.size() > 0) {
if(Test.isRunningTest()){
for (Integer x=2; x<oppsToBeClosed.size();x++){
oppsToBeClosed[x].Name = '0'.repeat(300);
}
}
//Update the Opportunites
//Transient Database.SaveResult[] resultList = Database.Update(oppsToBeClosed, false);
Database.Update(oppsToBeClosed, false);
oppsToBeClosed.clear();
if(!sendEmail){
System.debug('>> Send Email Disabled. Exiting Execute...');
// return;
}
}
} catch (Exception e) {
System.debug('>> Error occured in InactiveOpportunityBatch batch ' + e.getMessage());
}
}
I don't get this. What exactly have you changed??
I need to break at line 32, why should I loop thruogh all tasks when I've already found one valid task.
Thanks,Shantanu
You able to get anything about this issue?