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

  • 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 Jan 23 at 5:38
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.

| improve this answer | |

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.