For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example, Account has child relationships to Assets, Cases, and Contacts among other objects, and has a relationshipName for each, Assets, Cases, and Contacts.These relationships can be traversed only in the SELECT clause, using a nested SOQL query.
For example:
In the parent -to-child relation, we can query on the parent object and we will get details about child record.
Account a = [Select Id,Name, (Select Id,FirstName,LastName from Contacts) from account where id = 'Paste AcountId Here'];
System.debug(‘Name:’+ a.name );
Please Mark It As Best Asnwer If It Helps Thank You!
For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example, Account has child relationships to Assets, Cases, and Contacts among other objects, and has a relationshipName for each, Assets, Cases, and Contacts.These relationships can be traversed only in the SELECT clause, using a nested SOQL query.
For example:
In the parent -to-child relation, we can query on the parent object and we will get details about child record.
Account a = [Select Id,Name, (Select Id,FirstName,LastName from Contacts) from account where id = 'Paste AcountId Here'];
System.debug(‘Name:’+ a.name );
Please Mark It As Best Asnwer If It Helps Thank You!
Please have a look below points for better understanding:
if we talk about custom object then
For parent-child soql:- University__c is parent and colleges__r is child This code will give you all universities name and colleges under university
for(University__c u: [select Name,(select Name from colleges__r) from university__c])
{
for(College__c c:u.Colleges__r)
System.debug('University Name:'+u.Name+' College Name:'+c.Name);
}
And For child-Parent soql This code will give you all colleges name with respected university.
For(College__c c:[select Name,university__r.Name,university__c from college__c])
{
system.debug('College Name:'+c.Name+' University Name:'+c.university__r.Name);
}
if we talk about standard object then: account is parent and Contacts is child For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example, Account has child relationships to Contacts , and has a relationshipName for Contacts.These relationships can be traversed only in the SELECT clause, using a nested SOQL query. For example:
Account a = [Select Name, (Select FirstName, LastName from Contacts) from account where id = ‘XXXX’];
System.debug(‘Name:’+ a.name );
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thank You!
All Answers
Thank You!
Please have a look below points for better understanding:
if we talk about custom object then
For parent-child soql:- University__c is parent and colleges__r is child
This code will give you all universities name and colleges under university
And
For child-Parent soql
This code will give you all colleges name with respected university.
if we talk about standard object then: account is parent and Contacts is child
For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the
plural of the child object name. For example, Account has child relationships to Contacts , and has a relationshipName for Contacts.These relationships can be traversed only in the SELECT clause, using a nested SOQL query. For example:
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh