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
Rahul22Rahul22 

SOQL Query on child related records

Hello Everyone,

I am new to salesforce development and need help on SOQL query.

We follow account hierarchy (Parent-Child relationship) and all opportunities which we create are associated with child accounts. Need help in querying all the opportunities of the child records which I want to show on parent record.

Regards,
Rahul

 
ManojjenaManojjena
HI Rahul ,

In salesforce we have two different types of relationship query .

In salesforce Account is parent and Opportunity is child .  There are two way parent to child and child to parent .

If parent to child then you need to write inner query .
If child to parent then you need to use relationship names in query .

Please check below link it will help .


https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_query_using.htm#sforce_api_calls_soql_relationships_query_using

Let me know incase you have any issue .

Thanks
Manoj
Rahul22Rahul22
Hi Manoj,

Thanks for the info.

Actually, I want to query related objects of the child object on the parent object.

Here is the scenario:

Parent Account : A
Associated Child Accounts : Child A, Child B
Opportunities are associated with child A and B. 

Now, I want to write a query which provides all the opportunities associated with child accounts (Child A&B) of parent account A.
My actual requirement is to display all the child accounts opportunities, tasks on the parent account related list using VF page.

Regards,
Rahul



 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Rahul,

May I request you please check the Salesforce documentation on  "SOQL Relationship Queries" check below link. Basic Examples of SOQL RELATIONSHIPS:
Child > Parent (Standard Object)
Selectid,Account.Name,Account.Phone,Account.industry,Account.Type,Account.Rating,Account.website,Account.Ownership,Account.AnnualRevenue,Account.NumberOfEmployees,Account.CleanStatus from Contact
Child >Parent(Custom Object)
Selectid,COLLEGE__r.Name,COLLEGE__r.Contact__c,COLLEGE__r.Count__c,COLLEGE__r.Highest_Marks__c,COLLEGE__r.Address__cfrom Studnt__c

Parent >Child(Standard object)
select Name, Industry, (select AssistantName, Email from contacts)from ACCOUNT
Parent >Child (Custom Object)
1select id,Name,(select Studnt__c.name__c from Studnts__r) from College__C

let us know if it helps.

Thanks
Rahul Kumar