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.
Reason of System.CalloutException
See also:
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.
Example:
Apex Trigger
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:
- Organization-Wide Address in Apex
- Switch Statements in Apex
- System.ListException: Duplicate id in list error in Salesforce
Conclusion:
Hope you like this tutorial, for any query or suggestions please feel free to comment.
Thank you.
4 Comments
Very clear, everything needed to illustrate the concept is there and no clutter. Thank you!
ReplyDeleteThanks dude...!!
DeleteI wonder, if i want my callout to return some result to use in trigger. how can i do it?
ReplyDelete"I wonder, if i want my callout to return some result to use in trigger. how can i do it?"
Deleteplease share the solution for this.
Post a Comment