0
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);
       
        }
    }    
 }

Errors:

  • 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

1
  • Hey, where you're writing obj.cases, obj is a SObject and doesn't have any cases on it. Same for Contact_Products__r. After conversion to Contact, you might well be able to refer to them :-) Jul 15, 2022 at 11:43

2 Answers 2

0

Your this line:

for(Case c : obj.cases){

Just above this line you are typecasting the obj to Contact. And obj is SObject, so it does not have any reference to cases

Try for(Case c : contacts.cases){

Same for the below one for(Contact_Products__c Contact_Products : contacts.Contact_Products__r){

2
  • Thanks for giving me that answer this is very helpful for me
    – sunny roxx
    Jul 15, 2022 at 13:11
  • please give me one more solution to this code ,how to merge both the for Loop if(sObjectType == 'Order') { List<order__c> list_Order = new List<order__c>(); 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); }
    – sunny roxx
    Jul 15, 2022 at 13:15
0

Unfortunately, some Salesforce documentation encourages the use of dynamic - string based - queries in Batchable classes. As in most other places static - compiler checked - queries are the better choice.

So start by changing your start method to use static queries where the compile will report any errors and you can eliminate those errors step by step.

PS

There doesn't at first sight appear to be much common logic between the Contact and Order cases so two separate classes is likely a better way to go with more descriptive names e.g. DeleteContactBatch and ???OrderBatch

Not the answer you're looking for? Browse other questions tagged .