Salesforce SOQL | Relationship Queries - Part 2

Salesforce SOQL Relationship Queries

Salesforce SOQL Relationship Queries

In the previous Episode, we have discussed Salesforce SOQL Basics with simple Examples.

In this Episode, we are gonna discuss Salesforce SOQL Relationship Queries on standard and custom object.

So let's get started When you want to write queries based on parent-child relationship queries on standard and custom object we will use Salesforce SOQL Relationship Queries.

There are two types of relationship queries :
1. Child To Parent Relationship
2. Parent To Child Relationship


1. Child To Parent Relationship :

Consider Contact (Child) and Account (Parent) relationship.
=============================================
Salesforce SOQL Relationship Queries
=============================================

In the above diagram contact is a child of the Account object.
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 a child to parent relationship query efficiently.
Now Let's Discuss Parent to child relationship on both standard as well as a custom object.

Parent To Child Relationship :

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

Salesforce SOQL Relationship Queries

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

When you are trying to write a query to refer the child object records from the parent object then the child object child  object name would be the relationship name.

Parent To Child Relationship On Standard
The child object name should be referred from with plural name.
Syntax :
=============================================
SELECT Account.Name,(SELECT Contact.FirstName, Contact.LastName FROM Account.contacts) FROM Account
=============================================

Now let's understand with some examples on this.
#1 Example :
=============================================
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
=============================================

#2 Example :
=============================================
SELECT Account, Id, Name, (SELECT Quantity, ListPrice, PricebookEntry.unitprice, pricebookEntry.Name FROM OpportunityLineItems) FROM Opportunity
=============================================

#3 Example :
=============================================
SELECT Amount, Id, Name, (SELECT Quantity, Listprice, priceBookEntry.unitprice, priceBookEntry.Name, priceBookEntry.product2.family FROM OpportunityLineItems) Opportunity
=============================================

When we will use these queries in triggers and apex classes you will understand when and how to use relationship queries in the next episodes.

Now let's understand Parent To Child Relationship Custom Objects.

Parent To Child Relationship Custom Objects 
When you are writing a query to refer custom child object fields from the parent to child relationship then we have to append child object plural name__r.
Syntax :
=============================================
[SELECT ParentName__c, Age__c, (SELECT Child_Name__c, Dob__c FROM Child__r) FROM Parent__c]
=============================================

Let's understand this by taking real time scenario example 
Example :
Write an Apex program to print customer details along with corresponding transaction details of the customer whose account type is saving.
=============================================
public class parentchild  //Class Name
{
 public List<customer__c> acc{get; set;}
  
  public pageReference Show() //Class Methods
 {
   acc = [Select customer_name__c, Account_Type__c, (SELECT                           Type__c, Account__c FROM Transactions__r) FROM                                   customer__c)]; //Parent To Child Relationship
   
  return null ;
 }
}
=============================================

From the above examples, you can easily understand how to use Parent-Child Relationship Queries on both standard and custom objects.
I would request you to run these queries at least once and experiment with your business scenarios. 

In the Next episode, we will discuss Salesforce SOSL.


WOHOOO !! YOU HAVE JUST COMPLETED APEX SOQL RELATIONSHIP PART 2 EPISODE 

If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)

<< PREVIOUS                                                     NEXT >>



Salesforce SOQL | Relationship Queries - Part 2 Salesforce SOQL | Relationship Queries - Part 2 Reviewed by on Rating: 5

No comments:

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.