Hello Everyone, In this tutorial, I am going to explain about "Callout from triggers are currently not supported" exception and also provide a solution to solve this problem that helps you a lot to make callout request from the trigger. In this unit, I will tell you the reason why this error occurred while callout request.

After completing this tutorial, you’ll able to:
  • Know the reason of System.CalloutException.
  • Solve System.CalloutException.
So let's begin,

Reason of System.CalloutException

In certain scenarios, you need to make the callout from the trigger to call an external web-service, third-party API's but when you try to do this, you'll get "System.CalloutException: Callout from triggers are currently not supported" exception.




Solution of System.CalloutException

To solve this error, simply encapsulating the callouts in a @future method, after that you can invoke callouts from triggers.

Syntax:
 public class CalloutClass{
     @future(callout=true)
     public static void myMethod(String a){
          //long-running Apex code
     }
 }

Example:

Apex Trigger 
 trigger Mytrigger on Account (after insert,after update) {
 IPGeolocation.getIPGeolocation();
 }
Apex Class 
 public class IPGeolocation {    
   
    @future(callout=true)
    public static void getIPGeolocation() {        
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('http://api.ipstack.com/47.9.161.12?access_key=84cce4c3f6b699dc4');       
        req.setMethod('GET');
        HttpResponse res;
        res = h.send(req);
        System.debug('Result'+res.getbody());  
    }    
 }

See also:

Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.