0

After a record is updated in Salesforce, i need to perform a callout to synchronize data to an External System.

Ideally, i would like to do this kind of synchronously, however it doesn't seem to be achievable. I read this document from Salesforce, where it is said that we can perform callouts after publishing Platform Event messages. However, there is a note saying: "This feature doesn't change the way you make Apex callouts from Apex triggers, which require the @future(callout=true) annotation."

My questions is more for confirmation: Is there a chance that in a Trigger i publish a platform event, and after the event is published to perform a callout to the external system? I suppose no, but i ask more for confirmation.

From my understanding, we should use @future methods, however it also needs handling concerning the Order of execution (update the record now and in 2 seconds - callouts could be performed in a different sequence, since they are asynchronous.

2 Answers 2

4

Platform Event Triggers also can't make callouts, so you'd still need a Future or Queueable method. Note that asynchronous methods are still called in order of being queued, so you don't really need to worry about the "ordering problem."

1
  • Thanks for your answer @sfdcfox! Commented Oct 26, 2021 at 5:55
2

You can achieve this now with Platform Event Triggered flows.

Once you publish a platform event. You can configure and flow to listen to this and then from the Flow configure it to call an Apex Action.

NOTE : When consuming the params in the invocalble request DO NOT USE strings.get(0), as Platform events batches events raised in same time and then passes it to the Apex action. So we should iterate the input params here.

public without sharing class SendExternalAPIRequestPE {

    @InvocableMethod(Label='test callout' Callout=true)
    public static void fireAPIRequest(List<String> strings){
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://jsonplaceholder.typicode.com/users');
        req.setMethod('GET');
        HttpResponse res = new HttpResponse();
    }
}
6
  • Your note is confusing. Please clarify.
    – Phil W
    Commented May 21, 2022 at 9:37
  • 1
    Modified my explanation. Hope it makes sense now. Commented May 21, 2022 at 9:58
  • 1
    It is worth noting that this probably works because the invocable method is actually handled in a separate transaction against the org.
    – Phil W
    Commented May 21, 2022 at 10:25
  • Yes and as transaction is separated, it makes a perfect tool to fire callouts in near real time from trigger, if Async queue is overloaded. Commented May 21, 2022 at 10:49
  • @PhilW The statement is not entirely true in real world example, as I am already facing the issue where sometimes it does not consider the transaction as a new one. salesforce.stackexchange.com/questions/376639/… Commented May 21, 2022 at 12:10

You must log in to answer this question.

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