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 usrs = new deactivateUsers();
String jobID = System.schedule('Deactivate Inactive Users', sch, usrs);
System.debug(jobId);
}
}
Best Answer
Common Misconception
When you write jobs which implement
Batchable
orSchedulable
, stick with thepublic
modifier. You should never use theglobal
modifier unless you actually need it (webservice
functionality orApexRest
) 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 theSchedulable
interface are:Setup
>Develop
>Apex Classes
.system.schedule
method.Developer Console
(or your IDE of choice) and go toExecute Anonymous
.system.schedule
similar to what you are currently attempting in yourexecute
method.