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
AngelikaAngelika 

How do you implement Apex scheduler with System.Schedule method

 

 

I am new to apex and am trying to build an apex schedule class that runs everyday. If the account review date for commisions is two weeks (14 days) away the scheduler will send an email to our Sales Department.

 

I think I have all the information I need. I have a made a class that implements the schedulable interface.

 

I have a system schedule method but I don't know where to put it....in the scheduable class or in a new class?

 

Do I have to make a test class for this once my other code is all right? How do I build a test class?

 

Here is my code:

 

global class AccountReviewScheduler implements Schedulable {

 

   global void execute (SchedulableContext ctx) { //what does this line of code do?//

 

{

SendEmail();

}

 

 

public void SendEmail()

{

 

      if (Account.Next_Account_Review_Date_c == System.Today().addDays(14))

{

 

                 Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                 Mail.setTemplateId('00XF0000000LfE1;);

                Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail });

}

 

}

 

// I'm not sure how to link this information to the previous class, make another one? I don't think I can put it in the above class. This schedules my class to run every day at 2:00 am.

 

newScheduledClass m = new newScheduledClass();  

//schedule set for 2am everyday //

String s = '0 0 2 * * ?';

 

// I'm not sure what "Job Name" specifies. I got this code from someone else. //  

system.schedule('Job Name', s, m);

 

Thanks for your help. I have read over the tutorial in the Apex workbook and the Apex Scheduler in the documentation...but I still don't know where to put my system.schedule method or what "job name" means in my code.

 

Please help!

Leon MorockiLeon Morocki

You can run System.schedule from your developer console/execute enonymous windon in Eclipse. After that you can monitor your scheduled job in Setup -> Monitoring -> Scheduled Jobs.

 

Here you can find a detailed description how to test a scheduled job:

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm#apex_scheduler_testing

 

Job Name is just a user friendly name that's going to be displayed on the Schedule Jobs monitoring page.

salesforcecrapsalesforcecrap

Hey Angelika,

It's a great question that is really missing from the Apex documentation. You can do this explicitly by logging into Salesforce, clicking on your name in the upper right, then Developer Console, and then click the Workspace: Default dropdown in the upper right of the dev console and select "open execute anonymous window."

 

Yes, you have to test the class.