How to do Callouts from Apex Triggers

Salesforce Logo
Sharing is Caring

In this blog post, we’ll discuss how to correctly do Callouts from Apex Triggers. Salesforce doesn’t allow External Callouts to directly happen from Apex Triggers. You will get the error “Callout from triggers are currently not supported’.

Why doesn’t Salesforce Allow External Callouts from Triggers?

Salesforce doesn’t allow external callouts from triggers because they need to execute quickly and shouldn’t be held up by anything.

There’s three ways we can potentially solve the error “Callout from triggers are currently not supported”. All of the methods involve potentially using asynchronous methods. We can use Future Methods, Batch Apex, or Outbound Messaging.

Doing Apex CallOuts Through Future Methods

Future Methods are asynchronous methods that can be fired at any point after they are called. The methods go into a queue and are processed later. The blog post What are Salesforce Future Methods? covers them in depth. They are the easiest solution but they don’t necessarily scale well.

And then we have a static method that would do the actual calling out.

Future methods should be used cautiously as there is a restriction on the number of future methods that can be executed per 24 hour period. The limit is 200 method calls per full Salesforce User License. So for example, 10 full Salesforce users are limited to 2000 calls per 24 hour period.

The other problem with future methods is that there’s no guarantee how long it will remain in the queue. It could be executed immediately or it could be executed minutes later. The only guarantee from Salesforce is that it will be executed at some point.

Doing Apex Callouts Through a Batch Method

In Salesforce, a batch can do HTTP callouts per batch. To get around this limit you could potentially do smaller sized batches – ie a scope of 5 or something so 5 records are handled at a time.

Outbound Messaging

Outbound Messages work by a certain event triggering a message to be sent to an endpoint that you setup. The endpoint receives a message that includes the fields specified in the outbound message and then acknowledges after it does any processing. It works by using SOAP and WSDL file.

Sharing is Caring