-1

I want to call an external service whenever there's an update in a record from the UI for specific attributes. And stop the update if the external call failed. I was thinking if it's possible to do so through Apex.

So the flow will be something like that:-

  1. User made some changes in a record.
  2. If let's say change is made in A or B attribute, then we will make an external API call
  3. If call fails, then we will the abort the record update here as well and show some error in the UI.

4 Answers 4

3

Unfortunately, that's just not directly possible. Once a DML operation has started, you can't perform a callout in the same transaction. This limitation is in place for two reasons: atomicity and limiting database contention.

For atomicity, imagine you were allowed to perform a callout with an in-flight DML operation. Your callout could make permanent modifications to an external system (the callout occurs) and then the DML operation could fail later for some reason, and the external system wouldn't know the transaction rolled back. This limitation prevents that scenario from happening.

For database contention, it's important to note that all databases that use record locking for DML operations risk running into a scenario where multiple transactions try to update the same record at the same time, which can cause some updates to fail as records are locked for too long. To minimize this risk, the engineers designed this limitation to reduce the maximum possible time the database can be locked.

There's a number of possible options for the UI, though. You could use a Flow, Visualforce, Aura, or Lightning Web Component to call the third-party API before saving the record. If you need this behavior from an actual Salesforce API call, you'd have to use a webservice method or @RestResource class, and force users to use this custom SOAP/REST endpoint instead of the standard API.

10
  • Thanks for your response. Yes we need it from UI only, didn't know if external callout is possible through flows, Can you link me to some docs around this ?
    – ashwini571
    Dec 11, 2022 at 15:29
  • @ashwini571 help.salesforce.com/s/… ... trailhead.salesforce.com/content/learn/modules/… ... should help you get started. The main topic you're looking for is "External Services".
    – sfdcfox
    Dec 11, 2022 at 16:02
  • Btw, how does this prevent the scenario where we made an update in external system and then the update fails in salesforce and we end up with inconsistent state
    – ashwini571
    Dec 11, 2022 at 16:22
  • @ashwini571 I was explaining that the callout after DML restriction is what prevents inconsistent state with the external system. Here, you're describing a type of validation, so you need to do that validation before trying the DML.
    – sfdcfox
    Dec 11, 2022 at 16:33
  • Got it. Thanks @sfdcfox
    – ashwini571
    Dec 11, 2022 at 16:58
0

To achieve this, before calling your actual Update method, you should first make external API call and if it passes then only you should run your logic /call your Update Apex method.

0

You could try keeping your external API call in a @future call from your trigger. Irrespective of whether your insert was successful or not. To keep data sanity, you may develop a cleanup job.

1
  • 1
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 11, 2022 at 19:25
0

As other answers have said this needs a bit of a workaround to work. One way you could do this is to have a screen flow that could be set to a button to change. You could have a screen to collect the information on the fields A and B that you need to validate, then the next screen you can have an apex external call out to find if this can be updated, if so do the update via flow, if not provide an error message to the user.

2
  • Yeah, but do you have any idea how we can do this while creating the record ?
    – ashwini571
    Dec 13, 2022 at 17:58
  • Your original question did not ask about creating a new record. if you want this then most likely you will need a flow that will collect the information for a new record (perhaps over a couple of screens), then make the external call out then report any problems before creating the record. Fairly standard flow mechanics.
    – Andy Nix
    Dec 14, 2022 at 14:26

You must log in to answer this question.

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