function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
James CarboneJames Carbone 

Aborting multiple scheduled jobs is not deleting all the jobs

I have a scenario that creates 6 scheduled jobs so they run every ten minutes.  I store the cron trigger id so I can delete all the jobs by clicking a button rather than delete one at a time in the Setup module's Scheduled Jobs page.

 

I try to spin through the six jobs and call system.abort for each one, one at a time.  The debug logs show abort is called and there is no exception caught in my try block.  The problem is only the first job is actually deleted.

 

Here is my code:

 

for(My_Setting__c setting : settings)

{

    try

    {

       System.debug('--- Delete scheduled job with Job_Id = ' + setting.Value__c);

      System.abortJob(setting.Value__c);

   delete setting;

    }

    catch (Exception e)

    {

        System.debug('--- Unable to delete scheduled job with Scheduled_Geocoder_Job_Id = ' + setting.Value__c + ' because ' + e.getMessage());

    }

}

 

The setting is deleted, but 5 of the 6 jobs remain.

 

Here is a snippet from the debug logs:

 

 

12:10:17.456 (341086000)|USER_DEBUG|[119]|DEBUG|--- Delete scheduled job with Job_Id = 08e500000000HRJAA2

12:10:17.456 (341126000)|METHOD_EXIT|[119]|System.debug(ANY)
12:10:17.456 (341182000)|STATEMENT_EXECUTE|[120]|System.abortJob(String)
12:10:17.456 (341223000)|METHOD_ENTRY|[120]|System.abortJob(String)
12:10:17.474 (359183000)|METHOD_EXIT|[120]|System.abortJob(String)
12:10:17.474 (359252000)|STATEMENT_EXECUTE|[122]|Delete: SOBJECT:Map_Setting__c
12:10:17.474 (359321000)|HEAP_ALLOCATE|[122]|Bytes:8
12:10:17.474 (359365000)|DML_BEGIN|[122]|Op:Delete|Type:Map_Setting__c|Rows:1
12:10:17.474 (359514000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:8
12:10:17.498 (383561000)|DML_END|[122]

12:10:17.456 (341086000)|USER_DEBUG|[119]|DEBUG|--- Delete scheduled job with Scheduled_Geocoder_Job_Id = 08e500000000HRJAA212:10:17.456 (341126000)|METHOD_EXIT|[119]|System.debug(ANY)12:10:17.456 (341182000)|STATEMENT_EXECUTE|[120]|System.abortJob(String)12:10:17.456 (341223000)|METHOD_ENTRY|[120]|System.abortJob(String)12:10:17.474 (359183000)|METHOD_EXIT|[120]|System.abortJob(String)12:10:17.474 (359252000)|STATEMENT_EXECUTE|[122]|Delete: SOBJECT:Map_Setting__c12:10:17.474 (359321000)|HEAP_ALLOCATE|[122]|Bytes:812:10:17.474 (359365000)|DML_BEGIN|[122]|Op:Delete|Type:Map_Setting__c|Rows:112:10:17.474 (359514000)|HEAP_ALLOCATE|[EXTERNAL]|Bytes:812:10:17.498 (383561000)|DML_END|[122]

 

 

lakslaks

Hi,

 

Instead of performing "delete setting" after abortJob(), try deleting the whole list "settings" after the for loop.

 

Not sure whether that exactly is the problem. But I had tried a similar code with the only difference being that I was deleting the whole list of Job Ids after the looping. So guess that might be the issue here.

 

List<JobID__c> l_ltJobIDList = new List<JobID__c>(); 

l_ltJobIDList = [Select id, Job_ID__c from JobID__c]; for(JobID__c l_objJobID : l_ltJobIDList) 

for(JobID__c l_objJobID : l_ltJobIDList) 
{ 
  try
  {
     System.abortJob(l_objJobID.Job_ID__c);
  }
  catch(Exception l_objEx)
  {
     String l_stErrMessage = 'Error in aborting jobs.';
     System.debug(l_stErrMessage);
  }
}
delete l_ltJobIDList;

 

 

Hope this helps.

 

Regards,

Lakshmi.