0

I have a requirement where, on changing a particular field value of an account record from Lightning page layout, I have to make a callout. If the response from the callout is not successful, then the record shouldn't be saved, and it should display an error message.

Here, I am using Process Builder and using the invocable method, I am calling a REST API. In this case, I am getting this error message:

'You have uncommitted work pending. Please commit or rollback before calling out'.

But if I use a future call, then it will be a new transaction; the record will always save irrespective of the success or failed response.

How can I achieve this requirement criteria to make it look like a synchronous transaction? Appreciate your inputs.

nica
  • 1,400
  • 3
  • 18
  • 37

1 Answers1

2

You will need to implement this validation on the front end, using a Visualforce page or Lightning component. Your custom UI - perhaps an override of the Edit action - can make a callout to validate data input prior to initiating DML at all. You then have full freedom to interact with the user's active browser session based upon the response from your web service, be that by displaying errors, showing a Toast message, highlighting problem fields, or what have you.

Once the DML operation has begun, you cannot make a callout within the same transaction. The platform will not allow you to keep a database transaction open while waiting for a callout's response. See this question for details.

David Reed
  • 92,733
  • 13
  • 84
  • 157