fbpx

Free Salesforce Developers Tutorials >

Chapter 6 - SOQL >

Child to Parent Relationship

Child to Parent Relationship

What You’ll Learn


S2 Labs

What Is SOQL Relationship Queries?

Relationship queries involve at least two objects, a parent and a child. These queries are used to fetch data either from the Parent object when the SOQL query is written on the child, or from the child object when the SOQL query is written on the parent.

What Is Child To Parent Query In Salesforce?

Whenever we need to fetch data from parent object fields with child records, we use the query child-to-parent.

For Standard Objects

 SELECT Field_Name.Parent_Obj_Field FROM Child_Object;
List < Opportunity > oppList = [SELECT Name, Amount, CloseDate, Account.Name
    FROM Opportunity
];
for (Opportunity o: oppList) {
    System.debug('Opp: ' + o.Name + ' Acc: ' + o.Account.Name);
}

For Custom Objects

SELECT(Field_Name) __r.Parent_Obj_Field FROM child_object;
List < Branch__c > branchList = [SELECT Branch_ID__c, State__c,
    Branch_of_Bank__r.Bank_Name__c from Branch__c
];
for (Branch__c bl: branchList) {
    System.debug('Bank Name: ' + bl.Branch_of_Bank__r.Bank_Name__c + 'Branch: ' + bl.State__c);
}

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *