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
Sid LightningSid Lightning 

Need help in writting Test Classes

Hi,
I have a test class that deletes custom object record  at the end of day if the value of Grand Total field is 0.

Grand Total field is a roll up field, and is calculated basis the line item value.

Here is my batch class.

global class ProductInterestDeleteBatch implements Database.Batchable<sObject>,Schedulable{
    
    global void execute(SchedulableContext sc){
        ProductInterestDeleteBatch bc = new ProductInterestDeleteBatch();
        database.executeBatch(bc);
    }
    global Database.QueryLocator start(Database.BatchableContext bc){
        return database.getQueryLocator([SELECT Id FROM Product_Interest__c WHERE Grand_Total__c=0]);
    }
    global void execute(Database.BatchableContext bc,List<Product_Interest__c> productInterestList){
        if(!productInterestList.isEmpty()){
            database.delete(productInterestList,false);
        }
    }
    global void finish(Database.BatchableContext bc){
        
    }

}



I need help in writting test class for the same.

Khan AnasKhan Anas (Salesforce Developers) 
Hi Sid,

Greetings to you!

Please try the below code, I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
 
@isTest
public class Test_ProductInterestDeleteBatch {
    
    static testmethod void TestMethod1(){
        Product_Interest__c pi = new Product_Interest__c();
        pi.Name = 'Test Prod';
        // Add other required fields
        INSERT pi;
        
        Test.startTest();
        
        ProductInterestDeleteBatch obj = new ProductInterestDeleteBatch();
        
        String cronExpr = '0 0 0 3 9 ? 2022';
        String jobId = System.schedule('myJobTestJobName', cronExpr, obj);
        obj.execute(null);
        
        DataBase.executeBatch(obj); 
        
        Test.stopTest();
    }
}

I hope it helps you.

Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.

Thanks and Regards,
Khan Anas
Deepali KulshresthaDeepali Kulshrestha
Hi Sid,

Please check below code
 
@isTest
public class ProductInterestDeleteBatch_Test {
    
    @isTest private static void testProductInterestDeleteBatch()
    {
        Product_Interest__c prdInt=new Product_Interest__c();
        prdInt.Name='test';
        prdInt.Grand_Total__c=0;       
        insert prdInt;
        
        
        Test.startTest();  
        
        ProductInterestDeleteBatch obj = new ProductInterestDeleteBatch();
        DataBase.executeBatch(obj); 
        
        ProductInterestDeleteBatch m = new ProductInterestDeleteBatch();
        String sch = '20 30 8 10 2 ?';
        String jobID = system.schedule('Merge Job', sch, m);
        
        Test.stopTest();
    }
}


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

Thanks and Regards,
Deepali Kulshrestha
www.kdeepali.com