Salesforce is one of the leading customer relationship management (CRM) platforms that helps businesses improve customer relationships and drive overall business performance. The built-in tools in Salesforce allow businesses to generate leads, track and close more deals in one place.
Salesforce provides robust data management languages, such as SOQL and SOSL, for effective data governance and preparation. By mastering these languages, you can improve data accuracy and enable informed decisions.
This Salesforce data operations tutorial discusses in detail how to perform data operations in real-world Salesforce scenarios.
Table of Contents
Salesforce allows you to import, export, cleanse, maintain, and analyze data. SOQL and SOSL help you query and transform business data into accurate, consistent information.
By applying Salesforce DML operations, you can deliver scalable, secure data solutions to organizations. Specifically, you can provide data accessible to multiple teams and generate valuable insights for customer success.
SOQL is a query language used in Salesforce and is very similar to SQL. SOQL allows you to retrieve records from Salesforce databases.
Let’s explore more about SOQL here.
Example:
You can use the ‘Select’ Keyword to retrieve data from objects and fields.
SELECT ID, Salary FROM Employee
Using the above SOQL Query, you can retrieve the ID and salary of the custom object called ‘Employee’.
Furthermore, you can use Application Programming Interface (API) names, which are system-defined names for fields in SOQL queries.
In standard objects, field names represent the API names.
| If you want to gain hands-on expertise in Salesforce and master real-time data operations, consider enrolling in a comprehensive Salesforce training program that covers SOQL, SOSL, and DML in depth. |
SOQL queries are usually enclosed in square brackets.
The below SOQL Query returns records from the database where the salary field equals ‘50000’
Employee e = [ Select ID, Name from Employee Where Salary='50000']
Here, 'e' stores the ID and employee fields for records where Salary equals '50000'.
You can use the ‘WHERE’ clause to return records based on a condition.
Example:
SELECT SID FROM Student WHERE Percentage>85
In the above SOQL Query, you use the ‘WHERE’ clause to retrieve the selected data from the ‘student’ object. The ‘WHERE’ clause applies the ‘Percentage>85’ condition to filter the records in the ‘student’ object.
You can use comparison operators in SOQL to compare two values and return TRUE or FALSE.
Let’s describe various types of SOQL operators here.
Select S_Name, S_ID FROM Student WHERE S_Percentage="80."
SELECT S_ID, S_CLASS FROM Student WHERE S_FEE<10000
This query returns the S_ID and S_CLASS of students whose fees are less than 10000.
SELECT S_Name S_CLASS FROM Student WHERE S_FEE>10000
This query retrieves the S_Name and S_CLASS of students whose fees are greater than 10000.
SELECT S_Name, S_ID FROM Student WHERE S_Percentage <= 85
This query returns the S_Name and S_ID of students with a percentage less than or equal to 85.
SELECT S_Name, S_ID FROM Student WHERE S_Percentage>=90
This query retrieves the S_ID and S_Name of students with a percentage greater than or equal to 90.
SELECT S_NAME S_ID FROM Student WHERE S_ID LIKE '00%
This query finds records where S_ID contains '00'.
In SOQL, you can use the ORDER BY clause to retrieve records either in ascending or descending order, depending on the given condition.
Salesforce supports two types of ORDER BY Clause, as follows:
Let’s discover them with examples.
SELECT S_NAME S_FEE FROM Student ORDER BY S_FEE ASC
Source Table:
| S_NAME | S_FEE |
| Suresh | 15000 |
| Ramesh | 10000 |
This query returns the following records.
| S_NAME | S_FEE |
| Ramesh | 10000 |
| Suresh | 15000 |
SELECT S_NAME S_PERCENTAGE FROM Student ORDER BY S_PERCENTAGE DESC
Source Table
| S_NAME | S_PERCENTAGE |
| Suresh | 92 |
| Rajesh | 89 |
| Ramesh | 95 |
| Rakesh | 90 |
The above query returns the following records.
| S_NAME | S_PERCENTAGE |
| Ramesh | 95 |
| Suresh | 92 |
| Rakesh | 90 |
| Rajesh | 89 |
In SOQL, you can use logical operators to link one or two conditions in a single query.
Let’s go through the logical operators used in SOQL.
SELECT S_NAME S_FEE FROM Student WHERE S_FEE>5000 AND S_FEE<10000
| S_NAME | S_FEE |
| Ramesh | 6000 |
| Suresh | 7000 |
| Rakesh | 9000 |
SELECT S_NAME S_FEE FROM Student WHERE S_NAME =" VISWANATH" OR S_NAME=" RAKESH."
The above query returns the following records.
| S_NAME | S_FEE |
| Rakesh | 9000 |
In Summary, SOQL is a query language for retrieving data from the database. By using SOQL operators, you can perform various data manipulation operations.
Salesforce developers use SOSL to construct text-based search queries against the search index. In other words, SOSL lets developers quickly find records or data by searching for specific words or phrases.
Let’s learn further about SOSL.
Furthermore, you can execute SOSL queries in the following contexts.
How to write SOSL?
An SOSL query starts with the FIND clause. You can then add required clauses to filter the object type, data, and fields in the query.
The format of an SOSL query is shown below.
Syntax for SOSL Query
FIND {SearchQuery}
[ IN SearchGroup ]
[ RETURNING FieldSpec [[ toLabel(fields)] [convertCurrency(Amount)] [FORMAT()]] ]
[ WITH DivisionFilter ]
[ WITH DATA CATEGORY DataCategorySpec ]
[ WITH SNIPPET[(target_length=n)] ]
[ WITH NETWORK NetworkIdSpec ]
[ WITH PricebookId ]
[ WITH METADATA ]
[ LIMIT n ]
[ UPDATE [TRACKING], [VIEWSTAT] ]
We’ll understand the different fields of an SOSL query.
The FIND field - It specifies the text (word or phrase) to search for.
In Search Group - It specifies the scope of fields for the search. It defines the search in more detail. The possible values in it can be:
The Returning field - It represents the information to be returned in the search results. A list of one or more objects can be specified after this keyword. If you skip this, the results will include the IDs of all objects found.
The LIMIT field - It specifies the maximum number of rows returned by the query. If this is unspecified, the default is 200, the maximum number of rows that can be returned.
| SOQL | SOSL |
| You can make a query only on one object at a time. | You can make a query on many objects at a time. |
| You can query on all fields of any data type. | You can query fields with data types including name, phone & email. |
| You cannot perform DML operations on query results. | You cannot perform DML operations on search results. |
| Return Type: List | Return Type: List Of Lists. |
You can perform test SOSL queries using the Anonymous Window.
Below is the step-by-step procedure for the same.
FIND {john) IN ALL FIELDS RETURNING Naukri_com_JobForm_C
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm_c(name)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm_c, Levis_C
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm_c(First_Name_c), Levis_c(Price_c)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm_c(First_Name_c), Levis_c(Price_c WHERE Price_c >1000)
FIND {john} IN ALL FIELDS RETURNING Naukri_com_JobForm_c(First_Name_c LIMIT 2), Levis_c(Price_c WHERE Price_c >1000)
FIND {john) IN ALL FIELDS RETURNING Naukri_com_JobForm_c(First_Name_c LIMIT 2), Levis_c(Price_c WHERE Price_c >1000 ORDER BY Price_c )
Steps to Create SOQL Apex Class
Log in to Salesforce org → Developer Console → Ctrl + E → Write the code and execute.
Example
list<Levis_c > ListOfJean = new list<Levis_c >();
ListOfJean = [SELECT Price_c FROM Levis_c WHERE Price_c > 1000];
system.debug('The Result ='+ ListOfJean);
Output
12:54:26:007 USER_DEBUG [3] |DEBUG| The Result =(Levis_c:{Price_c=1200, Id=a066F00001MGfdVQAT})
You can perform this operation as follows.
Example:
list<list<sobject>> ListofResults = new list<list<sobject>>();
ListOfResults = [FIND 'Blonde' IN ALL FIELDS RETURNING Naukri_com_JobForm_c(First_Name_c LIMIT 2), Levis_c(Price_c WHERE Price_c >
500 ORDER BY Price_c)];
system.debug('The Results=' + ListOfResults );
//add additional code for List Seperation of Output
list<Naukri_com_JobForm_c> ListA = new list<Naukri_com_JobForm_c>();
list<Levis_c> ListB = new list<Levis_c>();
ListA = ListOfResults[0];
ListB = ListOfResults[1];
system.debug('The Result in List A =' + ListA);
system.debug('The Result in List B =' + ListB);
Output
The Results=
(
(Naukri_com_JobForm_c: {First_Name_c=Blonde,Id=a006F000039j3UgQAI}
),
(Levis_c:{Price_c=1100, Id=a066F00001MGxGbQAL},
Levis_c: {Price_c=1400, Id=a066F00001MGxEzQAL},
Levis_c:{Price_c=1500, Id=a066F00001MGxGqQAL}
)
)
After Adding Additional Code for List Separation, the Output is:
14:42:56:127 USER_DEBUG [11] |DEBUG| The Result in List A =
(Naukri_com_JobForm_c:{First_Name_c=Blonde, Id=a006F000039j3UgQAI})
14:42:56:127 USER_DEBUG [12] |DEBUG|The Result in List B =
(Levis_c:{Price_c=1100, Id=a066F00001MGxGbQAL},
Levis_c: {Price_c=1400, Id=a066F00001MGxEzQAL}, Levis_c:{Price_c=1500, Id=a066F00001MGxGqQAL})
Salesforce supports data manipulation operations, including insert, delete, and update.
The DML operations are implemented in the following way:
You can use the INSERT statement to insert new data into the database. Data can be added to any custom or standard object.
Example
Student newStud= new Student(Stud_Name='Rakesh');
Try {
Insert newStud;
} catch(DmlException d) {
Exception Code
}
The code above shows how to insert a new student's name. The try-catch block is used to check whether a student named Rakesh is already in the database.
Student[] oldStuds= [SELECT Stud_ID , Stud_Name FROM Student WHERE Stud_Fee<10000];
Try{
Delete OldStuds;
} catch (DmlException d) {
Exception Code
}
The code above deletes the records of students with fees less than 10000.
Student s = new Student(Name=" Rakesh")
insert(s)
Student myStud = [ SELECT Stud_ID, Stud_Name, Stud_Fee FROM Student WHERE Stud_ID= s.Stud_ID];
myStud. Fee='10000';
try{
Update myStud;
} catch (DmlException d) {
Exception Code
}
Upsert vobject[optional_field]
Upsert vobject[] [optional_field}
The upsert statement matches records based on a specified field or the record ID.
When you invoke the Upsert statement without specifying the domain, Salesforce uses the sObject ID to compare with existing records. If you provide the ID, it is used for comparison.
For custom objects, include a custom field labelled 'external ID' that serves as a unique identifier for records.
For standard objects, you must specify a field with the idLookup attribute set to true, meaning the system can use this field to find records by value.
Undelete vobject Name
Merge vobject vobject
Merge vobject ID
The first argument shows the superior record to which the other records are merged.
The second argument shows one or two records to be merged; after that, they can be deleted.
In Salesforce, you can use DML operations to modify data and play a key role in handling and formatting data.
Ans: Yes, you can learn Salesforce easily. If you have some experience with data management and basic business processes, learning Salesforce quickly would be helpful.
Ans: Data cleansing in Salesforce includes correcting or removing inaccurate and outdated data to produce high-quality data.
Ans: A data loader is an application you can use to perform bulk import, export, update, and delete operations on large volumes of data.
Ans: You can ensure data security by using role-based access control, encryption, and field-level security, and by applying data-handling best practices.
Ans: Salesforce developers can earn a salary between 17 LPA and 19 LPA in India with 1–6 years of experience, according to AmbitionBox. They can earn between 83k and 166k USD in the USA, according to Indeed.
Ans: Yes, the following Salesforce e-learning resources help you strengthen your understanding of Salesforce.
Performing Salesforce data operations enables you to manage, transform and maintain high-quality data across your organization. We hope this tutorial has provided you with a strong foundation in mastering SOQL and SOSL.
Are you curious to learn more about Salesforce data operations? You can check out a Salesforce course in MindMajix. By the end of the training, you will acquire the skills to work with SOQL and SOSL effortlessly and be prepared to become an industry-ready Salesforce professional.

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:
| Name | Dates | |
|---|---|---|
| Salesforce Training | May 12 to May 27 | View Details |
| Salesforce Training | May 16 to May 31 | View Details |
| Salesforce Training | May 19 to Jun 03 | View Details |
| Salesforce Training | May 23 to Jun 07 | View Details |