13

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?

  • Hi user2451, please mark my answer as accepted if it solves your issue – Patlatus Feb 12 '16 at 14:29
15

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

global class Dummy_Schedulable_Class implements Schedulable{

    global List<String> names;
    global List<String> cronExpressions;

    global 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.

  • I just tried this. I haven't finished my testing, but the parameter passing by way of the constructor appears to be working. – Jagular Jun 2 '14 at 20:30
  • @Jagular I'm glad this helped to you – Patlatus Jun 4 '14 at 7:08
  • @Patlatus It Worked...ThankYou!!! – A.Bretto Jun 25 '18 at 7:00
8

There is no way to pass information in an Apex sense.

Passing Information to Schedulable Job using a Custom Object.

You can pass information via the database, the basic steps are this...

  1. Use the Id returned from the System.schedule method
  2. After scheduling the job insert a record into a Custom Object (e.g. Schedule Info) that contains the information you want the job to read when it executes. Being sure to also store the job Id on the record.
  3. Then in the execute method use the SchedulableContext.getTriggerId (confusing name i know) method to obtain the job Id and query the record previously written for the information needed.
  4. You should consider putting in a try/finally some cleanup code to delete the record once the execute completes.

Apologies for not giving a code sample here, let me know if you need one and I can work one up.

NOTE: You may also want to consider using a List type protected Custom Setting, which has the benifit of saving DML, SOQL and is hidden from the end users.

  • loved the concept of using object as variable ! – Mohith Shrivastava Jul 27 '13 at 9:00
  • ha, thanks, i've not tried using this approach with a custom setting before, maybe a blog one day... :) – Andrew Fawcett Jul 29 '13 at 8:41
2

I use generally use a static variable (as seen below), and pass that into a system.schedule call, but there's no reason you cant query the database to construct the string instead. I hope this helps

global class ScheduledJob implements Schedulable {

    public static String Sched = '0 00 22 * * ?';  //Every Day at 10PM 
    //I use a set schedule, you can instead query your custom object below      

    global static String scheduleThisJob() {

        object object = [Select Id, Name From Custom_Object__c];
        Sched =  ?? //use the object above to construct the chron for the scheduled job

        ScheduledJob SJ= new ScheduledJob(); 
        return System.schedule('My Scheduled Job', Sched, SJ);
    }

    global void execute(SchedulableContext sc) {

        BatchActiveMemberCheck BatchActive = new BatchActiveMemberCheck(ActiveMemberCheckUtililty.activeContactIDs());
        ID batchprocessid = Database.executeBatch(BatchActive,50);      
    }


}
  • Originally I was trying to pass the scheduling string into the execute method which is where I was having problem. We have 1 scheduled class which say runs once per day, and that is supposed to create multiple other scheduled classes. So the execute method was to receive the string you reference so it can pass those off to the other scheduled method. But trying to see if we can use what you posted and accomplish this another way? So if my object has 10 items to be scheduled at various times today, we wanted to do that. Different times, or none, tomorrow, etc. – user2451 Jul 26 '13 at 19:57

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.