1

Has anyone ever come across this error while attempting to schedule a Schedulable Apex Class?

Error: You must select an Apex class that implements the Schedulable interface.

I had previously been able to schedule this class, but now for some reason, it's giving me this error.

Salesforce interface showing the schedulable class and the error that Salesforce is giving when attempting to schedule the class

It's not really making sense to me as the class I'm scheduling hasn't been changed:

global class PrimaryDistrictAccountAutoScheduler implements Schedulable {
    
    global void execute(SchedulableContext ctx) {
        
        Id batchInstanceId = Database.executeBatch(new PrimaryDistrictAccountAutoBatch(true), 50);
        
    }
    
}

I made some changes to the underlying batch and redeployed to this sandbox and that's when the issue arose. But, I ran the test in the development sandbox and again when deploying to this sandbox, so I'm not sure why that would be an issue!?

4
  • I've heard other having this issue intermittently, but no root cause was found AFAIK. Could you compile the apex classes and check if this problem persists?
    – arut
    Commented Sep 11, 2020 at 7:10
  • Just fyi you should pretty much never use global.
    – Adrian Larson
    Commented Sep 11, 2020 at 12:35
  • 2
    I've found that one has to run tests for this schedulable class in the org where you are trying to schedule it to make it appear; happens sometimes for invocable apex as well
    – cropredy
    Commented Sep 11, 2020 at 22:30

2 Answers 2

2

I have seen such issues in the past. The root cause identified was the class has got invalid when the sandbox was refreshed

Classes are stored with an isValid flag that is set to true as long as dependent metadata has not changed since the class was last compiled. If any changes are made to object names or fields that are used in the class, including superficial changes such as edits to an object or field description, or if changes are made to a class that calls this class, the isValid flag is set to false.

Recompile all the classes via Setup | Apex classes | Compile All classes to capture the correct code utilization. Queries that can help determine the apex classes that are invalid.

Select count() from ApexClass where isValid = false
Select Id, Name from ApexClass where isValid = false

Here is what I recommend

  1. Run a SOQL query to check if the Apex Class is Valid

  2. If it isn't, recompile all classes

  3. If it doesn't help, run the code snippet specified here to delete any orphaned jobs

  4. If the issues still persist, delete the class and recreate the class to see if this resolves the issue

  5. If none of these suggestions help, please reach out to support

1
  • Thank you, that totally worked! Commented Sep 13, 2020 at 22:12
1

@cropredy Thanks... Your suggestion worked for me. Running tests for the schedulable class in the org where I was trying to schedule it worked for me and after that the class started appearing.

You must log in to answer this question.

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