Table of Contents
Introduction
After going through the basics of the Salesforce SOQL queries now let us check some more examples of the same and the steps to write them in a more detailed manner.
Steps to write SOQL queries
- Analyse the need: Firstly, we have to analyze the need in the business and proceed with the next steps.
- Fragment the logic: Now, we have to fragment the business logic.
- Incremental queries: Now, according to the fragmented logic we have to prepare for the queries.
- Get the functions required: Get the appropriate functions to write the query efficiently.
Write SOQL Queries
Now, let’s write a basic SOQL query and then look at queries with different SOQL queries to understand better.
Example
SELECT Name, Phone FROM Account WHERE Name = “Amazon”
Explanation
Here, the query is simple. We are just trying to retrieve the “Name” and “Phone” fields from the “Account” object with a condition of the only name as “Amazon”.
Queries in SOQL
- The barebones query
SELECT Name, Phone, Companyage__C FROM Account
This is a basic query where two standard fields and one custom field are retrieved from the “Account” object.
- The WHERE clause
SELECT Name, Phone FROM Account WHERE name = “Amazon”
This query is the same as the one mentioned in the above example. Now, by using a “ WHERE” clause retrieve only the conditions that satisfies the conditions that we have written after the clause.
- Using OPERATORs
SELECT Name, Phone FROM Contact WHERE Address != null
Here, we get the “Name” and “Phone” fields from the “Contact” object but only whose addresses are not null. Which is mentioned in the condition after “WHERE” clause.
- Using AND and OR in the WHERE clause
SELECT Name, Phone, Site FROM Account WHERE AccountNumber != null AND Status = True
Here, we select to get the “Name”, “Phone” and “Site” fields from the “Account” object based on two conditions. One is the “Account Number” field should not be null and the “Status” field should be checked.
- Using TEXT, PICKLIST, or ID values
SELECT Name, Phone FROM Leave WHERE LeaveNo__c > 3 AND LeaveType = “Sick Leave” AND RecordId != “0015g00000MvGguAAF”
Here, we select “Name” and “Phone” fields on specific conditions on Text, Picklist, and Record Id.
- Using Numbers
SELECT Name, Phone FROM Account WHERE Phone = null
Here, we select “Name” and “Phone” fields from “Account” object on the condition that the phone’s field should not be null.
- Using Date
SELECT Name__c, Address__c FROM Student__c WHERE DateOfJoin__c = TODAY
Here, we need the names and addresses of the students who joined today and we use this query.
- Fuzzy Matching
SELECT Name, Phone FROM Contact WHERE Phone LIKE “%(200)%”
Now, the query returns the records whose phone number matches approximately.
- Sorting the results
SELECT Name FROM Account ORDER BY Name ASC
Now, you get all the names of the Account present in the org in an ascending order.
- Limiting your results
SELECT Name, Phone FROM Account LIMIT 10
Here, you simply get “Name” and “Phone” fields from “Account” object. But as you observe you get only 10 records.
- Putting it all together
SELECT Name, Phone FROM Contact WHERE Address != null AND (Birthdate = TODAY OR Age__c >30) ORDER BY Name LIMIT 20
Reference
https://www.sfdc99.com/2013/06/07/example-how-to-write-a-simple-soql-query/
0 Comments