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
niki s 7niki s 7 

write a soql query on c where a is parent from b and b is parent of c

write a soql query on c  where a is parent from b and b is parent of c 
Surya GSurya G
Hi Niki,

let a= apple__c, b= ball__c, c= cat__c

List<cat__c> catList = [select id, name, ball__r.id, ball__r.name, ball__r.apple__r.id, ball__r.apple_r.name__c from cat__c];
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Niki,
You can take reference from this soql query:-
List<user_three__c> usre3=[select id,name,user_two__r.name,user_two__r.user_one__r.name from user_three__c];
system.debug(usre3[0].name);
system.debug(usre3[0].user_two__r.name);
system.debug(usre3[0].user_two__r.user_one__r.name);
I have tested as well it is working fine.

In case you find any other issue please mention. 
If you find your Solution then mark this as the best answer. 
PriyaPriya (Salesforce Developers) 

Hi Niki,

You have to use the relationship query for eg. see below syntax:-
List<child__c> ch = [SELECT Id, Name, parent__r.FirstName, parent__r.LastName__c from child__c 
 

Kindly refer this article for the more knowledge :- 

https://www.sfdckid.com/2019/05/salesforce-soql-relationship-queries.html

Please mark it as a best answer so that it can help others.

Regards,

Priya Ranjan

niki s 7niki s 7
Thanks everyone