Querying & Manipulating Salesforce Data

Episode 4 – Querying & Manipulating Salesforce Data

Apex, Apex Hours for Students

In this episode we will learn about retrieving and manipulating data stored within Salesforce programmatically using Apex. Join us as you embark on this wonderful journey to become a champion Salesforce developer.

Episode 4 will be presented by Saket Joshi on Feb 13, 2020 at 6 PM Indian Standard Time.

Agenda

  • 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
  • Understanding DML Operations & Methods
  • Writing optimal queries – Query Plan

Most importantly don’t break a leg if you are overwhelmed with the pace of the live sessions. All Apex Hours for Student sessions will be recorded and will be available on our YouTube channel.

Please register here and follow our ApexHours website to get more notification.

What is SOQL

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 –
1) Apex Code
2) Developer Console
3) Salesforce REST and SOAP APIs
4) Salesforce CLI

Basics of SOQL

SOQL Syntax

Conditional Expressions

Conditional Operator Description
= Equals
!= Not Equals
< , <= 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

Conditional Expressions

  • 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 –
    • AND
    • OR
    • NOT
  • Use Apex variables for comparison in your SOQL query by adding a colon (:) before the variable name

Aggregate Queries

  • Use aggregate queries to perform the following aggregate functions on the result set –
    • COUNT
    • SUM
    • AVERAGE
    • MIN
    • MAX
  • 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

•Apex allows DML operations on both List<SObject> and single SObject to update data in the database

DML Operation Usage
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

Database Methods

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]);

Recording

Basic of SOQL and SOSL in Salesforce | Query Plan| DAY 4 Part 

Data Manipulation and Error Handling in Salesforce | DML | DAY 4 Part 2

Further Learning

  • Apex Basics & Database
  • Search Solution Basics

Assignment

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 register for our next session on Apex trigger. Check this post for all other session detail.

Please note that we have limit of 500 attendees that can join the online sessions. However, recording will be 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!

5,973 total views, 18 views today

19 comments

Leave a Reply