2 - SOQL : Parent to Child Relationship Queries

SOQL : Parent to Child Relationship Queries


FOR STANDARD OBJECTS==>. **For Relationship Queries we need ---> Child Relationship Names which is found in the ---> Child ---> Field & Relationships ----> in Lookup (Datatype) --> click it --You get Child Relationship Names

// Get all related contacts of an account

SELECT Name, Phone, Website, (SELECT Name, Department, Email FROM Contacts) FROM Account


FOR CUSTOM OBJECTS==>. **For Relationship Queries we need ---> Child Relationship Names which is found in the ---> Child ---> Field & Relationships ----> in Lookup (Datatype) --> click it --You get Child Relationship Names + (YOU ADD suffix __r in the  FOR CUSTOM OBJECTS==>. **For Relationship Queries we need ---> )

// Get all books(child object) of an author (parent object)

SELECT Name, (SELECT Name FROM Books__r) FROM Author__c

RESULT CAN HAVE MULTIPLE CHILD RECORDS

Limitations 

Only one level child can be queried

20 childs maximum can be queried


SOQL : Child to Parent Relationship Queries


FOR STANDARD OBJECTS==>. **For Relationship Queries we need --->Field Name which is found in the ---> Child ---> Field & Relationships ----> in Lookup (Datatype) --> click it --You get Field Name

// Get account information from contact

SELECT Name, Phone, Department, Account.Name, Account.Website FROM Contact


// get grandparent (user) object details

SELECT Name, Phone, Department, Account.Name, Account.Website, Account.Owner.Name FROM Contact

// Get author(parent object) from books(child object)

SELECT Name, Price__c, Author__r.Name FROM Book__c

ONLY ONE PARENT RECORD


Limitations 

Only FIVE level parents can be queried

55 parents maximum can be queried 


Data in Apex from SOQL


// Retrieve all books and assign to a List collection

List<Book__c> books = [SELECT Name, Price__c FROM Book__c];

for(Book__c book : books){

    System.debug('Book Name: '+book.Name+' Book Price: '+book.Price__c);

}



APEX (Binding) Variables in SOQL using COLON(:)


List<String> accountNames = new List<String>();

accountNames.add('Sample1');

accountNames.add('S2');

List<Account> accounts = [SELECT Id, Name, Phone, Rating FROM Account

                          WHERE Name IN :accountNames];


DYNAMIC QUERIES


String accountClass = 'B';


String queryString = 'SELECT Id, Name, Phone, Rating FROM Account';


if(accountClass == 'A'){

    queryString += ' WHERE Rating=\'H\' AND Type=\'Prospect\'';

} else if(accountClass == 'B'){

    queryString += ' WHERE Rating=\'AA\' AND Type=\'Other\'';

} else {

    queryString += ' WHERE Rating=\'BB\'';

}


List<Account> accounts = Database.query(queryString);

System.debug('Accounts '+accounts);



 sObjects (Salesforce Objects) 

In Salesforce, sObjects (Salesforce Objects) are foundational to handling data within Apex (Salesforce’s proprietary programming language). They are used for various reasons:

1. Universal Representation of Salesforce Data

  • sObjects represent records of Salesforce database tables (standard or custom objects) within Apex, allowing developers to interact programmatically with Salesforce data. Each Salesforce object, like Account, Contact, and Opportunity, is an sObject type, making it easy to work with both standard and custom object data in Apex.

 







Comments

Popular posts from this blog

API Names in Salesforce

18 - LWC - BEST PRACTICES - For accessing HTML elements and their values in JavaScript

6 - Object Relationships (Lookup and Master-Detail and Junction Objects)