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
RajanRajan 

soql query for Child relationship

Hi Friends,

I have stucked for a SOQL query. Requirement is as below:
I have 3 objects for example A, B and C
A- custom Object
B- Account 
C- Custom Object

A-B- Masterdetail Relationship
B-C - lookup relationship
Their is no relationship between A and C but I have to fetch data using query from Object C.
I have to display C object records for each account. How to write query for this condition?
Balayesu ChilakalapudiBalayesu Chilakalapudi
You can query like this,
 
for(B b:[Select Id,C_lookup__r.name,(select id from A__r) from B]){
    for(A a:b.A__r){
         System.debug("A.Id"+a.Id+'=>'+b.C_lookup__r.name);
    }
}

 
v varaprasadv varaprasad
Hi Rajan,


Please check once below code : 
 
select id,name, (select name from opportunities),(select lastname from contacts) from account
Here Account means B
        opportunities means C
        contacts  means A,

Hope it helps you:
Please let me know In case of any concerns.

Thanks
Varaprasad