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');
        req.setHeader('Accept', 'application/json');
        req.setHeader('Content-Type', 'application/json');
        req.setMethod('POST');
        req.setBody(JSON.serialize(acc));
        Http http = new Http();
        HttpResponse res = http.send(req);
        // Process the response or perform additional logic
    }

}

We can refactor the trigger to use a future method to perform the callout asynchronously.

public class AccountTriggerHandler {
    // Define a future method to perform the callout asynchronously
    @future
    public static void makeCallout(List<Account> newAccounts) {
        // Make a callout to an external API
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.example.com');
        req.setMethod('POST');
        req.setHeader('Accept', 'application/json');
        req.setHeader('Content-Type', 'application/json');
        req.setBody(JSON.serialize(newAccounts));
        Http http = new Http();
        HttpResponse res = http.send(req);
        // Process the response or perform additional logic
    }
}

trigger AccountTrigger on Account (after insert) {
    // Invoke the future method to perform the callout asynchronously
    AccountTriggerHandler.makeCallout(Trigger.new);

}

In this example:

We've created a new Apex class AccountTriggerHandler with a static future method makeCallout.

The callout logic is moved to the makeCallout method, annotated with @future to execute asynchronously.

The trigger AccountTrigger invokes the makeCallout method with the list of new Account records after they have been inserted.

Using a future method allows the callout to be performed asynchronously outside the context of the trigger, thus avoiding the "System.CalloutException: Callout from triggers are currently not supported" error. This approach adheres to Salesforce's platform restrictions and ensures that callouts are made at an appropriate time and in a manner that complies with Salesforce's governor limits.




Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach