Relationship Queries using SOQL

Relationship queries using SOQL, we can retrive the related Objects data. SOQL provides syntax to support relationship queries against standard objects and custom obejcts. Relationship queries using SOQL can be done to parent-to-child relationship and child-to-parent relationships between objects to filter and return results.

  • Contacts ————–<———Account

*Relationship name = Account.

Child to parent Relationship

In the child -to-parent Relationship, we can query on Contact and retrieves the values from Account that is associated.

Example:-

Contact C = [Select First Name, LastName, Account.Name, Account .Industry from Contact where id = 'XXXXX'];
System.debug (c. Account .Name);  * Prints the Account Name of Contact. 
System.debug(C.Account.Industry); * Prints the industry field value from account.

Parent-to-Child Relationship

In the parent-to-child relationship. We can query on the parent object and we will get the details about child records.

Account a = [Select Name, (Select Contact .FirstName, contact .LastName from Account. Contacts) From Account where id ='xxxxxxx']

System debug (a); * this will give all the Contact for that account.

For (Contact C: a.Contacts) 
{

system.debug (C. First Name);
 }

This statement displays FirstName of all contacts that are associated with the account.

Note:- When we want to write query on parent to child relationship then we need to write subquery ( query within another query).

Custom Relationship

This selection explains how relationship names for Custom objects and Custom fields that are created and used.

When we use a relationship name in a query. We must use the relationship names without __c. Instead, append an —r (underscore underscore r).

Example:- Daughter__c——————->——–Mother-of-child__c

Relationship Name =Mother -of-child—r, child -to-parent Relationship.

parent-to-child Relationship, Relationship =Daughter–r

When we use a child-to-parent relationship.

Daughter_ _c D= [Select id, First name _ _C, Mother-of child_ _r.firstname from daughter where LastName_ _C LIKE 'C%']

The above query returns the ID and first name of daughter objects, and the first name of the daughter’s mother if the mother’s last Name begins with ‘C’.

SELECT LastName __c, (Select LastName__c From Daughters --r) FROM mother- of-child __c.

The above query returns the last name of all mothers, and for each mother returned, the last name of the mother’s daughters.

Standard formats of Queries.

*Fetching single record from child-parent for standard objects.

Child obj name variable = [select child obj field1, parent obj name.parent obj field1, 
parent obj name.parent obj field2 from child obj name where condition];
System.debug (‘child obj records are’+variable);
System.debug (‘’ parent obj field1+variable .parent obj name.parent obj field1);
System.debug (‘parent obj field2+variable.parent obj name.parent objfield2);

*Fetching multiple records child-parent for standard object:

List<Child obj name> variable = [select child obj field1, parent obj name.parent obj field1,
 parent obj name.parent obj field2 from child obj name where condition];
System.debug (‘child obj records are’+variable);
For (child obj name variable1: variable)
{
System.debug (‘’ parent obj field1+variable1 .parent obj name.parent obj field1); 
System.debug (‘parent obj field2+variable1.parent obj name.parent objfield2);
}

Updating multiple records child-parent for standard object:

List<Child obj name> variable = [select child obj field1, parent obj name.parent obj field1, 
parent obj name.parent obj field2 from child obj name where condition];
System.debug (‘child obj records are’+variable); For (child obj name variable1: variable)
{
Variable1.parent obj name.parent obj field1=’..............’
Variable1.parent obj name.parent obj field2=’...............’
Update variable1.parent obj name;
System.debug (‘’ parent obj field1+variable1 .parent obj name.parent obj field1); 
System.debug (‘parent obj field2+variable1.parent obj name.parent objfield2);
}

*Fetching multiple records parent-child for standard object:

List<Parent obj name> variable= [select fieldname1 (select field1, 
field2 from parent obj name.childs obj)from parent obj name where condition];
System.debug (‘parent obj records’+variable1);
For (parent obj name variable1: variable) {
For (child obj name variable2:variable1.child plural obj name) {
System.debug (‘child obj field1’+variable2);
System.debug (‘child obj field2’+variable2);
} }

*Updating multiple records parent-child for standard object.

List<Parent obj name> variable= [select fieldname1 (select child obj field1, child obj 
field2 from parent obj name.child plural obj) from parent obj name where condition];
System.debug (‘parent obj records’+variable1); For (parent obj name variable1: variable)
{
For (child obj name variable2:variable1.child plural obj name) 
{
Variable2.child obj field1='..........';
Variable2.child obj field2='..........'; update variable2;
System.debug (‘child obj field1’+variable2); 
System.debug (‘child obj field2’+variable2);
 }
}