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
jameskCAjameskCA 

How to invoke system.schedule

I'm trying to create a schedule to run an email class we have.  I don't think I'm understanding the schedulable class and where / when you invoke the system.schedule method.  

I have an email class that works fine if you run with execute anonymous. 
CommissioningFollowup CF = new CommissioningFollowup();
My scheduler class uses the following code: 
global class SchedulerCommissioningFollowup implements Schedulable {
	global void execute(SchedulableContext SC) {
		SchedulerCommissioningFollowup SCF = new SchedulerCommissioningFollowup();   
        CommissioningFollowup CF = new CommissioningFollowup();        
		//Will Run Everyday at 00:00 (Midnight)
        String sch = '0 0 0 1/1 * ?';
		String jobID = system.schedule('Commissioning Followup Job', sch, SCF);
	}
}

I thought that would work, but nothing gets scheduled.  Am I supposed to call the system.schedule from my email class?  I'm just not sure how to actually schedule the job.  Thanks in advance.  
 
Arpit Jain7Arpit Jain7
HI Jim,

Change your schedule class as below

global class SchedulerCommissioningFollowup implements Schedulable
{
    global void execute(SchedulableContext SC)
    {        
        CommissioningFollowup CF = new CommissioningFollowup();               
    }
}

After this in developer console execute the below line of code to schedule your class

SchedulerCommissioningFollowup SCF = new SchedulerCommissioningFollowup();   
//Will Run Everyday at 00:00 (Midnight)
String sch = '0 0 0 1/1 * ?';
String jobID = system.schedule('Commissioning Followup Job', sch, SCF);

Try the above mentioned steps and that should fix your issue.

Let me know in case of any other concerns.

Thanks
jameskCAjameskCA
That works!  But, how would I use that in my email scenario?  Where are you supposed to actually call system.schedule?  In the class that's sending the email?  
Arpit Jain7Arpit Jain7
You need to call system.schedule in developer console and execute it anonymously like below

SchedulerCommissioningFollowup SCF = new SchedulerCommissioningFollowup();   
//Will Run Everyday at 00:00 (Midnight)
String sch = '0 0 0 1/1 * ?';
String jobID = system.schedule('Commissioning Followup Job', sch, SCF);