2

I found some documentation for scheduling apex jobs, but I'm a little stuck. I made a class that does what I want to be scheduled; it is called setInterest(). Here is what I have so far in the schedulable...

global class scheduledInterestCheck implements Schedulable {

    global void execute(SchedulableContext SC) {

        setInterest interest = new setInterest();

  }

}

What is that parameter?

Also, I saw part of the documentation had...

scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);

So it looks clear that something is calling the scheduled class, and setting the frequency it should be scheduled to. Where does this bit go?

  • It's a good practice to create a function (or three) for scheduling a class. That way, there's no need to always be looking up the parameters. I'll see if I can find an example. – Jagular Dec 22 '16 at 17:15
3

Just to add a touch more to Adrian's answer if I may, I'd like to give some context to this code that you've read:

scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
String jobID = system.schedule('Merge Job', sch, m);

Specifically, what does system.schedule('Merge Job', sch, m) do?

System.schedule is, as you've identified, a more complex method of scheduling a class and takes 3 parameters:

First Parameter

The first parameter is the name. This is used more as a description but at the time of execution, the name must be unique otherwise you'll get an error akin to the following:

Scheduled job 'MyScheduledJob' threw unhandled exception.

caused by: System.AsyncException: The Apex job named "MyScheduledJob'" is already scheduled for execution.

This may or may not be a good thing.

Second Parameter

This is the string you set here: String sch = '20 30 8 10 2 ?'; and is known as a "Cron Expression". This is constructed by:

  1. Seconds
  2. Minutes
  3. Hours
  4. Day of Month
  5. Month
  6. Day of Week
  7. Year (Optional)

There's also some special characters, I'd highly recommend reading up on this documentation. Basically, this tells the scheduler how often it should run. Here's a table from that documentation that explains it a bit better:

Expression          Description
0 0 13 * * ?        Class runs every day at 1 PM.
0 0 22 ? * 6L       Class runs the last Friday of every month at 10 PM.
0 0 10 ? * MON-FRI  Class runs Monday through Friday at 10 AM.
0 0 20 * * ? 2010   Class runs every day at 8 PM during the year 2010.

Third Parameter

The final parameter is the class itself that you want to execute! In this case, it's a class called scheduledMerge.

  • Thanks a bunch! I am actually pretty okay with understanding what it is, I'm more confused about where it goes. Is there another class I'm missing? In my mind, there is the class that does the work, the class implementing shedulable interface that you schedule as a job that calls the first class. However, that code seems to be calling the scheduled class....so there's a third one,....? – Natalie Spatharakis Dec 22 '16 at 16:49
  • 1
    @AdrianLarson's answer covers how to do that using the UI. But to make a class available for scheduling you need to implement schedule. Something like this: global class MySuperCoolScheduleClass implements Schedulable { followed by your batch class code. Then, you can either do what Adrian has said in his answer, or use the Developer Console, Debug > Open Execute Anonymous Window and paste in the code you've got in your question or a variation of it. – Dan Jones Dec 22 '16 at 16:54
  • Thanks for following up -- I'm sure I'm just missing one silly thing, but if you look at my code from my question, I have the implements schedulable class. What am I missing? It doesn't show as an option when I go to the UI through setup and try to find it as a class. – Natalie Spatharakis Dec 22 '16 at 17:05
  • 1
    @NatalieSpatharakis Ah I see, you're instantiating the class setInterest() but not telling Salesforce to do anything! I think the line you're missing is: Database.executeBatch(interest); – Dan Jones Dec 22 '16 at 17:06
  • 1
    @NatalieSpatharakis Editted the Answer. :) It was also Database.executeBatch is what I meant! – Dan Jones Dec 22 '16 at 17:14
2

Parameter Meaning

As for your first question the SchedulableContext interface:

Represents the parameter type of a method in a class that implements the Schedulable interface and contains the scheduled job ID. This interface is implemented internally by Apex.

How To Schedule

Unless you are making a complicated schedule (more frequently than once daily), just schedule it through the UI. Go to Setup > Develop > Apex Classes, and click on the Schedule Apex button.

Apex Classes Buttons

If you want to do a more complicated schedule than once at the same time each day (or on certain days of the week), then you can schedule the class via Execute Anonymous where you would paste a script similar to what you shared in your OP. Make sure the CRON expression (your sch variable) is correct before you actually run it.

  • oh, wow! Thanks! I'll give it a try, that seems much easier. – Natalie Spatharakis Dec 22 '16 at 16:19
  • Is the end date field when it should stop scheduling? I kind of want it to be scheduled daily indefinitely, unless it refers to something else.. – Natalie Spatharakis Dec 22 '16 at 16:20
  • Yeah, that's one downfall of the UI is you do have to set an end date. But you can set it many years out. – Adrian Larson Dec 22 '16 at 16:21
  • 1
    @NatalieSpatharakis N.B. Sometimes, in order for the schedulable class to even show up as a candidate for scheduling - even though it implements the schedulable interface, you have to use DC or the UI to Run Tests for that class's testmethod – cropredy Dec 22 '16 at 16:40
  • That's what I was coming back to ask, actually. I am searching for the class and I cannot find it. I'm in the "Schedule Apex" window from setup. What does DC refer to? – Natalie Spatharakis Dec 22 '16 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.