Scheduled class not showing up in scheduled jobs

2.0K    Asked by Manishsharma in Web-development , Asked on Aug 2, 2021

I am trying to run an apex class daily using System.schedule but It is not showing up in scheduled jobs nor I am able to query from Cron trigger on owner Id as my Id This is the piece of code. Note : - I need to run my apex class everyday

global class scheduledDeactivation implements Schedulable { public static String sch = '0 0 12 1/1 12 ? 2022'; global void execute(SchedulableContext SC) { deactivateUsers users = new deactivateUsers(); String jobID = System.schedule('Deactivate Inactive Users', sch, usrs); System.debug(jobId); } }


Answered by Al German

When you write jobs which implement Batchable or Schedulable, stick with the public modifier. You should never use the global modifier unless you actually need it (web service functionality or ApexRest) or if you are building a tool which you intend to distribute through a managed package but still want the code to be visible to others.


You don't schedule your job from within the execute method! You should schedule the job from somewhere outside the job. Otherwise every time it runs you add a duplicate job to the queue. The two most common ways to schedule a job which implements the Schedulable interface are:

Through the UI.

  • If you have a simple schedule like every day at X hour, use the UI to schedule class.
  • Navigate to Setup > Develop > Apex Classes.
  • Click the Schedule Apexbutton and select your class/schedule.
  • Click the Save button.

Through a script.

  • If you have a more complicated schedule, you can schedule the job using the system.schedule method.
  • Open up the Developer Console (or your IDE of choice) and go to Execute Anonymous.
  • Fire off a script that calls system.schedule similar to what you are currently attempting in your execute method.



Your Answer

Answer (1)

If a scheduled class or job is not showing up in your list of scheduled jobs, there are several potential reasons for this. Here are some troubleshooting steps to help identify and resolve the issue:

1. Verify the Schedule

Ensure that the class is correctly scheduled. Double-check the cron expression or the scheduling settings used.

2. Check for Errors

Look for any errors that might have occurred during the scheduling process. This can be done by checking logs or exception messages.

3. Confirm the Scheduler is Running

Make sure the scheduler service is running. If the scheduler is not running, no jobs will appear or be executed.

4. Ensure Proper Configuration

Ensure that the job scheduling configuration is correctly set up in your application or system. This includes checking configuration files or settings.

5. Verify Job Registration

Confirm that the job is correctly registered with the scheduler. Sometimes, jobs may not be registered due to issues in the job registration code.

6. Dependency Issues

Check if there are any dependency issues that might be preventing the job from being scheduled. This could include missing libraries or services that the job depends on.

7. Environment Specific Issues

Verify that the job is scheduled in the correct environment (e.g., development, testing, production). Sometimes jobs are scheduled in one environment but not another.

8. Permissions

Ensure that the account or service used to schedule the job has the necessary permissions to create and manage scheduled jobs.

9. Scheduler Specific Checks

Depending on the scheduler you're using (e.g., Quartz, Spring Scheduler, cron, etc.), there may be specific settings or common issues. Consult the scheduler's documentation for more detailed troubleshooting steps.

10. Logs and Monitoring

Review the scheduler logs and any monitoring tools you have in place. This can provide clues about what went wrong.

Example for Common Schedulers:

Quartz Scheduler:

Check JobStore Configuration: Ensure the job store is correctly configured.

Review Scheduler Logs: Look for any warnings or errors.

Check Trigger Configuration: Verify that the trigger is correctly configured and associated with the job.

Spring Scheduler:

@Scheduled Annotation: Ensure the method is annotated with @Scheduled.

Enable Scheduling: Make sure @EnableScheduling is present in your configuration class.

Cron:

  • Crontab File: Verify the crontab file entry is correct.
  • Cron Service: Ensure the cron service is running.

Example Troubleshooting Steps:

For Quartz:

  // Example job and trigger configurationJobDetail job = JobBuilder.newJob(MyJob.class)    .withIdentity("myJob", "group1")    .build();Trigger trigger = TriggerBuilder.newTrigger()    .withIdentity("myTrigger", "group1")    .startNow()    .withSchedule(SimpleScheduleBuilder.simpleSchedule()        .withIntervalInSeconds(40)        .repeatForever())    .build();Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();scheduler.start();scheduler.scheduleJob(job, trigger);// Verify Job in the SchedulerList jobGroupNames = scheduler.getJobGroupNames();for (String groupName : jobGroupNames) {    for (JobKey jobKey : scheduler.getJobKeys(GroupMatcher.jobGroupEquals(groupName))) {        System.out.println("Job found: " + jobKey.getName());    }}For Spring Scheduler:javaCopy code@Configuration@EnableSchedulingpublic class SchedulerConfig {    // Configuration to enable scheduling}@Componentpublic class MyScheduledTasks {    @Scheduled(fixedRate = 5000)    public void reportCurrentTime() {        System.out.println("The time is now " + new Date());    }}// Verify if the scheduler is working// Check application logs for the output of the scheduled method

By following these steps, you should be able to identify and resolve the issue of the scheduled class not showing up in scheduled jobs.

5 Months

Interviews

Parent Categories