• debprakash shaw
  • NEWBIE
  • 29 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 14
    Questions
  • 16
    Replies
public class CreateCaseUsingFlow {
    @InvocableMethod(label = 'Attach File')
    public static List<FlowOutput> createFile(List<FlowInput> list_FlowInput) {
        System.debug('list_FlowInput[0].attachment ==>' +list_FlowInput[0].attachment);
        System.debug('list_FlowInput[0].caseId ==>' +list_FlowInput[0].caseId);
        System.debug('list_FlowInput[0].attachmentName ==>' +list_FlowInput[0].attachmentName);

        
        Boolean isFileSuccessfullyCreate = false;

            String attachment = list_FlowInput[0].attachment;
       	    String attachmentName = list_FlowInput[0].attachmentName;
           
            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S';
            conVer.PathOnClient =attachmentName; 
            conVer.Title = attachmentName;
            conVer.VersionData = EncodingUtil.base64Decode(attachment);
            insert conVer;
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
            
            ContentDocumentLink conDocLink = New ContentDocumentLink();
            conDocLink.LinkedEntityId = list_FlowInput[0].caseId; 
            conDocLink.ContentDocumentId = conDocId;  
            conDocLink.shareType = 'V';
            insert conDocLink;
            isFileSuccessfullyCreate = true;
                       
        List<FlowOutput> list_FlowOutput = new List<FlowOutput>();
        FlowOutput obj_FlowOutput = new FlowOutput();
        obj_FlowOutput.isFileSuccessfullyCreate = isFileSuccessfullyCreate;
        list_FlowOutput.add(obj_FlowOutput);
        
        return list_FlowOutput;
    }
    
    public class FlowInput {
        @InvocableVariable(label=' Case Id' required='true')
        public String caseId;
        @InvocableVariable(label='ContentVersion attachment' required='true')
        public String attachment;
        @InvocableVariable(label='ContentVersion attachmentName' required='true')
        public String attachmentName;
    }
    
    public class FlowOutput {
        @InvocableVariable(label='isFileSuccessfullyCreate')
        public Boolean isFileSuccessfullyCreate;
    }
}

how do I write the test class for this code please suggest
thanks 
Hi,
I get a null value when deserialize the JSON

This field value I deserialize.
public class OrderUpdateJSON {

public ContactJson ContactJson;
    public class ContactJson{
        public String referrer_id;
        public String contact_email;
    }
    public static OrderUpdateJSON parse(String json) {
		return (OrderUpdateJSON) System.JSON.deserialize(json, OrderUpdateJSON.class);
	}
	
}
This is my class where I call a Deserialize class for Json2Apex convert
public with sharing class DeletionScript {
    public static void ContactJson(List<Handle_JSON__c> contJson){    
            for(Handle_JSON__c hj:contJson){
                string bdy=hj.JSON__c;
                
                OrderUpdateJSON objMainJason=(OrderUpdateJSON) System.JSON.deserialize(bdy,OrderUpdateJSON.class);

                system.debug('objMainJason------->' + objMainJason); //I get here null value

                OrderUpdateJSON.ContactJson objAH=objMainJason.ContactJson;
                system.debug('objAH==>'+objAH);//i get null value here

                string referrer=objAH.referrer_id;
                string email=objAH.contact_email;            
               
             }
        }
    }

I get null value debug is

User-added imagePlease give me a suggestion to resolve this issue

Thanks in Advanced  

I have three types of contact that are level1, level 2, and level 3, and level 2  and level 3 are linked to level 1
here I want to write a class and I want to filter it based on like
Delete Level 1 Contact which does not have Levels 2 & 3 and level 1 email have 'levelone@gmail.com'

Please give the suggestion on how to write the SOQL for this scenario

Thanks 

 

Hi, every, I'm stuck on my VF page, unable to pass the input value from the VF page to the apex controller.
My Apex controller:==
```
public with sharing class RefundPaymentController {
    public static Id paymentId{get;set;}
    public static Payment__c paymnt {get;set;}
  
    public RefundPaymentController(ApexPages.StandardController st){
        hideButton = false;        
        paymnt = (Payment__c)st.getRecord();
        System.debug('this.paymnt==>'+paymnt);  
      
    }
    public static void refundProcess() {
       
        try{
            hideButton = true;
           paymentId = ApexPages.currentPage().getParameters().get('id');
          
            modalVisibility = false;
            Id purchaseOrderId = null;
           // System.debug('Reason_For_Refund==>'+Reason_For_Refund);
            System.debug('paymentId==>'+paymentId);

            if(paymentId != null){        
                
                System.debug('this.paymnt==>'+paymnt);
                purchaseOrderId=paymnt.Id; 
                if(purchaseOrderId != null){
                    checkRefundEligibility(purchaseOrderId,paymnt);
                }
            }
            hideButton = false;
        }catch(Exception ex){
            hideButton = false;
            System.debug('Exception ==> ' + ex.getMessage());
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR, +'Method: refundProcess ==> ' + CONST_ERROR + ex.getMessage()));
        }
    }

and VF Page==>
<!-- <apex:page StandardController="Payment__c" extensions="RefundPaymentController" action="{!refundProcess}"> -->
<apex:page StandardController="Payment__c" extensions="RefundPaymentController" action="{!checkPermission}">
    <apex:slds />

    <apex:form >
        <apex:pageMessages id="showmsg"></apex:pageMessages>
    
        <div style=" {!if(modalVisibility == True,'display:block', 'display:none')}">
            <div style="height:640px">
                <section role="dialog" tabindex="-1" class="slds-modal slds-modal_x-small slds-fade-in-open" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1">
                    <div class="slds-modal__container">
                        <header class="slds-modal__header">
                            <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Are you sure you want to refund the payment?</h2>
                        </header>
                        <div class="slds-modal__content slds-p-around_medium slds-p-left_large" id="modal-content-id-1">                            
                            <apex:pageBlock >
                                <apex:pageBlockSection columns="1">
                                    <apex:inputField value="{!Payment__c.Reason_For_Refund__c}" label="Reason for refund" required="true" id="Payment__c" style="width:200px" />                                    
                                    <apex:inputField label="Case number" value="{!Payment__c.Case_number__c}"  style="width:200px" />
                                </apex:pageBlockSection>
                            </apex:pageBlock>
                        </div>
                        <div class="slds-modal__footer">
                            <apex:commandButton styleClass="slds-button slds-button_brand" value="Yes" action="{!refundProcess}" onclick="this.onclick=function(){return false;}"/>
                            <apex:commandButton styleClass="slds-button slds-button_brand" value="No" action="{!redirectToPaymentRecord}" disabled="{!hideButton}"/>
                        </div>
                    </div>
                </section>
            </div>
            <div class="slds-backdrop slds-backdrop_open"></div>
        </div>
        <br/>
        <br/>
        <div style=" {!if(modalVisibility == True,'display:none', 'display:block')}">
            <apex:commandButton value="Back To Payment Record" action="{!redirectToPaymentRecord}"/>
        </div>
    </apex:form>

    <script>
        function disableOnSubmit(input) {
            var btn = input;
            setTimeout(function(){ 
                btn.disabled = 'disabled';
                // Use the Salesforce CSS style to make the button appear disabled
                btn.className = 'btnDisabled';
                //btn.value = "Saving..."; 
            }, 50);
        }
    </script> 
</apex:page>

please give me a suggestion to achieve this. Thanks in advance
 
Hi Everyone,
I have One trigger and one handler to call my two batch insert and update batch class. when running the code I get an error(error:>> first error: UNABLE_TO_LOCK_ROW) when I do update and insert at the same time so that I merge the code to UPSERT Batch(insert + update batch). now I don't understand how to modify the handler for upsert batch.
This is the trigger to execute the handler
 
trigger BillingSchemeProductsTrigger on Billing_Scheme_Products__c (after update,after insert) {
    System.debug('Before Exception');

	BillingSchemeProductsTriggerHelper handler = new BillingSchemeProductsTriggerHelper();
    if(BillingSchemeProductsTriggerHelper.skipTrigger==false){ 
        if(Trigger.isInsert && Trigger.isAfter){
            handler.OnAfterInsert(Trigger.new);
        }  
        if(Trigger.isUpdate && Trigger.isAfter){
            handler.OnAfterUpdate(Trigger.oldMap, Trigger.new);
        } 
    }
}
handler class
public class BillingSchemeProductsTriggerHelper {
    public static boolean skipTrigger = false;
	public static String const_UnumReferrerSlug = Label.UNUM_Referrer_Slug; // This is unum_dev in QA
	
	public void OnAfterInsert( Map<ID, Billing_Scheme_Products__c> map_OldBSP,List<Billing_Scheme_Products__c> list_BSP){
		createRelatedRecord(map_OldBSP,list_BSP);
	}
	
    public void OnAfterUpdate(Map<ID, Billing_Scheme_Products__c> map_OldBSP, List<Billing_Scheme_Products__c> list_BSP){
		    updateRelatedRecord(map_OldBSP, list_BSP);
	}
	
	
	public void createRelatedRecord(Map<ID, Billing_Scheme_Products__c> map_OldBSP,List<Billing_Scheme_Products__c> list_BSP){
		Map<Id,Set<Id>> map_BSWithBSP=new Map<Id,Set<Id>>();
		Set<Id> set_TempId = new Set<Id>();
		
		Map<Id,Set<Id>> map_BSWithProductId = new Map<Id,Set<Id>>();
		Map<String,Boolean> map_BSPStatus = new Map<String,Boolean>();
		Map<String,Boolean> map_BSPStatusOnContact = new Map<String,Boolean>();
		Set<Id> set_TempProductId = new Set<Id>();
		String strKey = '';
        for(Billing_Scheme_Products__c bsp:list_BSP){
            if(map_BSWithBSP.containsKey(bsp.Billing_Scheme__c)){
               set_TempId = map_BSWithBSP.get(bsp.Billing_Scheme__c);   
            }else{
			   set_TempId = new Set<Id>();
            }
			set_TempId.add(bsp.Id); 
			map_BSWithBSP.put(bsp.Billing_Scheme__c, set_TempId); 

			if(map_BSWithProductId.containsKey(bsp.Billing_Scheme__c)){
               set_TempProductId = map_BSWithProductId.get(bsp.Billing_Scheme__c);   
            }else{
			   set_TempProductId = new Set<Id>();
            }
			set_TempProductId.add(bsp.Product__c); 
			map_BSWithProductId.put(bsp.Billing_Scheme__c, set_TempProductId); 
			
			strKey = bsp.Billing_Scheme__c + '~' + bsp.Product__c;
			map_BSPStatus.put(strKey, bsp.Inactive__c);
			
			if(bsp.InActive_on_Partner__c){
				map_BSPStatusOnContact.put(strKey + 'Partner', bsp.InActive_on_Partner__c);
			}if(bsp.InActive_on_ChildAbove16__c){
				map_BSPStatusOnContact.put(strKey + 'Over16', bsp.InActive_on_ChildAbove16__c);
			}if(bsp.InActive_on_ChildBelow16__c){
				map_BSPStatusOnContact.put(strKey + 'Under16', bsp.InActive_on_ChildBelow16__c);
			}

			// Added on 16-02-2022
			mapExtraAttributes(map_BSPStatusOnContact, bsp.Extra_Attributes__c, strKey);
        }
		
		if(map_BSWithBSP.size()>0){
			CreatedRelatedAccountAllowancesProducts(map_BSWithBSP);
		}
		
	}
	
	
	public void CreatedRelatedAccountAllowancesProducts(Map<Id,Set<Id>> map_BSWithBSP){
		Map<Id,Id> map_AccountWithAA = new Map<Id,Id>();
		Set<Id> set_BSId = map_BSWithBSP.keySet();
		System.debug('Billing Scheme ==> ' + set_BSId);
		Map<Id, List<Id>> map_BSIdWithListOfCustomAccId = new Map<Id,List<Id>>();
		Map<Id, List<Account_Allowances__c>> map_CustomAccIdWithListOfAA = new Map<Id,List<Account_Allowances__c>>();
		Map<Id, Decimal> map_CustAccIdWithCountToBeSubstracted = new Map<Id, Decimal>();
		
		List<Id> list_CustomAccId = new List<Id>();
		Id billingSchemeId = null;

		for(Account_Allowances__c objAW:[
			SELECT id, Account__c, Account__r.Instructing_Party__r.referrer__r.name, Account__r.Instructing_Party__r.Referrer__r.Slug__c,
			Billing_Scheme__c, Products__r.Name, Max_Allownace__c, Remaining_Allowance__c
			FROM Account_Allowances__c 
			WHERE Billing_Scheme__c!=null 
			AND Billing_Scheme__c IN : set_BSId
		]){
			billingSchemeId = objAW.Billing_Scheme__c;
			map_AccountWithAA.put(objAW.Account__c,objAW.Id); // For one custom account this will store only one AA, done only for mapping of BS
			
			if(objAW.Account__r.Instructing_Party__r.Referrer__r.Slug__c == const_UnumReferrerSlug && objAW.Products__r.Name == 'Psychological Services' && 		objAW.Max_Allownace__c == 8){
				list_CustomAccId.add(objAW.Account__c);

				List<Account_Allowances__c> list_AA = new List<Account_Allowances__c>();
				list_AA.add(objAW);
				map_CustomAccIdWithListOfAA.put(objAW.Account__c, list_AA);
			}
		}

		if(list_CustomAccId.size() > 0){
			map_BSIdWithListOfCustomAccId.put(billingSchemeId, list_CustomAccId);
		}


		//Query and get BSP from BS Id
		// We are changing only one BS in one request
		for(Billing_Scheme_Products__c obj_BSPs : [
			SELECT Id, Units_Allowed__c, Billing_Scheme__c
			FROM Billing_Scheme_Products__c 
			WHERE Product__r.name = 'Psychological Services' AND Billing_Scheme__c IN: map_BSIdWithListOfCustomAccId.keySet()
		]){
			//if 8 already exist then only perform this action
			if(obj_BSPs.Units_Allowed__c == 8){
				if(map_BSIdWithListOfCustomAccId != null && map_BSIdWithListOfCustomAccId.containsKey(obj_BSPs.Billing_Scheme__c) ){
					for(Id customAccountId : map_BSIdWithListOfCustomAccId.get(obj_BSPs.Billing_Scheme__c)){
						if(map_CustomAccIdWithListOfAA != null && map_CustomAccIdWithListOfAA.containsKey(customAccountId) ){
							for(Account_Allowances__c obj_AA : map_CustomAccIdWithListOfAA.get(customAccountId)){
								// only products with mental health would be present here
								// if max allowance is 8 only then calculate remaining allowances for plus
								// we have only one of this kind
								if(obj_AA.Max_Allownace__c == 8){
									Decimal countToBeSubstracted = obj_AA.Max_Allownace__c - obj_AA.Remaining_Allowance__c;
									map_CustAccIdWithCountToBeSubstracted.put(customAccountId, countToBeSubstracted);
									// System.debug('customAccountId --> ' + countToBeSubstracted);
								}
							}
						}
					}
				}
			}
		}
		
		InsertAllowanceRecordBatch objAccBatch = new InsertAllowanceRecordBatch();
		objAccBatch.map_BSWithBSP = map_BSWithBSP;
		objAccBatch.map_AccountWithAA = map_AccountWithAA;
		objAccBatch.map_CustAccIdWithCountToBeSubstracted = map_CustAccIdWithCountToBeSubstracted;
		Database.executeBatch(objAccBatch, 500);
	}
    public void updateRelatedRecord(Map<ID, Billing_Scheme_Products__c>  map_OldBSP, List<Billing_Scheme_Products__c> list_BSP){
		Map<Id, Set<Id>> map_BS_Products = new Map<Id, Set<Id>>();
		Set<Id> set_BillingSchemeId = new Set<Id>();
		Set<Id> set_TempProduct = new Set<Id>();
        for(Billing_Scheme_Products__c objBS:list_BSP){
			String newExtraAttribute = objBS.Extra_Attributes__c;
			//get Old Billing_Scheme_Products
            Billing_Scheme_Products__c objOldBSP=map_OldBSP.get(objBS.id);
			String oldExtraAttribute = objOldBSP.Extra_Attributes__c;
            
			// Add Check for Extra attribute is changed
			if(objBS.Inactive__c!=objOldBSP.Inactive__c 
				|| objBS.InActive_on_Partner__c!=objOldBSP.InActive_on_Partner__c 
				|| objBS.InActive_on_ChildAbove16__c!=objOldBSP.InActive_on_ChildAbove16__c 
				|| objBS.InActive_on_ChildBelow16__c!=objOldBSP.InActive_on_ChildBelow16__c
				|| newExtraAttribute != oldExtraAttribute){

				if(map_BS_Products.containsKey(objBS.Billing_Scheme__c)){
					set_TempProduct = map_BS_Products.get(objBS.Billing_Scheme__c);
				}else{
					set_TempProduct = new Set<Id>();
				}
				set_TempProduct.add(objBS.Product__c);
                map_BS_Products.put(objBS.Billing_Scheme__c, set_TempProduct);
                set_BillingSchemeId.add(objBS.Billing_Scheme__c);
            }
        }
		
        system.debug('map_BS_Products----->' + map_BS_Products);
		if(map_BS_Products!=null && map_BS_Products.size()>0){
			System.debug('Run UpdateAllowanceRecordBatch on Billing Scheme Product Update');
			UpdateAllowanceRecordBatch objAccBatch = new UpdateAllowanceRecordBatch();
			objAccBatch.set_BillingSchemeId = set_BillingSchemeId;
			objAccBatch.map_BS_Products = map_BS_Products;
			objAccBatch.strAction = 'Update Billing Scheme Product';
            Database.executeBatch(objAccBatch, 500);			
        
		}
    }

	public static void mapExtraAttributes(Map<String,Boolean> map_BSPStatusOnContact, String extraAttributes, String strKey){
		// If we have Extra Attributes then use this else continue what is going till now, All Projects
		// this is only for child Contact Product, Active/Inactive of Members
		if(String.isNotBlank(extraAttributes)){
			// convert Json to map
			Map<String, Object> map_JsonEntries = (Map<String, Object>) JSON.deserializeUntyped(extraAttributes);
			Decimal childAllowedForMinAge = (Decimal) map_JsonEntries.get('childMinAge');
			Boolean isChildAllowed = (Boolean) map_JsonEntries.get('isChildAllowed');
			System.debug(childAllowedForMinAge + ' - ' + isChildAllowed);
			map_BSPStatusOnContact.put(strKey + 'FROM_EXTRA_ATTRIBUTES~' + childAllowedForMinAge, isChildAllowed);
		}
	}
}

now see the handler line number 126/163 here call the insert batch or update batch,i want to merge the insert or update batch to upsertAccountAllowences ,I already merge the Update or Insert batch to Upsert batch I want only modify the handler

Thanks in advance 

Hi everyone,

I want to know how to enable a field audit trail in salesforce org

Thanks in Advance 

My batch class query
```soql
query='Select Id,Account.id,Account.name,Account__c,Member_Level__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 (LastmodifiedDate >=:fromDate AND LastmodifiedDate <=:toDate)';
```

Batch execute method>>>>
```java
if(sObjectType == 'Contact'){
            List<Contact> list_contact = new List<contact>();
            List<Contact> lst_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 : list_Sobject){
                Contact contacts  = (contact)obj;
                list_contact.add(contacts);
                for(Case c : contacts.cases){
                    Case Cas  = (Case)c;
                    list_Case.add(Cas);     
                }
                for (Contact cntc : [select id,name from contact where Parent_Member_Name__r.id IN:list_contact]) {   //Parent_Member_Name__c its lookup field
                    Contact cntact  = (Contact)cntc;
                    lst_contact.add(cntact);
                }
                for(Contact_Products__c Contact_Products : contacts.Contact_Products__r){
                    Contact_Products__c ContactProducts  = (Contact_Products__c)Contact_Products;
                    list_Contact_Products.add(ContactProducts);   
                }
            }
            delete list_Case;
            delete list_Contact_Products;
            delete lst_contact; 
            delete list_contact;
```

in my query the Member_Level__c is a picklist field her value like
level-1,level-2,level-3

and my execute method have "Parent_Member_Name__c" this is a lookup field of contact(selflookup) and always Level- 2 or 3 are connected to level-1 with the help of this field

In my org contact means family, like :- Level-1 = Parent (main member),Level-2=semi parent(main member wife),Level-3=child(child of main member) 

Here I'm trying, when I delete level-1 contact delete also level- 2 or 3 
please guide me how to do that ,that is very helpful for me 

Thanks in Advance

picklist field name = Member_Level__c

value= Level 1 - Parent Account Holder
            Level 2 - Adult Secondary Account
            Level 3 - Child Dependent Account
 

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

 

I try this query in batch class but when running the code I get an error "System.QueryException: unexpected token: '1' "

please tell me how to fix that

Thanks in Advance
 

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 
 
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 
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);
        
        }
    }    
    
}
I want to call the same batch class in the same batch. Please give me the syntax of how to call in the finish method.

Thanks in Advance
Hi,
I want to retrieve the record which is not on those dates like (Form_Date to To_Date )

Thanks in advance 
How to delete more than 50k records using batch class
an object like Account link with contact, and contact link with product, and product link with order
All the objects have more than 50k record
How to write the batch class for that scenario
thanks in advance
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);
        
        }
    }    
    
}
public class CreateCaseUsingFlow {
    @InvocableMethod(label = 'Attach File')
    public static List<FlowOutput> createFile(List<FlowInput> list_FlowInput) {
        System.debug('list_FlowInput[0].attachment ==>' +list_FlowInput[0].attachment);
        System.debug('list_FlowInput[0].caseId ==>' +list_FlowInput[0].caseId);
        System.debug('list_FlowInput[0].attachmentName ==>' +list_FlowInput[0].attachmentName);

        
        Boolean isFileSuccessfullyCreate = false;

            String attachment = list_FlowInput[0].attachment;
       	    String attachmentName = list_FlowInput[0].attachmentName;
           
            ContentVersion conVer = new ContentVersion();
            conVer.ContentLocation = 'S';
            conVer.PathOnClient =attachmentName; 
            conVer.Title = attachmentName;
            conVer.VersionData = EncodingUtil.base64Decode(attachment);
            insert conVer;
            
            Id conDocId = [SELECT ContentDocumentId FROM ContentVersion WHERE Id =:conVer.Id].ContentDocumentId;
            
            ContentDocumentLink conDocLink = New ContentDocumentLink();
            conDocLink.LinkedEntityId = list_FlowInput[0].caseId; 
            conDocLink.ContentDocumentId = conDocId;  
            conDocLink.shareType = 'V';
            insert conDocLink;
            isFileSuccessfullyCreate = true;
                       
        List<FlowOutput> list_FlowOutput = new List<FlowOutput>();
        FlowOutput obj_FlowOutput = new FlowOutput();
        obj_FlowOutput.isFileSuccessfullyCreate = isFileSuccessfullyCreate;
        list_FlowOutput.add(obj_FlowOutput);
        
        return list_FlowOutput;
    }
    
    public class FlowInput {
        @InvocableVariable(label=' Case Id' required='true')
        public String caseId;
        @InvocableVariable(label='ContentVersion attachment' required='true')
        public String attachment;
        @InvocableVariable(label='ContentVersion attachmentName' required='true')
        public String attachmentName;
    }
    
    public class FlowOutput {
        @InvocableVariable(label='isFileSuccessfullyCreate')
        public Boolean isFileSuccessfullyCreate;
    }
}

how do I write the test class for this code please suggest
thanks 

I have three types of contact that are level1, level 2, and level 3, and level 2  and level 3 are linked to level 1
here I want to write a class and I want to filter it based on like
Delete Level 1 Contact which does not have Levels 2 & 3 and level 1 email have 'levelone@gmail.com'

Please give the suggestion on how to write the SOQL for this scenario

Thanks 

 

Hi everyone,

I want to know how to enable a field audit trail in salesforce org

Thanks in Advance 

My batch class query
```soql
query='Select Id,Account.id,Account.name,Account__c,Member_Level__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 (LastmodifiedDate >=:fromDate AND LastmodifiedDate <=:toDate)';
```

Batch execute method>>>>
```java
if(sObjectType == 'Contact'){
            List<Contact> list_contact = new List<contact>();
            List<Contact> lst_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 : list_Sobject){
                Contact contacts  = (contact)obj;
                list_contact.add(contacts);
                for(Case c : contacts.cases){
                    Case Cas  = (Case)c;
                    list_Case.add(Cas);     
                }
                for (Contact cntc : [select id,name from contact where Parent_Member_Name__r.id IN:list_contact]) {   //Parent_Member_Name__c its lookup field
                    Contact cntact  = (Contact)cntc;
                    lst_contact.add(cntact);
                }
                for(Contact_Products__c Contact_Products : contacts.Contact_Products__r){
                    Contact_Products__c ContactProducts  = (Contact_Products__c)Contact_Products;
                    list_Contact_Products.add(ContactProducts);   
                }
            }
            delete list_Case;
            delete list_Contact_Products;
            delete lst_contact; 
            delete list_contact;
```

in my query the Member_Level__c is a picklist field her value like
level-1,level-2,level-3

and my execute method have "Parent_Member_Name__c" this is a lookup field of contact(selflookup) and always Level- 2 or 3 are connected to level-1 with the help of this field

In my org contact means family, like :- Level-1 = Parent (main member),Level-2=semi parent(main member wife),Level-3=child(child of main member) 

Here I'm trying, when I delete level-1 contact delete also level- 2 or 3 
please guide me how to do that ,that is very helpful for me 

Thanks in Advance

picklist field name = Member_Level__c

value= Level 1 - Parent Account Holder
            Level 2 - Adult Secondary Account
            Level 3 - Child Dependent Account
 

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

 

I try this query in batch class but when running the code I get an error "System.QueryException: unexpected token: '1' "

please tell me how to fix that

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
    //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 
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);
        
        }
    }    
    
}
Hi,
I want to retrieve the record which is not on those dates like (Form_Date to To_Date )

Thanks in advance 
How to delete more than 50k records using batch class
an object like Account link with contact, and contact link with product, and product link with order
All the objects have more than 50k record
How to write the batch class for that scenario
thanks in advance