7

I am trying to call the system.schedule method in a generic manner to help avoid scheduled job naming confusion like so:

 // Set my schedulable class to run.
private String setMyClassRunning(String jobName, String sch){
    SchedulableClass sc = new SchedulableClass();

    String scheduledId = scheduleJob(jobName, sch, sc);
    return scheduledId;
}

// Set a scheduled job to run. This function has been isolated to account for test scenarios.
// If this method is called in a testing context, then add "Test" to the job name to avoid conflicts 
// with already scheduled jobs.
private String scheduleJob(String jobName, String cronExpression, Object schedulableClass)
{
    if(Test.isRunningTest())
    {
        jobName = 'Test' + jobName;
    }
    return system.schedule(jobName, cronExpression, schedulableClass);  
}

However, when I try and save the code, I get the error "Method does not exist or incorrect signature: system.schedule(String, String, Object);

This is confusing as the system method descriptions clearly show that this method does exist.

I know that it is possible to schedule a job using the method on an individual class basis, but the point is I want to make this generic, so I don't have to re-write the testing name convention code every time. Can anyone help??

1 Answer 1

9

Ive had a play with this in a dev instance and it seems changing your second functions arguments from

private String scheduleJob(String jobName, String cronExpression, Object schedulableClass)

to

private String scheduleJob(String jobName, String cronExpression, Schedulable schedulableClass)

fixes the error message.

EDIT: Ive now tested this in a dev instance and substituting 'Object' for 'Schedulable' works and the job does get created.

This blog also seems to be trying to achieve something similar to you: http://alexdberg.blogspot.co.uk/2011/06/starting-apex-scheduled-jobs-without.html

1
  • Thanks Jon, that is exactly what I needed, works perfectly! Commented Oct 16, 2012 at 13:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .