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)];
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);
\ 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:
while (true) {
SELECT Id, Name FROM Account WHERE Id > :lastRecordId ORDER BY Id
LIMIT :batchSize
];
Long endHeapSize = Limits.getHeapSize();
break;
}
break;
}
}
}
\ 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.