
What is SOQL in Salesforce in 2023? | The Developer Guide
In this Blog we will learn about SOQL in salesforce.
What is SOQL?
SOQL stands for Salesforce Object Query Language. When we have to perform any query on standard or custom objects then we need to write SOQL. So, SOQL fetches the records which are available in a particular object whether it is standard or custom.
So, let’s take an example to understand this:
Note: Always remember whenever we query the records, we have to store them in a list as we can query multiple records at a time. keeping this in mind:
Standard Object
List<Account> accList = new List<Account>(); accList = [Select Id, Name FROM Account Limit 2]; System.debug(‘accList =>' +accList);
Custom Object
List<Student__c> StudList = new List< Student__c>(); StudList = [Select Id, Name FROM Student__c Limit 2]; System.debug(‘Student List =>' +StudList);
Limit: It limits the number of records to be retrieved from the database.
Don't forget to check out: Relationship Queries in SOQL | Salesforce Tutorial Guide
How to use Query editor for SOQL?
Write Query as Select Id, Name FROM Account Limit 2
SOQL with Condition
In condition we have to put a where clause to check the condition and fetch selected records.
List<Student__c> StudList = new List< Student__c>(); StudList = [Select Id, Name FROM Student__c WHERE Name = ‘abc’]; System.debug(‘Student List =>' +StudList);
Use variables in Query for applying conditions
While using variable use ‘ : ‘ for checking condition.
List<Student__c> StudList = new List< Student__c>(); String stuName = ‘abc’; StudList = [Select Id, Name FROM Student__c WHERE Name =: stuName ]; System.debug(‘Student List =>' +StudList);
Null check on List returned through SOQL
To check this we can apply condtion on the list such as
if( !studList.isEmpty()) { for(Student__c s : studLIST) { } }
Count Queried Records
This will count the queried records.
List<Student__c> StudList = new List< Student__c>(); Integer c; c = [SELECT count() FROM Student__c WHERE Name = ‘abc’ ]; System.debug(‘Student List =>' + c ) ;
Ordering Query Results
By default, order by applies ascending order of records.
Order by Ascending
AccList = [SELECT Id,Name FROM Account WHERE Name Like ‘Tech%’ ORDER By Name ];
Order by Descending
AccList = [SELECT Id,Name FROM Account WHERE Name Like ‘Tech%’ ORDER By Name DESC];
Query Related Records (Parent to Child & Child to Parent)
Standard Object – parent is Account and child is Contact
List<Account> accList = new List<Account>(); AccList = [SELECT Id,Name, (SELECT Id,Name From Contacts) FROM Accounts WHERE Name = ‘abc’]; If(!accList.isEmpty()) { For(Account acc : accList) { system.debug(‘Acc Contacts is >’ + acc.Contacts); for(Contacts con : acc.Contacts){ system.debug(‘Acc Contacts is >’ + acc.Contacts); } }
Here contacts is the child relationship name and abc is the parent record and its related contacts will be queried automatically.
Check out another amazing blog by Bhawana here: All You Need to Know About SOSL and Related Records in Apex
Custom Object – Parent is Student and child is Books
List<Student__c> studList = new List<Student__c>(); studList = [SELECT Id,Name, (SELECT Id,Name From Books__r) FROM Student__c WHERE Name = ‘abc’]; If(!studList.isEmpty()) { For(Student__c std : studList) { system.debug(‘Student Books is >’ + std.Books__r); } }
Books__r is the child relationship name in custom object
Child to Parent
Standard Object – parent is Account and child is Contact
List<Contact> ConList new List<Contact(); ConList = [SELECT Id,Name, Account.Name, Account.Phone FROM Contact where Name = ‘xyz’]; System.debug(‘ConList =>' + ConList); //this is not working so apply loop to get all name and phone seperately If(!ConList.isEmpty()) { For(Contact con : ConList){ System.debug(‘Account Name =>' + con.Account.Name); System.debug(‘Account Phone =>' + con.Account.Phone); } }
Custom Object – parent is Student and child is Books
List<Book__c> bookList new List<Book__c(); bookList = [SELECT Id,Name, Student__r.Name, Student__r.Roll_Number__c FROM Book__c where Name = ‘xyz’]; System.debug(‘BookList =>' + bookList); //this is not working so apply loop to get all name and roll numbers seperately If(!bookList.isEmpty()){ For(Book__c b : bookList){ System.debug(‘Student Name =>' + b.Student__r.Name); System.debug(‘Student Roll Number =>' + b.Student__r.Roll_Number__c); } }
This is all about SOQL in salesforce. I hope this information is helpful to you.
Responses