0

I want user of the managed package to be able to run a scheduled job that repeats every hour. The Scheduled Batch class is from the managed package and I'm having trouble accessing it when logged in as a user or subscriber in org where the package is installed. I've seen this question (scheduling a batch class to run every 15 min in managed package), but I'm wondering how to do it without the fancy UI. My end goal is that user/admin where package is installed can schedule it to run hourly and leave it to repeat indefinitely.

Stripped Batch Schedulable class: GetAllRecordsBatch

global with sharing class GetAllRecordsBatch implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful, Schedulable {
public GetAllRecordsBatch() {
}
public Database.QueryLocator start(Database.BatchableContext BC) {

}
public void execute(Database.BatchableContext BC, List<Cognism_Campaign__c> scope) {
    //logic
}
public void finish(Database.BatchableContext BC) {
}
global void execute(SchedulableContext sc){
    GetAllRecordsBatch b = new GetAllRecordsBatch();
    database.executebatch(b, 10); 
}
}

When I try running it:

namespace.GetAllRecordsBatch refresher = new namespace.GetAllRecordsBatch();
// Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year
String sch = '0 31 * * * ?';
String jobID = System.schedule('GetAllRecordsBatch2', sch, refresher);

I get an error message saying:

Method is not visible: void cogn.GetAllRecordsBatch.<init>()

I presume this part is needed to run the schedulable from UI? Because I can schedule it from the UI but that only support daily runs and I need it to run at least hourly:

global void execute(SchedulableContext sc){

I guess I'm missing a part or just need to make some method global, but not sure which one and why? Any help to understand this is appreciated.

1 Answer 1

0

Issue here is contructed of this class is not global. Without global constructor you will not be able to schedule the class from your subscribers code. But if manage package provides any Ui to schedule then you can schedule this class in subscribers org.

So there are 2 solutions:-

  1. Create a Ui from where you can schedule the class in subscribers org.
  2. Make constructor global in next release so that you can schedule on your own.

Only small downside to global constructor is that you cann't change the method declaration once package is released. So if you need to pass new parameters for constructor then you need to create a new global constructor.

4
  • 1. I'm building the package. There is no UI, I would like to get around that if possible. 2. I can make the constructor global. So, you say all I need to do is to change this part to global? public GetAllRecordsBatch() {}
    – Notoc
    Oct 8, 2021 at 11:11
  • yes, global GetAllRecordsBatch() {}. Upgrade subscriber to latest version and you will be good to go. Oct 8, 2021 at 11:12
  • Thank you Manjot, I'll try this out.
    – Notoc
    Oct 8, 2021 at 11:20
  • If this works you can upvote and mark this as correct ans Oct 8, 2021 at 11:21

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.