1

I have a "GeoCoderSchedulable" class in a managed package. I want to schedule the class in my salesforce org by code, not by salesforce Scheduler GUI. I used the below code in Anonymous block to schedule but getting the below error - "Line: 1, Column: 1 Enclosing type for global methods in apex classes must be declared as global"

 global class ScheduleGeoCoder implements system.schedulable{
    GeoCoderSchedulable testobj = new GeoCoderSchedulable();
    public String cronexpression = '0 0 0 ? * 1,2,3,4,5,7';
    global void execute(SchedulableContext ctx) {
      System.schedule('Account Geocoding Batch_New2', cronexpression, testobj);
    }
    }

Please let me know where is the issue in my code.

1 Answer 1

3

Anonymous Apex does not support defining new classes, but you don't need a Schedulable to schedule another Schedulable. In fact, that's highly unlikely to get you what you want. To schedule the class GeoCoderSchedulable in Anonymous Apex, you just need to call System.schedule() directly:

System.schedule('Account Geocoding Batch_New2', '0 0 0 ? * 1,2,3,4,5,7', new GeoCoderSchedulable());

You'll need to replace GeoCoderSchedulable with the fully-qualified name of the class, including its namespace.

2
  • When I am running your code I am getting error "Line: 1, Column: 74 Invalid type: GeoCoderSchedulable" Sep 12, 2018 at 11:35
  • 1
    @AritraChakraborty As David said, you need to include the namespace of the class, such as new somens.GeoCoderSchedulable()).
    – sfdcfox
    Sep 12, 2018 at 13:22

You must log in to answer this question.

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