ONE DAY SALE

Free Salesforce Developers Tutorials >

Chapter 6 - SOQL >

Parent to Child Relationship

Parent to Child Relationship

What You’ll Learn


S2 Labs

What Is Parent-To-Child Relationship Query?

Whenever we need to fetch data of parent records with their related child records, then the query we use is parent-to-child.

For Standard Objects:

SELECT(SELECT Child_Obj_Field FROM Child_Relationship_Name) FROM Parent_Object;
List < Account > accList = [SELECT Name, NumberOfEmployees, (SELECT Name, Amount, CloseDate FROM Opportunities) FROM Account];
for (Account acc: accList) {
    System.debug(‘ACC: ’+acc.Name + ’NoEmpl: ’+acc.numberOfEmployees.; List < Opportunity > opList = acc.Opportunities;
        for (opportunity o: oplist) {
            System.debug(‘Opportunity Name: ’+o.Name + ‘Amount: ’+o.Amount);
        }
    }

For Custom Objects:

SELECT(SELECT Child_Obj_Field FROM(Child_Relationship_Name) __r)
    FROM Parent_Object;
    List < Bank__c > bankList = [SELECT Bank_Name__c, (SELECT Branch_Id__c, Name, State__c FROM Branches__r) FROM Bank__c];
    for (Bank__c ba: bankList) {
        List < Branch__c > branchList = ba.Branches__r;
        for (Branch__c br: branchList) {
            System.debug('Bank:' + ba.Bank_Name__c + '|Branch ID:' + br.Branch_ID__c + '|Branch Name:' + br.Name + '|Branch State:' + br.State__c);
        }
    }

You can not write a subquery inside a subquery in SOQL.

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 *