Find your content:

Search form

You are here

calling the system.schedule method in a generic context

 
Share

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??


Attribution to: Christopher Alun Lewis

Possible Suggestion/Solution #1

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


Attribution to: Jon Hazan
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3370

My Block Status

My Block Content