Working with sObjects, SOQL, and SOSL

Prakher Chaturvedi
5 min readMay 8, 2020

--

We are now already familiar with some of the different data-types available in Salesforce. We are also familiar with classes and have completed the Quick Start: Apex trailhead module.

Today, I have taken up sObjects, SOQL, and SOSL. I will be studying them and also working with them in a trailhead module. As we have already discussed different data types, I moved to the trailhead and have completed a module named Apex Basics for Admins. People wanting to have some more clarity about the topics that we discussed in our last series(Primitive Data types, Collection ) can take this module and complete it. You will also get to know about control data flow (if, else) and loops.

DML OPERATIONS ON sObjects

Before getting into SOQL and SOSL, Let us understand about DML and how they can be used.
DML is an abbreviation for Data Manipulation Language and as its name suggests, it is used for manipulating data on the Salesforce Platform.
Some of the common DML commands used in Salesforce are:
Insert- used to insert a new record in a sObject
Update-
used to update an existing record
Delete-
used to delete an existing record
Upsert-
creates new records and updates sObject records within a single statement
merge-
merges up to three records of the same sObject type into one of the records, deleting the others, and re-parenting any related records.

Let us perform these common operations on a sObject for understanding their syntax and how they work.

insert DML operation

The above DML operation will create a new record in the Account object with the name ‘Test Business’.

Now let’s try to update the same Account object which we just created using update DML command.

Fetching the existing Account Record and updating it

Let us understand, what upsert does. Upsert command in the below code will update the description of the already existing record and create a new record.

SOQL:

As now we are comfortable with the basic DML commands, I will now move to SOQL and what SOQL is. SOQL is a query language used to query saved records in Salesforce Objects. It is very similar to standard SQL but it is designed for the Salesforce platform.

Some Points that can completely describe SOQL are :

-Salesforce Object Query Language
- used to read saved records
-Similar to SQL
-when embedded with Apex, it is referred to as inline SOQL
-Works on only one object at a time
-can query any type of field
- Can be used in both class and triggers
-DML operations cannot be performed
-Returns records

SYNTAX-
SELECT Name, Phone FROM ACCOUNT

One major difference between SQL and SOQL is that we cannot perform SELECT * on any object in SOQL.

SOSL:

Salesforce Object Search Language (SOSL) is a search language used to search for records with some specific text.

Some Points that can completely describe SOQL are :

-Salesforce object search language
-used to perform text searched in records
- can be used to search across multiple standard and custom objects
-Works on multiple objects at once
-can only query email, text or phone fields
-Cannot be used on triggers
-DML operations cannot be performed
-Returns fields

-SYNTAX:
FIND {SFDC} in ALL FIELDS RETURNING
Account(Name), Contact(FirstName,LastName)

So the above points are well sufficient for understanding SOQL and SOSL and also will help you understand the difference between both of them.
Let’s move to a trailhead module for some hands-on experience on what we have learned together.

Trailhead module that we will be covering is Apex Basics & Database

The first hands-on challenge brushes up your programming concepts with loops, using data types, classes, and methods.
In Get Started with Apex section, we will be required to build a code using class, methods, and list. This code will be returning a list.

The above code can be executed by making a method call with the class name and a dot operator.

Manipulate Records using DML:

What we have done in the above code?
A string is passed in the method insertNewAcccount, This method will create a new account with the name passed as a parameter. If there is an exception, we will catch the exception and return null.

Write SOQL QUERIES:

Querying Contact object to find records with specific last name and mailing postal code

The above SOQL query is finding performing query on Contact object using two strings. The field on which this query works is LastName and Mailing postal code.

We should be knowing an important aspect here that we should use a colon(:) when using a variable in the query. A colon is used to bind the value.

Write SOSL QUERIES:

SOSL query to find a string in Contact and Lead object

The above query is written to Find a string(passed as a parameter in searchContactsAndLeads method) in Contact and Lead Object. This query returns a list of sObjects.

So here we are done for the day, we have learned sObjects, SOQL, and SOSL. We are also done with our hands-on experience working with the stuff we read. Also, we have received one more badge on our trailhead profile which can be showed in our resume providing the trailhead profile link.

Congratulations on completing one more shiny badge which will enhance your resume

We are now confident with sObjects, SOQL, and SOSL with hands-on experience. Let’s take-up apex triggers next. We will learn Triggers and complete some trailhead modules to get going with it.

How Can You Contribute?

  • Connect with me on Twitter or Linkedin or Instagram.
  • Provide me continuous feedback and suggestions for moving ahead in this journey.
  • Share the series on Twitter and LinkedIn.

Posts In This Series :

Salesforce Development — Learning Apex Programming, VisualForce, Lightning Components
First Step toward Learning Apex Programming
Day 2 in Apex — Writing apex codes
Working with sObjects, SOQL, and SOSL
Understanding Salesforce Triggers and working with them
Apex Test classes and methods
Understanding VisualForce Concepts
Controllers in VisualForce

--

--

Prakher Chaturvedi

Looking for remote opportunities | Salesforce Developer | Lightning Web Components | APEX | 5x Certified | Trailhead Ranger | Content Creator (Medium)