Relationship Queries in Salesforce


By using Relationship Queries, we can fetch the Data from multiple associated objects.

  • i.e. Upon fetching the Data from Multiple Objects, instead of using a separate SOQL Query. We can combine all the SOQL Queries into a single one with the help of "Relationship Queries".=
  • i.e. We can fetch the Parent Records and the associated Child records from the Child objects by using a single SOQL Query.
Upon using the Relationship Queries, the objects should be associated with either the "Lookup or Master-Detail" association.

By using Relationship queries, we can reduce the number of "SOQL Queries" inside the transaction, so that we can avoid the exception "Too Many 

By using Relationship Queries, we can fetch the Data with the below combinations.

1. Parent To Child (Standard Relationship)
2. Parent To Child (Custom Relationship)
3. Child To Parent (Standard Relationship)
4. Child To Parent (Custom Relationship)

========================================================================

Parent To Child (Standard).
  • By using this approach, we can fetch the Parent Records and the associated Child records by using a single SOQL Query.
  • The objects should be associated, with the Standard / Readymade association.
  • We can implement these features with the help of "Inner Queries / Sub Queries".
Syntax :

[ Parent SOQL Query ( Child SOQL Query )]

  • To fetch the associated child records, we need to write the "Inner Query", which should be enclosed inside the braces  "( )".
  • In Inner Query, the Child object name should be in "Plural format".
  • We can write one or more Inner Queries inside a single SOQL Query.
  • SOQL supports Only One Level of Inner Queries. (i.e. Inside an Inner Query, we can't write another Inner Query).
Example :

( Select id, firstname, lastname, email, phone from Contacts )
( Select id, name, amount, stagename from Opportunities )
( Select id, subject, description from Cases )

Write an apex program, to All the Associated Contacts, Opportunities, Cases for the Account "Edge Communications".


Child To Parent (Standard):

  • By using Relationship Queries, we can fetch the Data from Child To Parent also. (i.e. Bottom to Top approach)
  • i.e. Upon fetching the Child Record information, we can fetch the associated Parent Record details, and the associated Grand Parent record details, etc.
  • By using the Child To Parent Query, we can traverse the Objects from the Child to the associated parent objects.
  • We can traverse the objects from Bottom to Top up to "5 Levels"

Upon fetching the Parent Record details, we need to Query the Parent Object fields with the below syntax.

Syntax:

<ParentObjectName>.<FieldName>
Ex:
Account.Name
Account.Rating
Account.Active__c
Account.Pan_Number__C

Write an Apex Program, to fetch the Contact Record Details, and the associated Account and Account's Owner details based on the specified Contact Record's Email id.



Parent To Child (Custom Relationship):

  • By using Relationship Queries, we can fetch the Parent Record details along with the associated Child Records based on the required filters, Which have been associated with "Custom Relationship".
  • We can achieve this, with the help of "Inner Queries".
Syntax:

[Parent Object Query ( Child Object Query) ]

Upon writing the Child Object Query (i.e. Inner Query), the Child Object Name should be defined with "Plural Format" and by post-fixing with "__r".
Ex:
Positions__r
Candidates__r
("__r" represents the Custom Relationship)

Write an apex program, to Fetch all the Hiring Manager Records and the associated Position Record details.




Like our post & bookmark this blog for your future learning. 

Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box. 

Happy Coding Sharing Is caring. 😀😀😀 

Post a Comment

Post a Comment (0)

Previous Post Next Post