If you’ve built a custom UI for Salesforce, you can use the Salesforce Object Query Language (SOQL) and Salesforce Object Search Language (SOSL) APIs to search your organization’s Salesforce data.
SOQL(Salesforce Object Query Language) You can use SOQL to read information stored in your organization’s database. SOQL is syntactically similar to SQL (Structured Query Language).
SOSL(Salesforce Object Search Language) Only one object at a time can be searched(Search in Single object) Many objects can be searched at a time(Search in an entire organization or Database) Query all types of fields. Query on only email, text, or phone.
Determining Which to Use
A SOQL query is the equivalent of a SELECT SQL statement and searches the org database. SOSL is a programmatic way of performing a text-based search against the search index.
Using SOQL or SOSL depends on whether you know which objects or fields you want to search, plus other considerations.
Use SOQL when you know which objects the data resides in, and you want to:
- Restore data from a single object or from multiple objects that are related to one another.
- Count the number of records that meet specified criteria.
- Sort results as part of the query.
- Retrieve data from number, date, or checkbox fields.
Use SOSL when you don’t know which object or field the data resides in, and you want to:
- Restore data for a specific term that you know exists within a field. Because SOSL can tokenize multiple terms within a field and build a search index from this, SOSL searches are faster and can return more relevant results.
- Retrieve multiple objects and fields efficiently where the objects might or might not be related to one another.
- Retrieve data for a particular division in an organization using the division’s feature.
- Retrieve data that are in Chinese, Japanese, Korean, or Thai. Morphological tokenization for CJKT terms helps ensure accurate results.
Performance Considerations
To increase the efficiency of queries and searches, keep in mind:
- When a given search can use either language, SOSL is generally faster than SOQL if the search expression uses a CONTAINS term. Both SOQL WHERE filters and SOSL search queries can specify the text you should look for.
- If you’re searching for a specific distinct term that you know exists within a field, you might find SOSL is faster than SOQL for these searches. SOSL can tokenize multiple terms within a field (for example, multiple words separated by spaces) and build a search index of this. For example, you might use SOSL if you were searching for “Johnson” against fields that contained values like “Sam and Johnson Company”.
- The number of fields to be searched or queried should be minimum. Using many fields leads to many arrangements, which can be difficult to sort.
Which is better SOQL or SOSL?
SOSL is generally faster than SOQL if the search expression uses a CONTAINS term when a given search can use either language. SOSL can tokenize multiple terms within a field (for example, multiple words separated by spaces) and build a search index of this.
SOQL
- Only one object at a time can be searched i.e., search in a single object.
- It queries all types of fields.
- It can be used in classes and triggers.
- DML operations can be performed on query results.
- SOQL return records.
- Using SOQL we can know in Which objects or fields the data resides.
SOSL
- Many objects can be returned at a time i.e., search in the entire organization or database.
- It queries only email, text, or phone.
- It can use in classes but not in the trigger.
- DML operation cannot be performed on search results.
- By using SOSL, we don’t know in which object or field the data resides.
- It returns Field.
Can we perform DML operations on SOQL?
You can retrieve a record from the database to obtain its fields, including the ID field, but this can’t be done with DML. You’ll need to write a query by using SOQL. You’ll learn about SOQL in another unit. You can perform DML operations either on a single sObject, or in bulk on a list of sObjects.
Which data type Cannot be queried in SOSL?
Using SOSL we can make a query on many objects at a time. We can query all fields of any data type. Can query on fields whose data type is Name, Phone & Email. We cannot perform DML operations on search results.
Why do we use SOQL query in Salesforce?
SOQL stands for Salesforce Object Query Language. You can use SOQL to read information stored in your organization’s database. SOQL is syntactically similar to SQL (Structured Query Language). You can write and execute a SOQL query in Apex code or in the Developer Console’s Query Editor.
When should you use SOQL and when should you use SOSL?
A SOQL query is the equivalent of a SELECT SQL statement and searches the org database. SOSL is a programmatic way of performing a text-based search against the search index. Using SOQL or SOSL depends on whether you know which objects or fields you want to search, plus other considerations.
Why is SOSL not used in triggers?
SOSL queries are only supported in Apex classes and anonymous blocks. You cannot use a SOSL query in a trigger.
If you are also willing to learn more about salesforce you can contact us. Or you can also work with TCI to grow more in your career.