Passing Parameter Into Schedulable Class

Is there a way to pass a parameter into a schedulable class execute method? We were trying to pass a string from another scheduled class into this scheduled class but it looks like we can’t do that?

As quick context we have the scheduled date and time as a record in a custom object so we are trying to pass that info into our scheduled class.

global class MturkVSSchedule implements Schedulable{

    global void execute(SchedulableContext SC, **String s**){

Putting String S throws a compile error.
Is there a different way that I’m missing?

Answer

What about passing parameters through constructor and storing them in instance variables?

public class Dummy_Schedulable_Class implements Schedulable{

    public List<String> names;
    public List<String> cronExpressions;

    public void execute(SchedulableContext SC) {
        System.debug(LoggingLevel.ERROR, names );
        System.debug(LoggingLevel.ERROR, cronExpressions );
        testScheduleClassMethod(names[0]);
    }

    public void testScheduleClassMethod(string argPass){

    }

    public Dummy_Schedulable_Class ( List<String> aNames, List<String> aCronExps) {
        if ( names == null ) {
            names = aNames;
        } else {
            names.addAll(aNames);
        }
        if ( cronExpressions == null ) {
            cronExpressions = aCronExps;
        } else {
            cronExpressions.addAll(aCronExps);
        }
    }

}

And whenever you need to pass parameters you can do the following:

System.schedule('test', '0 47 18 * * ?', new Dummy_Schedulable_Class(new String[]{'a','b'}, new String[]{'c','d'}));

Please mark this answer as accepted or vote up if you agree that it answers your question.

Attribution
Source : Link , Question Author : ddeve , Answer Author : Patlatus

Leave a Comment