+ Start a Discussion
Sindhu AmbarkarSindhu Ambarkar 

soql query issue for child to parent relationship

I have two custom objects Job_Application_c,review__c
I need to write child to parent relationship soql query.
Please help me
Thanks & Regards,
Sindhu Ambarkar.
Pankaj_GanwaniPankaj_Ganwani
Hi Sindhu,

You can use something like this to fetch the parent field info using child SOQL:
[select Job_Application_r.Id from review__c];

Make sure you specify the correct relationship API name for Job_Application_c lookup field on review__c object.
Mahesh DMahesh D
Hi Sindhu,

If Job_Application__c is the child Object and Review__c is the parent object then
 
List<Job_Application__c> jaList = [Select Id, Name, Review__c, Review__r.Name from Job_Application__c];

If Review__c is the child Object and Job_Application__c is the parent object then
List<Review__c> rList = [Select Id, Name, Job_Application__c, Job_Application__r.Name from Review__c];

Also look into the below links for more information about Relationship Queries:

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

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

https://developer.salesforce.com/page/A_Deeper_look_at_SOQL_and_Relationship_Queries_on_Force.com

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

https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html


Please do let me know if it helps you.

Regards,
Mahesh
Ajinkya DhasAjinkya Dhas
Hi Sindhu,

Please follow the below explanation and examples to solve your problem ☁️⚡️

1. Child To Parent Relationship :

Consider Contact (Child) and Account (Parent) relationship.

In the child to parent relationship, relationship name will be a parent object which means a foreign key is an Account object.
#1 Example :
SELECT Contact.FirstName, Contact.Account.Name From Contact

#2 Example : Write a query to fetch list of Contact and Account names from contact object where Account.Industry = 'Media'
SELECT Id, Name, Account.Name FROM Contact WHERE Account.Industry='Media'

NOTE: When we use relationship name to the custom objects in the SOQL query. We should append ObjectName__r to the relationship name.

For Example :
List<child__c> ch = [SELECT Id, Name, parent__r.FirstName, parent__r.LastName__c from child__c WHERE age__c < 25];

NOTE: When you are writing a query from child to parent relationship always a relation will be master-detail field name (or) lookup field name.

For Example: 
Write a query to fetch a list of the transaction along with Account type and customer names whose transaction type is a deposit. (NOTE: Master-Detail field in the transaction is customer details__c).
List<transaction__c> tr = [SELECT Id, CustomerDetails__r.CName__c, customerDetails__r.AccountType__c, Name FROM Transaction__c];

So these are the ways you can use the child to parent relationship query efficiently.

For More Information Please Follow Below Link To Know More :
https://www.salesforcekid.com/2019/05/salesforce-soql-relationship-queries.html

Wishing You Happy Learning ☁️⚡️
Please Let Me Know If This Helps 

Thank You,
Ajinkya Dhas ☁️⚡️
http://www.salesforcekid.com