Selectid,Account.Name,Account.Phone,Account.industry,Account.Type,Account.Rating,Account.website,Account.Ownership,Account.AnnualRevenue,
Account.NumberOfEmployees,Account.CleanStatus from Contact
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.
- Parent >Child (Custom Object)
I hope it will be helpful.Best Regards
RahulKumar
Refer below links:
https://developer.salesforce.com/forums/?id=9060G000000XbH5QAK
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships.htm
https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html
Let us know if this helps :)
Thanks,
Amit Singh
Please check the link below :
http://www.infallibletechie.com/2013/11/how-to-display-parent-child-and.html
Hope it helps,
Regards,
Roshni
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 :
===============================================================================================================
#2 Example : Write a query to fetch list of Contact and Account names from contact object 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 :
=============================================================================================================== 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).
===============================================================================================================
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