Callout from triggers are currently not supported exception/error in Salesforce

Callout from triggers are currently not supported exception/error in Salesforce

In certain scenarios, we need to make the callout from the trigger to call an external webservice however we are not able to do so as it gives the below mentioned error “Callout from triggers are currently not supported”.

An Apex trigger can execute a callout when the callout is invoked within a method defined as asynchronous: that is, defined with the @future keyword. The @future annotation signifies that the Apex method executes asynchronously.

Callouts must be made asynchronously from a trigger so that the trigger process isn’t blocked while waiting for the external service’s response. The asynchronous callout is made in a background process, and the response is received when the external service returns it. To make an asynchronous callout, use asynchronous Apex such as a future method.

Best practice is to call future callout method on after insert and after update events.

Sample Code:

Trigger:

trigger LeadTrigger on Lead ( after insert ) { 
 
    Set < Id > setLeadIds = new Set < Id >(); 
     
    for ( Lead objLead : trigger.new )  
        setLeadIds.add( objLead.Id ); 
         
    LeadTriggerHandler.updateODSLeads( setLeadIds ); 
 
}  

Apex Class:

public without sharing class LeadTriggerHandler { 
 
    @future (callout=true) 
    public static void updateODSLeads( Set < Id > setLeadIds ) { 
     
        List < Lead > listLeads = [ SELECT Id, LastName FROM Lead WHERE Id IN: setLeadIds ]; 
        system.debug( listLeads ); 
     
        /*Do the callout here*/ 
     
    } 
 
}  

Leave a Reply