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 retrieve string query to execute method in batch class

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
    //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(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__r.Instructing_Party__c =:list_Employee) OR (Account.id = :list_Employee)) AND (NOT(account__c =: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'){
            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 : 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;
            
        }
        
    }       
    global void finish(Database.BatchableContext bc){
        if (sObjectType == 'Order') {       
            Id batchJobId = Database.executeBatch(new DeleteBulkRecordBatch2('Contact',list_Employee,FROM_DATE,toDate), 1000);
       
        }
    }    
    
}

Variables does not exist: cases
Variables does not exist: Contact_Products__r


these two errors are coming in this code, how to resolve this
please tell me where I'm missing
Thanks in advance 
AnkaiahAnkaiah (Salesforce Developers) 
Hi debprakash,

Your code is looks fine.
Can you run the  below query in developer console and check.
'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';

Thanks!!
debprakash shawdebprakash shaw
Hi Ankaiah,
I run the query in the developer console, The query is okay
and my code comes errors like-
Variables does not exist: cases
Variables does not exist: Contact_Products__r

Thanks 
saniez joe 2saniez joe 2
Execute Method performs an operation which we want to perform on the records fetched from start method. Finish method executes after all batches are processed. Orthocarolina Patient Portal Login (https://patientportallogin.onl/orthocarolina-patient-portal-login/)