In this episode we will learn about retrieving and manipulating data stored within Salesforce programmatically using SOQL & DML in Salesforce. We will learn how to use SOQL in Salesforce. SOQL (Salesforce object Query Language) used to retrieves the records from the database. We will also learn about basic of SOSL in Salesforce.
What is SOQL in Salesforce?
SOQL Stands for Salesforce Object Query Language. Similar to SQL, but is designed specifically for Salesforce data. Uses the same structure and keywords from SQL. You can query data from Salesforce using SOQL in.
- Apex Code
- Developer Console
- Salesforce REST and SOAP APIs
- Salesforce CLI
Basics of SOQL
|< , <=||Less Than, Less Than or Equal To|
|> , >=||Greater Than, Greater Than or Equal To|
|INCLUDES, EXCLUDES||Includes or Excludes Values. Applies only to Multi-Select Picklists.|
|LIKE||Returns the records where the field values match the pattern specified after the LIKE operator|
|IN||Selects only the records where the value of the field matches any of the values specified after IN keyword|
|NOT IN||Selects only the records where the value of the DOES NOT MATCH any of the values specified after IN keyword|
- Each conditional operator mentioned in the above table returns a TRUE/FALSE value
- Build your own logic by joining multiple conditional operators using logical operators –
- Use Apex variables for comparison in your SOQL query by adding a colon (:) before the variable name
- Use aggregate queries to perform the following aggregate functions on the result set –
- Specify the field name to perform aggregation against that field in the result set
GROUP BY and HAVING Clauses
- Use the GROUP BY clause to group the result set based on the field specified
- HAVING is an optional clause used to filter the results that an aggregated function returns
- Difference between HAVING and WHERE clause is that you can use aggregated function in HAVING, but not in WHERE.
Writing SOQL in Apex
- SOQL query always returns a List. In Apex, create a List for the object specified in the query
- Always store the query result in a List and not a single instance
- Use for…loop for iterating over the results of a SOQL query
- Can directly write query in a for…loop for better performance
DML Operations in Salesforce
Apex allows DML operations on both List<SObject> and single SObject to update data in the database
|insert [SObject | List<SObject>]||Insert a new record or a list of records|
|update [SObject | List<SObject>]||Update existing records with the new data specified. Existing records are matched using the ID|
|delete [SObject | List<SObject>]||Delete records with the matching ID from the database|
|upsert [SObject | List<SObject>]||Performs both insert/update on the specified records. If record ID matches, then it updates the record. Otherwise it inserts a new record|
|merge SObject1 SObject2||Merges up to three records of the same SObject into a single record|
|undelete [SObject | List<SObject>]||Retrieves a record or list of records previously deleted from recycle bin|
|DML Operation||Equivalent Database Method|
|insert [SObject | List<SObject>]||Database.insert([SObject|List<SObject>], [Boolean allOrNone]);|
|update [SObject | List<SObject>]||Database.update([SObject|List<SObject>], [Boolean allOrNone]);|
|delete [SObject | List<SObject>]||Database.delete([SObject|List<SObject>], [Boolean allOrNone]);|
|upsert [SObject | List<SObject>]||Database.upsert([SObject|List<SObject>], [Boolean allOrNone]);|
|merge SObject1 SObject2||Database.merge([SObject|List<SObject>], [SObject|List<SObject>], [Boolean allOrNone]);|
|undelete [SObject | List<SObject>]||Database.undelete([SObject|List<SObject>], [Boolean allOrNone]);|
Basic of SOQL and SOSL in Salesforce | Query Plan| DAY 4 Part
- What is SOQL
- Basics of SOQL
- Relationship Queries
- SOQL in Apex, Querying Records in Batches by Using SOQL for Loops
- Introduction to SOSL
- Using the right tool for the job SOQL Vs SOSL
Data Manipulation and Error Handling in Salesforce | DML | DAY 4 Part 2
- Understanding DML Operations & Methods
- Writing optimal queries – Query Plan
- Apex Basics & Database
- Search Solution Basics
Complete below assignment to win $1000 Salesforce Voucher. Click here for rule.
| 1.Write a method to insert an Account record. Also insert five related contacts to that same account.|
2.Write a SOQL query to fetch the contact records that were inserted in previous question. (Hint: Use the CreatedDate field to add filter)
3.Write a query on Contact object and fetch the Account details. Use child to parent relationship query.
4.Write a query on Account object and fetch the related contacts. Use the parent to child relationship query.
Don’t forget to check our next session on Apex trigger. Check this post for all other session detail. All of session are recorded and posted on our YouTube channel. Make sure to subscribe our YouTube channel to get notification for video upload.
So, learn at your pace and free will and ace your journey to Salesforce!
I hope this session helped you to learn about what is SOQL in Salesforce and how to write SOQL in Salesforce. Let us know if this session helped you to learn SOQL.