-1

I'm looking to pass a variable to my scheulder class when scheduling a batch job. The idea is that I'm scheduling the class multiple times, but passing in a different variable each time.

Example:

String cron = '0 0 0,17 ? * SAT *';
System.schedule('BatchOpportunityOwnerAssignment Daily Package Lead',
    cron, new BatchOpportunityOwnerAssignmentScheduler('Package Lead'));

String cron = '0 0 0,18 ? * SAT *'; 
System.schedule('BatchOpportunityOwnerAssignment Daily Non Package Lead', 
    cron, new BatchOpportunityOwnerAssignmentScheduler('Non Package Lead'));

Here's what my current class looks like now:

global class BatchOpportunityOwnerAssignmentScheduler implements Schedulable {
        global void execute( SchedulableContext sc ) {
            BatchOpportunityOwnerAssignment apBatch = new
 BatchOpportunityOwnerAssignment(VARIABLE HERE);
            database.executebatch(apBatch,50);
    }
}

Thoughts?

1
  • 1
    Please use code blocks, not quotes, to format your code. Right now, it's not clear what you're asking; you can edit to clarify. It looks like you need to write a constructor for your scheduler class.
    – David Reed
    Jan 25 at 16:34
1

Sure, just add a constructor:

global class BatchOpportunityOwnerAssignmentScheduler implements Schedulable {
  String variableHere;
  global BatchOpportunityOwnerAssignmentScheduler(String variableHere) {
    this.variableHere = variableHere;
  }
  global void execute(SchedulableContext context) {
    Database.executeBatch(new BatchOpportunityOwnerAssignment(variableHere), 50);
  }
}

Note: If you're not an ISV, it is recommended that you use public instead of global.

3
  • Don't think you need the global access modifier.
    – Adrian Larson
    Jan 25 at 17:07
  • 2
    @AdrianLarson I made no assumption, it could easily be for an ISV that wants to do this, but definitely worth observing.
    – sfdcfox
    Jan 25 at 17:12
  • Adding the constructor worked. thanks! Jan 25 at 19:40

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.