I have a custom REST API which is invoked with a payload, which contains items to purchase and CC Info. The custom REST API should return the status of the transaction synchronously to indicate that the order has been processed, and the payment has gone through. Logically, one would create the records and, if that succeeded without error, then make the callout for the payment. So the Apex class behind the REST API will do 2 things (i) create the records for the items to purchase, and (ii) make a PayPal API call for the payment. But this is not possible, since callouts cannot be made after a DML. What is the best way to get round this? I want to avoid making the callout first, because then creating the record after the callout could potentially throw an error after the payment has gone through.


1 Answer 1


I think the best way to solve this is by using platform events instead of the custom objects in your design.

With platform events note that you will still not be able to publish the event before making callout because of an issue mentioned here (I hope in future this issue is rectified because it does not look like a technology limitation at this point), However note that if you perform a callout and then release a platform event it will be better than doing a DML on a custom object.

Because platform events can be replayed back and you add some retry mechanism to the events.

If you are building for an ISV note that at this point the Professional Edition orgs do not support platform events.

You must log in to answer this question.

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