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
HoysalaHoysala 

Test class for this anyone help

global class DS_CallsStatusUpdate implements Database.Batchable<sObject>,Schedulable
{
    global Database.queryLocator start(Database.BatchableContext bc)
{
        return Database.getQueryLocator('SELECT Id,OCE__Account__c,OCE__CallDateTime__c,OCE__Status__c FROM OCE__Call__c WHERE OCE__Status__c =\'draft\' AND OCE__CallDateTime__c<LAST_N_DAYS:2');
}
 global void execute(Database.BatchableContext bc, List<OCE__Call__c> scope)
    {
        List<OCE__Call__c> callLst = new List<OCE__Call__c>();
        for(OCE__Call__c ca : scope)
        { 
            ca.OCE__Status__c = 'Submitted';
            callLst.add(ca);
        }   
        system.debug('calls--'+callLst);
        UPDATE callLst; 
    }

 
Dushyant SonwarDushyant Sonwar
You need to create test data of OCE Call then call Database.executebatch to cover your apex code.You can check the below url for how to cover code of a batch class.

http://www.infallibletechie.com/2013/11/test-class-for-batch-apex-in-salesforce.html

Hope this helps.
Ajay K DubediAjay K Dubedi
Hi Hoysala,
Firstly you have to add the finish method in your batch class.
Follow this batch class:
global class DS_CallsStatusUpdate implements Database.Batchable<sObject>
{
    global Database.queryLocator start(Database.BatchableContext bc)
    {
        return Database.getQueryLocator('SELECT Id,OCE_Account__c,OCE_CallDateTime__c,OCE_Status__c FROM OCE_Call__c WHERE OCE_Status__c =\'draft\' AND OCE_CallDateTime__c<LAST_N_DAYS:2');
    }
    global void execute(Database.BatchableContext bc, List<OCE_Call__c> scope)
    {
        List<OCE_Call__c> callLst = new List<OCE_Call__c>();
        for(OCE_Call__c ca : scope)
        {
            ca.OCE_Status__c = 'Submitted';
            callLst.add(ca);
        }   
        system.debug('calls--'+callLst);
        UPDATE callLst;
    }
    global void finish(Database.BatchableContext BC){
        
    }
}
Test Class:
@isTest
private class DS_CallsStatusUpdate_Test {
    @isTest static void testDS_CallsStatusUpdate() {
        OCE_Call__c obj = new OCE_Call__c();
        obj.Name = 'Test';
        obj.OCE_Account__c = 'TestAccount';
        //If OCE_Account__c is a lookup field
        //then create a record of that Object and put the id as below.
        // obj.OCE_Account__c = Field.Id;
        Obj.OCE_CallDateTime__c = DateTime.newInstance(2019,05,5);
        obj.OCE_Status__c = 'draft';
        insert obj;
 
        Test.startTest();

            DS_CallsStatusUpdate o = new DS_CallsStatusUpdate();
            DataBase.executeBatch(o);
            
        Test.stopTest();
    }
}
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi