We are working on one web-service call in after trigger. As we can not call synchronous web-service call in trigger, we are using future call-out. But we want to rollback the changes if web-service call fails or throws the error after completion.

One way, store the old values is dummy-records and when want to roll back the changes, update the record using dummy-record and delete the dummy-record.

Is there any other way to rollback the changes?

Thanks in advance:)

  • Use a batch job instead. – Eric Oct 13 '16 at 12:04
  • Can you please explain how we can achieve this as both will be asynchronous call for web-service and will have same issue for rollback – Swapnil Kulkarni Oct 13 '16 at 12:24
  • Because in a batch you can control the transaction. But I see your point if the trigger for everything is a change prior then your only choice is to store the changes. I would look at your requirements as you may be going about it wrong. What is the web service doing? – Eric Oct 13 '16 at 12:39
  • We are using 3rd party system to validate the records and in response we will receive validated record with few more values that we are storing in our record. In this. As this will be the call on update of record, we want to revert back the changes if there is any failure in web service call – Swapnil Kulkarni Oct 21 '16 at 11:53

As you realise, the trigger's transaction will have committed before the web service response fails. So you have to move into the area of compensating transactions i.e. providing a way to undo what has been done.

Exactly how you do that will depend on what changes were committed e.g. if its just one field change you could store the previous value (in another field on the same object) and restore that on the fail. If it is many field values or many rows then the solution gets more complicated.

  • 2
    Or if you get into a cascading triggers situation. You could end up with modifications to records many relationships distant. – Adrian Larson Oct 13 '16 at 15:17

Depending on the performance hit, you will have to model it in your environment

  1. Create a long text field
  2. JSON.serialize the trigger.old value, or class representing the data you are tracking, and store it on that field.

to rollback, simply deserialize and update.

Another way is create a class to have only the fields you are interested in and deserialize the trigger.old into that and serialize the class. Then to undo, deserialize the string into the class and update your record with that data.

You will have to weigh pcost of CPU time vs storage costs. But adding the field onto the record does not increase data storage. However, Serializing and deserializing is expensive.


You can use history object to assign old values back to the record once webservice gets failed. For this you have to enable history tracking on fields which needs to be rolled back in transaction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.