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 

how to delete record in Contact field have other picklist value / how to fetch the record from my contact query which "Member_Level__c = Level 2 or level-3" which are related to level 1 contact

My batch Class:-
```java
global with sharing 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
    //sobject type = Employeer(account),Contact
    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(fromDate == null && toDate == null ){
                query='Select Id,account_holder__r.Account__c,account_holder__r.AccountId from Order__c WHERE account_holder__r.AccountId IN:list_Employer';
            }else{
                query='Select Id,account_holder__r.Account__c,account_holder__r.AccountId from Order__c WHERE account_holder__r.AccountId IN:list_Employer AND (NOT (CreatedDate >=:fromDate AND CreatedDate <=: toDate))'; 
            }
        }else if(sObjectType == 'Contact'){
            if(fromDate == 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 AccountId IN :list_Employer';
            }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 Member_Level__c=:Level 1 - Parent Account Holder And AccountId IN :list_Employer AND account__c NOT IN :set_NonDeleteId AND CreatedDate >=:fromDate AND CreatedDate <=: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') {
            for(SObject obj : s_object){
                order__c order  = (order__c)obj;
                if(order != null){
                    set_nonDeleteId.add(order.Account_Holder__r.account__c); 
                }
                System.debug('--set_nonDeleteId---'+set_nonDeleteId);
            }
}else if(sObjectType == 'Contact'){
            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(SObject obj : s_object){
                Contact contacts  = (contact)obj;
                list_contact.add(contacts);
                System.debug('--list_contact---'+list_contact);
                for(Case c : contacts.cases){
                    Case Cas = (Case)c;
                    list_Case.add(Cas); 
                    System.debug('--list_Case---'+list_Case);
                }

                for(Contact_Products__c Contact_Products : contacts.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;
        }
    }
    global void finish(Database.BatchableContext bc){
        if (sObjectType == 'Order') {       
            Id batchJobId = Database.executeBatch(new DeleteBulkRecordBatch2('Contact',list_Employee,FROM_DATE,toDate), 100);
        }
    }
}
```
In this class I have a contact picklist field Member_Level__c have three value like
1.    Level 1 - Parent Account Holder
2.  Level 2 - Adult Secondary Account
3.    Level 3 - Child Dependent Account
and Contact have a selflookup field name- Parent_Member_Name__c here connect with those contact which have "Member_Level__c='Level 1 - Parent Account Holder'"
In my batch class contact query I filter that which "Member_Level__c='Level 1 - Parent Account Holder" those have store in list and Delete that list_contact
now I want when i delete list_contact Delete as well as those contact which are related to "Member_Level__c='Level 1 - Parent Account Holder" 

or how to fetch the record from my contact query which "Member_Level__c = Level 2 or level-3" which are related to level 1 contact

thanks in advance