+ Start a Discussion
Malik Butler 5Malik Butler 5 

Need help finishing a batch job

I have a batch job that needs to be finished but I'm not sure how to finish it
global class ReloadCancellation implements Database.Batchable<sObject> {
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'SELECT Subject,Status,SchedEndTime FROM Service Appointment';
        return Database.getQueryLocator(query);
    }

}

. The criteria is "We  would like to cancel any "Reload" service appointments that are Open from the day before.
 
Create batch to query all the service appointments where:
 
  • ServiceAppointment.Subject = “Reload”
  • and ServiceAppointment.Status = “O” (Open)
  • and ServiceAppointment.ScheduledEnd  less than Today
 
Set to ServiceAppointment.Status = “C” (Canceled)
 
We will run batch at 4:00 AM and it will run Under Integration Account." Attached is what I have so far with the code.
PRAKASH JADA 13PRAKASH JADA 13
global class ReloadCancellation implements Database.Batchable<SObject>, Schedulable{
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String subject         = 'Reload';
        String status         = 'O';
        Date scheduledEnd     = Date.today();
        
        String query = 'SELECT Subject,Status,SchedEndTime FROM ServiceAppointment ' + 
            ' WHERE subject = : ' + subject + 
            ' AND status = :' + status + 
            ' AND scheduledEnd < ' + scheduledEnd;
        
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<ServiceAppointment> serviceAppointments) {
        
        // Loop to iterate over the service appointments
        for(ServiceAppointment service : serviceAppointments) {
            service.status = 'C';
        }
        // Preforming the DML operation
        update serviceAppointments;
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
    global void execute(System.SchedulableContext SC) {
        Database.executeBatch(new ReloadCancellation(), 200);
    }
}

Please chang the object name before you save the code. I hope this will help you.
Malik Butler 5Malik Butler 5
Could you possibly help with the test class?
PRAKASH JADA 13PRAKASH JADA 13

Hi Here is the Batch class with test class with 100% code coverage

Note: change the object Name from Contact to your object Name and you can change the batch class name before saving the code in batch class and in test class as well 

In test class chang the object from contact to your object name. To save the contact we need an account so I used the account actually you don't need that.
Hopefully, by making these changes it will work for you.

Batch class:
------------------------
global class UpdateContactBatch implements Database.Batchable<sobject>, Schedulable {
    
    global Database.QueryLocator start(Database.BatchableContext BC) {
        String subject             = 'Reload';
        String status             = 'O';
        Date scheduleEnd        = Date.today();
        
        String query = 'SELECT Subject__c, Status__c, scheduledEnd__c FROM Contact ' + 
            ' WHERE subject__c         = :subject ' + 
            ' AND status__c         = :status ' +
            ' AND scheduledEnd__c     < :scheduleEnd';
        
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> serviceAppointments) {
        
        // Loop to iterate over the service appointments
        for(Contact service : serviceAppointments) {
            service.status__c = 'C';
        }
        // Preforming the DML operation
        if(serviceAppointments.size() > 0) {
            update serviceAppointments;
        }
        
    }
    
    global void finish(Database.BatchableContext BC) {
        
    }
    
    global void execute(System.SchedulableContext SC) {
        Database.executeBatch(new UpdateContactBatch(), 200);
    }
}

Test class:
----------------------
@isTest
public class UpdateContactBatchTest {
    @testSetUp static void prepareTestData() {
        
        Account account = new Account(Name = 'Test Account');
        insert account;

        contact contact = new Contact(LastName = 'Test LastName', FirstName = 'Test FirstName', subject__c = 'Reload',
                                       AccountId = account.Id, status__c = 'O', scheduledEnd__c = Date.today()-1);
        insert contact;

    }
    @isTest static void testMethod1() {
        Account account = [SELECT ID, NAME FROM Account WHERE NAME = 'Test Account'];
        Contact contact = [Select Id FROM Contact where AccountId = :account.Id];
        Test.startTest();
        UpdateContactBatch batch1 = new UpdateContactBatch();
        String sch = '0 0 2 * * ?';
        System.Schedule('Testing the batch class', sch, batch1);
        Test.stopTest();
    }
}