2

I would like to know if this is technically possible from Salesforce to make an API call from a trigger before insert/update to check the data of a record with an API that would respond TRUE/FALSE based on the data of the record.

Therefore, I plan on doing: - Before Insert/Update trigger - Send a web service call to an external API with data of the new record we plan to insert/update - Receive from the web service a TRUE/FALSE: TRUE means that creation can be done, FALSE means that the creation/update can't be done, with an error message returned additionally. - If error, display the error message on Salesforce

I know this can be done easily coding everything within Salesforce but my idea is to externalize this out of Salesforce

Thanks

1 Answer 1

7

No, it is not technically possible. Synchronous callouts in Triggers are not allowed. There is an Idea to have Salesforce do this, but it's already a decade old, and may never be implemented. The main concern about callouts during triggers is that it will significantly increase database contention, which may cause deadlocks as the database has to hold record locks for potentially up to 2 minutes, and/or resource exhaustion if too many callouts happened at once.

As a comment from salesforce.com says:

This is something that we can't offer without jeopardizing our service. If you're in a trigger, you have a database connection open. If you make a callout, and that callout endpoint is hung, your connection is hung. That's one connection that your fellow multi-tenants cannot use. If your trigger is hit by a few hundred users, our service is blocked.

This is the reason why we do not allow DML in a method prior to making a callout, even in asynchronous context.

If we were to impose very aggressive timeouts on callouts to reduce the impact of the above scenario, we'd only make programmers frustrated with the too-tight limits, and I don't know that we'd add much to service protection.

Alternative solutions include creating a Flow, and having users enter data through the Flow, which can then callout and verify the data before creating a new record, using a Visualforce page or Lightning component to collect and validate the data, or performing the callout asynchronously and later just showing an error somehow on the record after it's created.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .