I have 3 objects A, B and C. First A object which contain some amount of records. And I need to update or create a new record to the second B object. The condition is, if the user exist in the second B object then need to update else need to create the user there with information.

i.e. I need to do update and insert in the same method. If the Update and Insert is success then to update to object C, else roll back both the insert and update.

Does the roll back feature work for Update?

  • Are you asking whether roll back works with updates?
    – Matt Lacey
    Feb 27, 2014 at 4:18
  • yes when i see the examples it only shown for insert.
    – AnuRaj
    Feb 27, 2014 at 4:22

2 Answers 2


Invoking Database.rollback not only rolls back inserts, but it goes much, much further.

It also rolls back:

  • any DML operations, including insert, update, delete, undelete, convert lead, etc.
  • sending of email: any calls to Messaging.sendEmail after the savepoint will be undone, meaning the email will be removed from the message queue of email to send (which is why email is only sent after your apex script completes).
  • Calls to @future methods or Queuable jobs. Any methods called within the rolled back part of your script will not be invoked (also why @future doesn't start until your script completes).

It's worth noting that you can't serialize or otherwise preserve savepoints beyond the life of the script they were created in. For example, if a visualforce action method is invoked and it creates a savepoint a controller property would would not be able to rollback to that savepoint from a different action. Once the action that invoked apex is done the savepoint is not longer valid.

Rolling back also doesn't invoke triggers, validations, etc like a delete operation would. So once the savepoint is gone there's no way to guarantee you can restore things to their previous state.

  • "also why @future doesn't start until your script completes" - is it documented somewhere? Apr 8, 2015 at 14:06
  • Not that I know of, but it's quite apparent that it's the case since rollback or an unhanded exception stops the execution - which wouldn't be possible otherwise. Apr 8, 2015 at 15:15

Yes, you can use rollback with updates, there's an example shown in the documentation on transaction control.

Account a = new Account(Name = 'xxx'); insert a;
System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].

// Create a savepoint while AccountNumber is null
Savepoint sp = Database.setSavepoint();

// Change the account number
a.AccountNumber = '123';
update a;
System.assertEquals('123', [SELECT AccountNumber FROM Account WHERE Id = :a.Id].

// Rollback to the previous null value
System.assertEquals(null, [SELECT AccountNumber FROM Account WHERE Id = :a.Id].

You must log in to answer this question.

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