function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SFAdmin5SFAdmin5 

Need Schedulable trigger for batch class

Hi-

 

I've got a batch class that works the way I want it to here, but I can't get the schedulable class that I need to schedule this job to run to work.  The batch class works, but when I try to save the schedulable class, I can't and get this error "Constructor Not Defined".  Any idea what I need to do to fix this?

 

Batch class

global class deleteAccounts implements  Database.Batchable<sObject> {
global final string query;
global deleteAccounts (String q)
{
   query = q;
}
global Database.QueryLocator start(Database.BatchableContext BC){
   return Database.getQueryLocator([SELECT id FROM Account WHERE Name ='test']);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
  delete scope;
}
global void finish(Database.BatchableContext BC){
  // Get the ID of the AsyncApexJob representing this batch job  
  // from Database.BatchableContext.    
  // Query the AsyncApexJob object to retrieve the current job's information.  
 }}

 Schedulable class that won't save (I get Contructor Not Defined error):

 

global class scheduledBatchable implements Schedulable{
   global void execute(SchedulableContext sc) {
      deleteAccounts x = new deleteAccounts(); 
      database.executebatch(x);
   }
}

 Thanks!

SidharthSidharth

In your Schedule class you trying to access the default batch constructor (with no arguments) which is not present in the batch class.

Instead in the batch class you have constructor with one argument.

 

- remove argument from constructor ( batch line 3 )

 

SFAdmin5SFAdmin5

Yeahj I found that out before I read this but you are correct.  The worrking batch job, which I've also made schedulable, now works fine.  Problem for me now is figuring out the test class for this thing.

 

In case anyone needs a schedulable batch job that deletes records based on some criteria...here's a working class

 

global class deleteAccounts implements  Database.Batchable<sObject>, Schedulable {
String query = 'SELECT id from Account WHERE Name=test';
global deleteAccounts ()
{
}
global void execute(SchedulableContext SC) {
      deleteAccounts x = new deleteAccounts(); 
      database.executebatch(x);
   }
global Database.QueryLocator start(Database.BatchableContext BC){
   return Database.getQueryLocator([SELECT id FROM Account WHERE Name ='test']);
}
global void execute(Database.BatchableContext BC, List<sObject> scope){
  delete scope;
}
global void finish(Database.BatchableContext BC){

 }}

 

SidharthSidharth

Making test class for Batch is pretty simple. Look at this example:

 

global static testmethod void testBatch() {

Banks__c[] b1 = new Banks__c[]{
new Banks__c(Temp_Date__c = '072599'),
new Banks__c(Temp_Date__c = '072585')
};
insert b1;

batch_bankParseDate batch = new batch_bankParseDate();

Test.startTest();
batch.query += ' where Id = \'' + b1[0].id + '\'';
batch.query += ' OR Id = \'' + b1[1].Id + '\'';
System.debug('##### query: ' + batch.query);
Database.executeBatch(batch);
Test.stopTest();

List<Banks__c> b2 = [Select Id, Temp_Date__c, Change_Date__c from Banks__c where id = :b1[0].id or id = :b1[1].id or id = :b1[2].id or id = :b1[3].id or id = :b1[4].id];


system.assertEquals(Date.valueof('2005-07-25 00:00:00'), b2[0].Change_Date__c);
system.assertEquals(Date.valueof('2005-07-25 00:00:00'), b2[1].Change_Date__c);

}

 

Create sample date. Make dynamic query and execute it. Query the result and Assert/validate it.