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 fetch record from selflookup sobject record

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
AnkaiahAnkaiah (Salesforce Developers) 
Hi debprakash,

Level2 & level3 records are linked to Level1 record?

Thanks!!
debprakash shawdebprakash shaw
yes
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

try with below trigger.
trigger deleterelatedcontacts on contact(before delete){

set<id> parentids = new set<id>();

for(contact con:trigger){

parentids.add(con.id);

}

List<contact> contactlist = [select id from contact where Parent_Member_Name__c=:parentids];

Delete contactlist;


}

If this helps, Please mark it as best answer.

Thanks!!