0

I have a situation where I need to pass a string to schedulable controller from batch or any apex controller. I see that we can do it using public constructor in schedulable implementation. Please let me know how can I pass a String parameter to schedulable controller. I tried below code but I am getting error.

Dependent class is invalid and needs recompilation: Class BatchControllerQueingCalls : Constructor not defined: [schedulerQueingCalls].Constructor()

global class schedulerQueingCalls implements schedulable{
    global String sObjectName;

    public schedulerQueingCalls(String objName){
        this.sObjectName = objName;
    }
    global void execute(SchedulableContext sc){
        BatchControllerQueingCalls getRecs = new BatchControllerQueingCalls(''+sObjectName);
        database.executeBatch(getRecs);
    }
}

if we do the same in a Batch controller, it is working fine without any issues.

Also, how the global variable values passes to Execute method in scheduler..

2
  • check this one salesforce.stackexchange.com/questions/14634/… Commented Jan 22, 2020 at 8:15
  • I forgot to save batch controller first. I had to commentOut scheduler part in batch controller and save newly edited constructor in schdulable. Below SOLVED answer explains my second question about passing param to execute. Thank you for your efforts.
    – AshSFcloud
    Commented Jan 23, 2020 at 5:38

2 Answers 2

1

With all async, class-based processing, Salesforce allows you to construct and initialize an instance of your async processing class with whatever parameters you need in order to define the state (member variables) you want for the async execution. This state is simply serialized into the database. When it is time to execute, Salesforce must re-create an instance, which it does via deserialization.

As such it is essential that you define a no-arg constructor to support this deserialization. Since you have not, you get this error. Unless you are getting this error due to trying to call a no-arg constructor from within the batch (you didn't include the code so this isn't clear).

NB: the instance's state is defined by whatever it is when you ask Salesforce to queue the invocation. Thus you could use construction and/or post-construction state modification (e.g. property setters) to define the state.

There's no "passing" of "global" variables as such, just assignment to them and usages of them (exactly the same as "public" variables). However, unless you are making the code accessible across a package boundary, there is no need to use the global access modifier in your Apex code.

1

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);

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