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
debprakash shawdebprakash shaw 

can Anyone write the test cases

How to write the test cases for this batch class
can anyone write the test class for this batch, I'm  very thankful to him
Thanks in Advance

global class DeleteBulkRecordBatch2 implements Database.Batchable<SObject>, Database.Stateful {
    
    String query = '';
    String sObjectType = '';
    List<String> list_Employee= new List<String>();
    Date FROM_DATE = Null;
    Date toDate = Null;
    Set<Id> set_nonDeleteId = new Set<Id>();
    
    // Constructor
    global DeleteBulkRecordBatch2(String sObjectType, List<String> list_Employee,Date FROM_DATE,Date toDate){
        this.sObjectType = sObjectType;
        this.list_Employee = list_Employee;
        this.FROM_DATE = FROM_DATE;
        this.toDate = toDate;
        
    }
    global Database.QueryLocator start(Database.BatchableContext bc){
        
      
        if(sObjectType == 'Order'){
            if(FROM_DATE == Null && toDate == Null ){
                query='Select Id,account_holder__r.Account__c,account_holder__r.AccountId from Order__c WHERE account_holder__r.AccountId=:list_Employee';
            }else{
                query='Select Id,account_holder__r.Account__c,account_holder__r.AccountId from Order__c WHERE (account_holder__r.AccountId = :list_Employee) AND (NOT (LastmodifiedDate >=:FROM_DATE AND LastmodifiedDate <=: toDate))'; 
            }
        }else if(sObjectType == 'Contact'){
            if(FROM_DATE == Null && toDate == Null ){
                query='Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from Contact WHERE Account.id=:list_Employee';
            }else{
                query='Select Id,Account.id,Account.name,Account__c,Parent_Member_Name__r.id,(Select Id from cases),(select Id from Contact_Products__r) from contact WHERE (Account.id = :list_Employee) AND (NOT(account__c =:set_nonDeleteId)) AND (LastmodifiedDate >=:FROM_DATE AND LastmodifiedDate <=:toDate)';
            }
        }else if(sObjectType=='CustmAccount'){
            if(FROM_DATE == Null && toDate == Null ){
                query='Select Id,Instructing_Party__c,(Select id from Account_Allowances__r) from Account__c WHERE Instructing_Party__c=:list_Employee';
            }else{    
                query='Select Id,Instructing_Party__c,(Select id from Account_Allowances__r) from Account__c WHERE Id NOT IN (Select Account__c From Contact) And (Instructing_Party__c = :list_Employee) AND (NOT(Id=:set_nonDeleteId)) AND (lastModifiedDate >= :FROM_DATE And lastModifiedDate <= :toDate)';
            }
        }
        System.debug(query);
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext bc, List<SObject> s_object){
        System.debug('--execute---');
        System.debug('--set_nonDeleteId---'+set_nonDeleteId);
        System.debug('--list_Employee---'+list_Employee);
        
        if(sObjectType == 'Order') {
            List<order__c> list_Order = new List<order__c>();
            // todo=merge both the for Loop
            for(SObject obj : s_object){
                order__c order  = (order__c)obj;
                list_Order.add(order);
                System.debug('--list_Order---'+list_Order);
            }
            for(Order__c obj_Order : list_Order){
                set_nonDeleteId.add(obj_Order.Account_Holder__r.account__c);
                System.debug('--set_nonDeleteId---'+set_nonDeleteId);
            }
        }else if(sObjectType == 'Contact'){
            Contact[] cont = Database.query(query);
            List<Contact> list_contact = new List<contact>();
            List<Case> list_Case = new List<Case>();
            List<Contact_Products__c> list_Contact_Products = new List<Contact_Products__c>(); 
            for(Contact obj : cont){
                Contact contacts  = (contact)obj;
                list_contact.add(contacts);
                System.debug('--list_contact---'+list_contact);
                for(Case c : obj.cases){
                    Case Cas  = (Case)c;
                    list_Case.add(Cas); 
                    System.debug('--list_Case---'+list_Case);
                }
                for(Contact_Products__c Contact_Products : obj.Contact_Products__r){
                    Contact_Products__c ContactProducts  = (Contact_Products__c)Contact_Products;
                    list_Contact_Products.add(ContactProducts);
                    System.debug('--list_Contact_Products---'+list_Contact_Products);
                }
            }
            delete list_Case;
            delete list_Contact_Products;
            delete list_contact;
            
        }else if(sObjectType== 'CustmAccount'){
            Account__c[] acnt = Database.query(query);
            List<Account__c> list_CustAccount = new List<Account__c>();
            List<Account_Allowances__c> list_AccountAllowences = new List<Account_Allowances__c>();
            for(Account__c obj : acnt){
                Account__c CustAccount  = (Account__c)obj;
                list_CustAccount.add(CustAccount);
                for(Account_Allowances__c AcntAlwnc : obj.Account_Allowances__r){
                    Account_Allowances__c AccountAllowances  = (Account_Allowances__c)AcntAlwnc;
                    list_AccountAllowences.add(AccountAllowances); 
                }
            }
            delete list_AccountAllowences;
            delete list_CustAccount;
        }
        
    }       
    global void finish(Database.BatchableContext bc){
        if (sObjectType == 'Order') {       
            Id batchJobId = Database.executeBatch(new DeleteBulkRecordBatch2('Contact',list_Employee,FROM_DATE,toDate), 1000);
        }else if(sObjectType == 'Contact'){
            Id batchJobId = Database.executeBatch(new DeleteBulkRecordBatch2('CustmAccount',list_Employee,FROM_DATE,toDate), 1000);
        
        }
    }    
    
}
mukesh guptamukesh gupta
Hi Debpraksh,

Have you tried to wirte your side few line of code,  because Community member ready for help but not possible for your scretch development

I hope you understand 

 
debprakash shawdebprakash shaw
Hi Mukesh,
I understand that actually, the challenge is I don't know how to write Test Class
senies12 joesenies12 joe
Great Post! Thank you for visiting here. mysainsburys (https://tractorsinfo.com/mysainsburys/)