Skip to main content
added 14 characters in body
Source Link
Matt Lacey
  • 25.8k
  • 7
  • 68
  • 153

Here's my way of passing parameters to a schedulable apex class.

This following class has a constructor that takes two parameters and sets the class attributes to hold the values passed into as the arguments and creates an instance object of the class which will contain the class attributes as well as the class methods. Here we have one method called main (note: this is an arbitrary name).

The execute method is the Schedulable interface's method which needs to be specified because we're implementing the interface and this method calls the instance object's main method which can use the passed-in arguments that have been saved into the object's attribute variables.

public with sharing class CronJob_That_Takes_Parameters implements Schedulable {
    String aString; Integer toBeCalculated;
    @testVisible Integer anInteger; // tip: use @testVisible annotation if you want to avoid hard coding values in the unit test

    public void execute(SchedulableContext ctx) {
        this.main(); // invoke the main method from this schedulabel execute method
    }

    // this is the class constructor
    public CronJob_That_Takes_Parameters(String aString, Integer anInteger) {
        this.aString = aString;
        this.anInteger = anInteger;
        this.toBeCalculated = this.anInteger * this.aString.size() /* you can also use custom settings and/or custom metadata values here */;
    }

    // put in the code for the main process
    public void main() {
        try {
            // main logic

        } catch (Exception ex) {
            // error handling
        }
    }
}

}

in order to schedule this class use the following code.

    // schedule the cron job
    String aString = 'String value argument', cronExp = '0 0 6 ? * MON-FRI'; // every weekday at 6am
    Integer anInteger = 10;
    CronJob_That_Takes_Parameters cronJob = new CronJob_That_Takes_Parameters(aString, anInteger);
    System.schedule('cron job name goes here!', cronExp, cronJob);

Here's my way of passing parameters to a schedulable apex class.

This following class has a constructor that takes two parameters and sets the class attributes to hold the values passed into as the arguments and creates an instance object of the class which will contain the class attributes as well as the class methods. Here we have one method called main (note: this is an arbitrary name).

The execute method is the Schedulable interface's method which needs to be specified because we're implementing the interface and this method calls the instance object's main method which can use the passed-in arguments that have been saved into the object's attribute variables.

public with sharing class CronJob_That_Takes_Parameters implements Schedulable {
String aString; Integer toBeCalculated;
@testVisible Integer anInteger; // tip: use @testVisible annotation if you want to avoid hard coding values in the unit test

public void execute(SchedulableContext ctx) {
    this.main(); // invoke the main method from this schedulabel execute method
}

// this is the class constructor
public CronJob_That_Takes_Parameters(String aString, Integer anInteger) {
    this.aString = aString;
    this.anInteger = anInteger;
    this.toBeCalculated = this.anInteger * this.aString.size() /* you can also use custom settings and/or custom metadata values here */;
}

// put in the code for the main process
public void main() {
    try {
        // main logic

    } catch (Exception ex) {
        // error handling
    }
}

}

in order to schedule this class use the following code.

// schedule the cron job
String aString = 'String value argument', cronExp = '0 0 6 ? * MON-FRI'; // every weekday at 6am
Integer anInteger = 10;
CronJob_That_Takes_Parameters cronJob = new CronJob_That_Takes_Parameters(aString, anInteger);
System.schedule('cron job name goes here!', cronExp, cronJob);

Here's my way of passing parameters to a schedulable apex class.

This following class has a constructor that takes two parameters and sets the class attributes to hold the values passed into as the arguments and creates an instance object of the class which will contain the class attributes as well as the class methods. Here we have one method called main (note: this is an arbitrary name).

The execute method is the Schedulable interface's method which needs to be specified because we're implementing the interface and this method calls the instance object's main method which can use the passed-in arguments that have been saved into the object's attribute variables.

public with sharing class CronJob_That_Takes_Parameters implements Schedulable {
    String aString; Integer toBeCalculated;
    @testVisible Integer anInteger; // tip: use @testVisible annotation if you want to avoid hard coding values in the unit test

    public void execute(SchedulableContext ctx) {
        this.main(); // invoke the main method from this schedulabel execute method
    }

    // this is the class constructor
    public CronJob_That_Takes_Parameters(String aString, Integer anInteger) {
        this.aString = aString;
        this.anInteger = anInteger;
        this.toBeCalculated = this.anInteger * this.aString.size() /* you can also use custom settings and/or custom metadata values here */;
    }

    // put in the code for the main process
    public void main() {
        try {
            // main logic

        } catch (Exception ex) {
            // error handling
        }
    }
}

in order to schedule this class use the following code.

    // schedule the cron job
    String aString = 'String value argument', cronExp = '0 0 6 ? * MON-FRI'; // every weekday at 6am
    Integer anInteger = 10;
    CronJob_That_Takes_Parameters cronJob = new CronJob_That_Takes_Parameters(aString, anInteger);
    System.schedule('cron job name goes here!', cronExp, cronJob);
Source Link
Bahman.A
  • 938
  • 4
  • 17
  • 40

Here's my way of passing parameters to a schedulable apex class.

This following class has a constructor that takes two parameters and sets the class attributes to hold the values passed into as the arguments and creates an instance object of the class which will contain the class attributes as well as the class methods. Here we have one method called main (note: this is an arbitrary name).

The execute method is the Schedulable interface's method which needs to be specified because we're implementing the interface and this method calls the instance object's main method which can use the passed-in arguments that have been saved into the object's attribute variables.

public with sharing class CronJob_That_Takes_Parameters implements Schedulable {
String aString; Integer toBeCalculated;
@testVisible Integer anInteger; // tip: use @testVisible annotation if you want to avoid hard coding values in the unit test

public void execute(SchedulableContext ctx) {
    this.main(); // invoke the main method from this schedulabel execute method
}

// this is the class constructor
public CronJob_That_Takes_Parameters(String aString, Integer anInteger) {
    this.aString = aString;
    this.anInteger = anInteger;
    this.toBeCalculated = this.anInteger * this.aString.size() /* you can also use custom settings and/or custom metadata values here */;
}

// put in the code for the main process
public void main() {
    try {
        // main logic

    } catch (Exception ex) {
        // error handling
    }
}

}

in order to schedule this class use the following code.

// schedule the cron job
String aString = 'String value argument', cronExp = '0 0 6 ? * MON-FRI'; // every weekday at 6am
Integer anInteger = 10;
CronJob_That_Takes_Parameters cronJob = new CronJob_That_Takes_Parameters(aString, anInteger);
System.schedule('cron job name goes here!', cronExp, cronJob);