+ Start a Discussion
Todd B.Todd B. 

Trying to Schedule My First Batch Job

So I am trying to create my first batch class, and I am MORE THAN a little confused.  I created two classes.

<pre>
global class BatchUpdate_Financials implements Database.Batchable<sObject>
{
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        String query = 'SELECT Id,Name,status__c FROM Financial_Planning__c ';
        return Database.getQueryLocator(query);
    }

    global void execute(Database.BatchableContext BC, List<Financial_Planning__c> scope)
    {
        for (Financial_Planning__c fp : scope){

         if(fp.status__c == 'Active')
          update fp;

        }
    } 
    global void finish(Database.BatchableContext BC)
    {
    }
}
</pre>

<pre>
global class Scheduler_class implements Schedulable{

    public static String sched = '0 00 00 * * ?';  //Every Day at Midnight

    global static String scheduleMe() {
        Scheduler_class SC = new Scheduler_class();
        return System.schedule('My batch Job', sched, SC);
    }

    global void execute(SchedulableContext sc) {

        BatchUpdate_Financials fp = new BatchUpdate_Financials();
        ID batchprocessid = Database.executeBatch(fp,50);          
    }
}
</pre>

Then I read I have to type something like this in the Developer Console to actually schedule the job.

<pre>
Scheduler_class sch1 = New Scheduler_class();
String sch = '0 00 02 * * ?';
System.Schedulable('Financials Update', sch, sch1);
</pre>

But when i do, I get this error: "Method does not exist or incorrect signature: System.Schedulable(String, String, Scheduler_class)"

Any ideas what I am doing wrong?
Best Answer chosen by Todd B.
Cool GuyCool Guy
Try Like This.

global class Scheduler_class implements Schedulable{

        global void execute(SchedulableContext sc) {

               BatchUpdate_Financials m = new BatchUpdate_Financials();
               database.executeBatch(m);

        }

}


And then schedule this job in developer console like

Scheduler_class m = new Scheduler_class();
String sch = '00 00 08 * * ?';
system.schedule('Financials Update', sch, m);



All Answers

kevin Carotherskevin Carothers
Try 
System.Schedule('Financials Update', sch, sch1);


Cool GuyCool Guy
Try Like This.

global class Scheduler_class implements Schedulable{

        global void execute(SchedulableContext sc) {

               BatchUpdate_Financials m = new BatchUpdate_Financials();
               database.executeBatch(m);

        }

}


And then schedule this job in developer console like

Scheduler_class m = new Scheduler_class();
String sch = '00 00 08 * * ?';
system.schedule('Financials Update', sch, m);



This was selected as the best answer
Todd B.Todd B.
Thanks Cool Guy, that did the trick!
Todd B.Todd B.
Hey @cool guy,

How do you write a test class for a batch job?

Cool GuyCool Guy
@isTest
private class testCaseForBatch
{
    Static testMethod void testCaseMethod()
    {
       Database.QueryLocator QL;
       Database.BatchableContext BC;
       List<Contact> allInseringContacts = new List<Contact>();
       Contact con =new Contact();
            con.lastname='Testing Contact';
            con.MailingCity='Testing City';
            con.MailingState='Testing State';
       allInseringContacts.add(con);
       insert allInseringContacts;
       batchClassName currentContact=new batchClassName(allInseringContacts);
       QL=currentContact.start(BC);
       test.starttest();
           currentContact.execute(bc,allInseringContacts);
           currentContact.finish(bc);
       test.stopTest();
   
   
    }
}