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
NareshGNareshG 

Apex Scheduler Issue in VF controller

 

Hi, 

 

I am facing few challenges while scheduling a Batch apex class. I am trying to schedule a Batch job. When I use System.Schedule() to schedule an Apex job, I am seeing some strange behavior: 
- If I schedule the Batch class through a trigger, the job does show up on the 'Scheduled Jobs' page on the org. The Job is executing successfully. So no issue in this case.


 - If I schedule the same Batch class through a VF controller class, the Job does NOT show up on the "Scheduled Jobs" page. The job is not executing. I am using the same code in the controller. So there is no code change.  The second challenge I am facing is: I am not able to delete existing Scheduled jobs in Apex. I am using System.abort(jobId) to delete the job within Apex but it's not working. This is not deleting the job. 

 

Any help will be appreciated!!!

 

Thanks,

Naresh

lakslaks

Hi,

 

I am not sure of the 1st problem regarding invoking batch job from controller, haven't tried it.

 

However I have done the 2nd part about deleting scheduled jobs. Here is a code snippet for the same.

 

List<CronTrigger> listCronTrigger = [select Id, CronExpression, EndTime, NextFireTime, OwnerId,
        PreviousFireTime, StartTime, State, TimesTriggered, TimeZoneSidKey from CronTrigger 
        where State = 'Waiting'];
        
        System.debug('No of jobs: '+listCronTrigger.size());
        If (listCronTrigger.size() > 0)
        {
           for (Integer i = 0; i < listCronTrigger.size(); i++)
           { 
                System.abortJob(listCronTrigger[i].Id);
                System.debug('Job details ::'+String.valueOf(listCronTrigger[i]));
           }
        }

 The above code is to select jobs in "Waiting" state and delete them. In case you don't want to select jobs in any particular state just remove the where clause, and you can delete all your scheduled jobs.

 

Hope this helps.

 

Regards,

Lakshmi.

ngabraningabrani

Could you post the code you are using to schedule the job. It may help in understanding the first issue.