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
Uttpal chandraUttpal chandra 

Child1 to parent to child2 SOQL query

Hi all,

Currently i am on a child1 and i want to write a query anyone know how to do it?
Deepali KulshresthaDeepali Kulshrestha
Hi Uttpal,
Suppose Child1 is Contact1(whose Id is Id1), Child2 is Contact2 and Parent is Account.
List<Contact> listContact1=[SELECT Id,AccountId FROM Contact WHERE Id=Id1];
Set<Id> accIdSet=new Set<Id>();
for(Contact itr:listContact){
List<Contact> listContact2=[SELECT Id FROM Contact WHERE AccountId IN accIdSet];
In this set you will get Ids of Contacts that are children of our Account.From this you can get your Child2. 

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
sfdc X-Mansfdc X-Man
Hi Uttpal,
Suppose Child1 is Contact1(whose Id is Id1), Child2 is Opportunity and Parent is Account.
String conId='0032v00002iNNsB';
List<Contact> conList = new List<Contact>();
conList=[SELECT Id,AccountId FROM Contact where Id=:conId];
Set<Id> sid=new Set<Id>();
    for(Contact con :conList){
List<Opportunity> oppList = new List<Opportunity>();
oppList=[SELECT Id FROM Opportunity WHERE AccountId IN:sid];
if this answer is suitable for you please choose the best aswer,
sfdc x-man