Posts

Showing posts with the label Apex Class

Five-Level Parent-to-Child Relationship SOQL Queries in Apex

Apex now supports traversing up to five levels of parent-child records with SOQL relationship queries. Each subquery counts towards the aggregate queries processed in an SOQL query statement. Starting from API version 61.0, SOQL parent-child relationship queries in Apex can be structured such that the first level of the query is a parent root, and child relationships can be queried up to four levels deep from the parent root. You can keep track of the number of aggregate queries processed in an SQL statement using the Limits.getAggregateQueries() method. The Limits.getLimitAggregateQueries() method returns a value of 300, the total number of aggregate queries that can be processed with SOQL query statements. This change applies to all editions. Below is an example of a Salesforce SOQL query statement with five levels of parent-child relationships. List<Account> accts =  [SELECT Name,     (SELECT LastName,         (SELECT AssetLevel,    ...

Use Apex Cursors for Expanded SOQL Query Result Support

Apex cursors allow you to process large query results without returning the entire result set in a single transaction. With cursors, you can traverse query results in parts and navigate forward and back. This makes it easier for developers to work with high-volume and resource-intensive jobs. Cursors are a great alternative to batch Apex as they can be used in a chain of queueable Apex jobs and are more powerful in many ways. They are especially useful for package and advanced developers who regularly work with large query result sets. NOTE: This feature is a Beta Service, which applies to all the salesforce editions. Apex cursors are stateless and generate results from the offset specified in the Cursor. fetch(integer position, integer count) method. You must track the offsets or positions of the results within your particular processing scenario. A cursor is created when a SOQL query is executed on a Database.getCursor() or Database.getCursorWithBinds() call. When a Cursor. fetch(...

System.CalloutException: Callout from triggers are currently not supported

The error message "System.CalloutException: Callout from triggers is currently not supported" indicates that a trigger in your Salesforce org is attempting to make a callout to an external system, which is not allowed. Salesforce does not allow callouts to be made directly from triggers. This restriction ensures the integrity and consistency of data in the Salesforce database. If handled improperly, callouts can result in long-running transactions, leading to performance issues or data inconsistencies. For example, when a new Account record is inserted and this trigger fires, you encounter the "System.CalloutException: Callout from triggers are currently not supported" error. trigger AccountTrigger on Account (after insert) {     for (Account acc : Trigger.new) {         // Make a callout to an external API         HttpRequest req = new HttpRequest();         req.setEndpoint('https://api.example.com');    ...