Understanding Parent-to-Child and Child-to-Parent Relationships in Salesforce
If you’ve worked with Salesforce for a while, you’ve probably heard terms like parent-to-child and child-to-parent relationships.
These are very common in the Salesforce world, and understanding them is key when you deal with related records, reports, or automations.
In simple words, these relationships show how objects are connected in your Org. Once you get this concept right, everything else in Salesforce data modelling starts to make sense.
What These Relationships Mean
Let’s keep it simple:
- Child-to-Parent means you are moving up the relationship.
For example, a Contact record can look up to its Account record.
In SOQL: - Parent-to-Child means you are moving down the relationship.
For example, an Account record can have many Contacts related to it.
In SOQL:
So, in short:
- Child-to-Parent = going upward (lookup field)
- Parent-to-Child = going downward (related list)
These relationships are what make Salesforce data feel connected and meaningful.
Why These Relationships Are Important
Relationships matter in Salesforce because almost everything is connected.
Here are a few reasons why they’re so useful:
- Reports and Dashboards: You can build reports that combine related data, like Accounts and Opportunities.
- Flows and Automation: Flows can update child records when something changes on the parent.
- Apex Code: Developers use relationships to get data without writing multiple queries.
- Data Imports: When importing data with relationships, you can link records easily using lookup fields.
Basically, without relationships, every record would stand alone and Salesforce wouldn’t be nearly as powerful.
Real-Life Examples
Example 1: Contact to Account (Child-to-Parent)
Every Contact belongs to an Account. If you want to show the Account Name or Industry on the Contact record, you can use a child-to-parent relationship.
SOQL Example:
SELECT Id, FirstName, LastName, Account.Name, Account.IndustryFROM Contact
You start from the child (Contact) and move up to its parent (Account).
Example 2: Account to Contacts (Parent-to-Child)
If you want to get all the Contacts that belong to each Account, you go the other way i.e. parent-to-child.
SOQL Example:
SELECT Id, Name, (SELECT Id, FirstName, LastName FROM Contacts)FROM Account
Now you start from the parent (Account) and move down to its children (Contacts).
How to Create and Use These Relationships
1. Creating Relationship Fields
You can create a relationship field on an object by choosing either:
- Lookup Relationship – a loose link between two objects.
- Master-Detail Relationship – a strong link; the child record depends on the parent.
Tip:
Use Master-Detail when you want child records to follow the parent (like deleting both together).
Use Lookup when the child can exist even if the parent is deleted.
2. Using Relationships in SOQL
In SOQL:
- To go child-to-parent, use a dot (.).
Example: Contact.Account.Name - To go parent-to-child, use a subquery in parentheses.
Example: (SELECT Name FROM Contacts)
3. Using Relationships in Flow
In Record-Triggered Flows, you can:
- Access parent fields using the relationship (e.g., Opportunity.Account.Name).
- Loop through child records using the Get Records element filtered by parent Id.
Example:
If an Account is deactivated, you can automatically update all related Opportunities using a parent-to-child loop in Flow.
4. Using Relationships in Apex Code
Here’s a quick example in Apex:
for (Account acc : [SELECT Id, Name, (SELECT Id, Name FROM Contacts) FROM Account]) {
for (Contact con : acc.Contacts) {
System.debug('Contact Name: ' + con.Name);
}
}
You can move both ways in Apex — as long as you include the relationship fields in your query.
Best Practices and Common Mistakes
Best Practices
- Always check the child relationship name before writing SOQL subqueries. You can find it in Object Manager.
- Use relationships in SOQL to reduce the number of queries and avoid hitting governor limits.
- Keep your data model clean — don’t create unnecessary lookup fields.
- Use Schema Builder to visualize and understand relationships between objects.
Common Mistakes
- Forgetting to include relationship fields in your query.
- Using the wrong relationship name (Contact__r vs Contacts__r).
- Querying too many child records at once and hitting limits.
- Choosing Master-Detail when Lookup would be safer.
FAQs
1. How Can I Find the Relationship Name?
Go to Setup → Object Manager → Child Object → Fields & Relationships → Lookup field. You’ll see the Child Relationship Name there.
2. Can a Parent Have Multiple Child Objects?
Yes! One parent can have many different child relationships.
3. What Happens If I Delete a Parent Record?
-
- In Master-Detail, the child records are also deleted.
- In Lookup, the link just becomes empty.
4. Can I Use These Relationships in Reports?
Yes. Salesforce allows cross-object reports — like Accounts with Opportunities, or Contacts with their Accounts. If you need help setting up advanced reporting or structuring your data model for accurate insights, our Salesforce consulting services can assist you.
5. How Many Levels Deep Can I Query in SOQL?
You can go up to five levels deep in a single query.
SELECT Id, Name, (SELECT Id, LastName FROM Contacts) FROM Account SELECT Id, Name, Account.Name FROM Contact
related blog
