+ Start a Discussion
JoshTonksJoshTonks 

Making a class Batch and Schedule Apex Class

Im trying to write a batch apex class that will check a checkbox if it is checked and uncheck them if they are checked. Im stuck on it on a few things I need to make it scheduled and i dont know how to set it so one day it will check them and the next it wont. The last thing is im not sure how to write a test class on this. If anyone can help me with any of this or point me in the right direction i would be grateful.

This is my batch class so far
 
global class FleetAssistUpdaterBatch implements Database.Batchable<sObject>
{
    
   global Database.queryLocator start(Database.BatchableContext ctx )
   {
          
        String str = 'SELECT Id, Trigger_Checkbox__c FROM Fleet_Assist__c WHERE Trigger_Checkbox__c = false';
        
        return Database.getQueryLocator(str);
        
   }
    
    global void execute(Database.BatchableContext ctx, List<Fleet_Assist__c> FAOToProcess)
     {
        
       List<Fleet_Assist__c> FAOList = new List<Fleet_Assist__c>();
       
       for(Fleet_Assist__c FAOObj : FAOToProcess){
               FAOObj.Trigger_Checkbox__c = true;
            FAOList.add(FAOObj);
          }
        
        update FAOList;
     }
   
   global void finish(Database.BatchableContext ctx)
    {
   
      AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,
                          TotalJobItems, CreatedBy.Email
                          FROM AsyncApexJob WHERE Id =
                          :ctx.getJobId()];
   // Send an email to the Apex job's submitter notifying of job completion. 
    
   Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
   String[] toAddresses = new String[] {a.CreatedBy.Email};
   mail.setToAddresses(toAddresses);
   mail.setSubject('Apex Sharing Recalculation ' + a.Status);
   mail.setPlainTextBody
   ('The batch Apex job processed ' + a.TotalJobItems +
   ' batches with '+ a.NumberOfErrors + ' failures.');
   Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
           
    }    
}

This is my test class so far
 
@isTest
private class FleetAssistUpdaterBatchTest {
       
   static testMethod void testSchedule() {

        Test.StartTest();
        
        List<Fleet_Assist__c> FAOList = new List<Fleet_Assist__c>();
        
        for(Integer i = 0; i<20; i++){
            Fleet_Assist__c FAOObj = new Fleet_Assist__c();
            FAOObj.Trigger_Checkbox__c = false;
            FAOObj.Company__c = '0010C000002JsnZ';
            FAOObj.Contact__c = '0030C000001EUiL';
            FAOList.add(FAOObj);
        }
        
        insert FAOList;
        
        FleetAssistUpdaterBatch sh1 = new FleetAssistUpdaterBatch();
        String sch = '0 0 23 * * ?'; 
        // system.schedule(, sch, sh1); 
        
        Test.stopTest();
        
   }

    
}

Ive done scheduled apex class before but not with batchable.
 
Best Answer chosen by JoshTonks
Ajay K DubediAjay K Dubedi
Hi Tonks,
This will schedule your batch class daily at 12 pm.
FleetAssistUpdaterBatch bObj = new FleetAssistUpdaterBatch();
String chronJobId = '0 0 12 1/1 * ? *';
System.schedule('BatchSchedule', chronJobId,  bObj );
And for test class:
@isTest
private class FleetAssistUpdaterBatchTest {
      
   static testMethod void testSchedule() {
 Fleet_Assist__c FAOObj = new Fleet_Assist__c();
 
          FAOObj.Trigger_Checkbox__c = true;
  //You need to put here all required field of Fleet_Assist__c.
 
       
         insert FAOObj ;
Test.StartTest();
FleetAssistUpdaterBatch bObj = new FleetAssistUpdaterBatch();
String chronJobId = '0 0 12 1/1 * ? *';
System.schedule('BatchSchedule', chronJobId,  bObj ); Test.stopTest();
 }  
}

Follow this link also for test class:
https://apexcoder.com/2016/11/08/how-to-write-test-class-for-batch-apex-in-salesforce/
And follow this link for schedule batch class:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi