Create Queries with Best Practice – SOQL and SOSL – Salesforce Certified Platform Developer I Study Guide

6.3  Create Queries with Best Practice

SOQL (Salesforce Object Query Language) and SOSL (Salesforce Object Search Language) are two query languages used in Salesforce to retrieve data from the database. Here are the main differences between SOQL and SOSL:

SOQL (Salesforce Object Query Language)

–– Queries start with the “SELECT” keyword.

–– It is used to query data from a single object or related objects.

–– It can query any type of field in the database.

–– It can be used in classes and triggers.

–– DML operations can be performed on the query results.

–– Returns records as the result of the query.

SOSL (Salesforce Object Search Language)

–– Queries start with the “FIND” keyword.

–– It is used to search for specific text across multiple objects.

–– SOSL can search on various text-based fields.

–– It can be used in classes and triggers.

–– DML operations cannot be performed on the search results.

–– Returns fields as the result of the search.

In summary, SOQL is used for querying data from single or related objects, while SOSL is used for performing text searches across multiple objects. SOQL returns records, while SOSL returns fields. Additionally, SOQL allows for more flexibility in querying different types of fields, whereas SOSL is limited to specific field types.

One common best practice to avoid exceeding governor limits is to avoid using SOQL statements inside loops. It can lead to performance issues and can quickly consume governor limits, such as the maximum number of SOQL queries, or CPU time allowed. This can result in errors like “System.LimitException”.

By moving the SOQL query outside of the loop, you avoid the query inside the loop problem and can handle the updates more efficiently. This approach is commonly referred to as bulkifying your code and helps in staying within Salesforce governor limits.

Here’s an example of how you can structure to follow. This code dynamically queries for Account records created in the last month and then retrieves associated Contact records, updating the Title field on those Contact records.

List<Contact> contactsToUpdate = new List<Contact>();

// Query for Account records created in the last month

Date lastMonthStartDate = Date.today().addMonths(-1).toStartOfMonth(); Date lastMonthEndDate = Date.today().toStartOfMonth(); Set<Id> accountIds = new Set<Id>();

for (Account acc : [SELECT Id FROM Account WHERE CreatedDate >= :lastMonthStartDate AND CreatedDate < :lastMonthEndDate]) {

accountIds.add(acc.Id);

}

  • Query related Contacts for the dynamically obtained Account Ids
  • outside the loop

List<Contact> relatedContacts = [SELECT Id, Name, Title FROM Contact WHERE AccountId IN :accountIds];

  • Update the Title field on contacts for (Contact con : relatedContacts) {

Contact updatedContact = new Contact(Id = con.Id); updatedContact.Title = ‘Updated Value’; contactsToUpdate.add(updatedContact);

}

  • Perform bulk update if there are contacts to update if (!contactsToUpdate.isEmpty()) {

update contactsToUpdate;

}

\ 1.\ The first SOQL query retrieves Account records created in the last month and adds their Id values to the accountIds set. This query is placed outside the loop, ensuring that only one query is executed regardless of the number of Account records.

\ 2.\ The second SOQL query retrieves Contact records based on the dynamically obtained Account Ids. Again, this query is placed outside the loop, preventing unnecessary queries inside the loop.

\ 3.\ The subsequent loop updates the Title field on the queried Contact records, and the final bulk update is performed outside the loop if there are contacts to update.

By using SOSL queries with Maps, you can avoid nested loops and efficiently retrieve and work with related records. This approach can help improve performance and avoid hitting CPU limits in your Apex code.