[SalesForce] Error: Method does not exist or incorrect signature: void schedule

Error:

Method does not exist or incorrect signature: void schedule(String, String, BatchEmail) from the type System

I am trying to write a scheduler class which runs every 60 days. I am getting the error above when I try saving it. Not sure what the problem is.

Here is the code:

 global class BatchEmails_Scheduler implements Schedulable {
         public static String sc = '0 0 18 1/60 * ? *'; 

        global static String schedule() {
                BatchEmails be = new BatchEmails(); 
                return system.schedule('Send Emails', sc, be); 
            }

            global void execute(SchedulableContext sc) {
                BatchEmails be1 = new BatchEmails(); 
                ID batchprocessid = database.executeBatch(be1, 100);
            }

    }

Best Answer

You are trying to pass a Batchable implementation where Schedulable is expected. This is just one reason why it is better to roll both into the same class. Regardless, your schedule method should pass in an instance of BatchEmails_Scheduler:

return system.schedule('<batch_name>', '<cron_string>', new BatchEmails_Scheduler());

Typically, rolling them together reduces the number of classes you have to maintain and adds little overhead:

public with sharing class MyBatch implements Database.Batchable<SObject>, Schedulable
{
    public void execute(SchedulableContext context)
    {
        Database.executeBatch(this);
    }
    public Database.QueryLocator start(Database.BatchableContext context)
    {
        // batch implementation
    }
    public void execute(Database.BatchableContext context, List<SObject> records)
    {
        // batch implementation
    }
    public void finish(Database.BatchableContext context)
    {
        // batch implementation
    }
}