Here’s an example – Create Queries with Best Practice – SOQL and SOSL – Salesforce Certified Platform Developer I Study Guide

Here’s an example to illustrate this approach. The code starts by defining a list of SObject types (searchTypes), which includes Account and Contact. It initializes a map (relatedRecordsMap) to store related records based on their SObjectType.

List<SObjectType> searchTypes = new List<SObjectType> {Account.SObjectType, Contact.SObjectType};

Map<SObjectType, List<SObject>> relatedRecordsMap = new Map<SObjectType, List<SObject>>();

// Build the SOSL query

List<List<SObject>> searchResults = [FIND ‘{search term}’ RETURNING Account(Name), Contact(Name)];

  • Iterate over the search results and populate the Map for (Integer i = 0; i < searchResults.size(); i++) {

List<SObject> records = searchResults[i]; SObjectType objectType = searchTypes[i]; relatedRecordsMap.put(objectType, records);

}

// Access the related records from the Map

List<SObject> accountRecords = relatedRecordsMap.get(Account.SObjectType); List<SObject> contactRecords = relatedRecordsMap.get(Contact.SObjectType);

  • Perform operations on the related records

\ 1.\ The SOSL query is designed to search for a specified term across both Account and Contact objects in a single query. This is more efficient than running separate queries for each object type.

\ 2.\ The use of a map (relatedRecordsMap) allows to organize the related records by their SObjectType. This structure provides a convenient way to access and work with the records for each object type without the need for additional nested loops.

\ 3.\ Bulkifying your code by using SOSL and maps helps in avoiding CPU limits. This is important in Salesforce Apex, where governor limits are enforced to ensure the efficient use of resources.

\ 4.\ By minimizing the number of queries and efficiently organizing related records, the overall performance of your Apex code is likely to improve. This becomes especially crucial when dealing with large datasets or executing code in a bulk context.

By using Limits.getHeapSize and implementing pagination, you can effectively manage the heap size and avoid hitting heap size limits in your Apex code. The same approach can be applied to SOQL and SOSL queries as well.

Here’s an example of using Limits.getHeapSize and pagination limit of 100 to check the heap size before and after executing the SOQL query:

  • Set the batch size for pagination Integer batchSize = 100;
  • Set the initial offset for pagination Id lastRecordId = null;
  • Initialize a list to store the retrieved records List<Account> accounts = new List<Account>();

while (true) {

  • Check the heap size before executing the query Long startHeapSize = Limits.getHeapSize();
  • Query records with pagination using WHERE and ORDER BY List<Account> queryResults = [

SELECT Id, Name FROM Account WHERE Id > :lastRecordId ORDER BY Id

LIMIT :batchSize

];

  • Check the heap size after adding records

Long endHeapSize = Limits.getHeapSize();

  • Check if adding the current batch exceeds the heap size limit if (endHeapSize – startHeapSize >= Limits.getLimitHeapSize()) {
  • Perform necessary operations when approaching heap size limit
  • Break the loop to avoid exceeding the heap size limit

break;

}

  • Add the retrieved records to the list accounts.addAll(queryResults);
  • Break the loop if all records have been retrieved if (queryResults.size() < batchSize) {

break;

}

  • Update the last record ID for the next batch lastRecordId = queryResults[queryResults.size() – 1].Id;

}

  • Process the retrieved records for (Account account : accounts) {
  • Perform necessary operations on each account

}

\ 1.\ The code checks the heap size before and after executing the SOQL query using Limits.getHeapSize. If adding the current batch of records would exceed the heap size limit, the code performs necessary operations and breaks the loop to avoid exceeding the limit.

\ 2.\ The code implements pagination to retrieve records in chunks, preventing issues related to hitting the query row limit. This is essential when dealing with large datasets, as Salesforce imposes limits on the number of records you can retrieve in a single query.

\ 3.\ The approach of using Limits.getHeapSize and pagination is applicable to both SOQL and SOSL queries. It’s a good practice to monitor and manage the heap size whenever you are dealing with data retrieval and processing in Apex.

In Salesforce, you can use both Developer Console and Workbench https:// workbench.developerforce.com/login.php as additional web-based application provided by Salesforce to execute SOQL and SOSL queries. Workbench is a versatile, user-friendly tool for various tasks, while Developer Console is focused on code development and debugging but includes query capabilities for developers working within that environment. Choose the tool that best aligns with your specific needs and workflow.