3

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 '14 at 4:18
  • yes when i see the examples it only shown for insert. – AnuRaj Feb 27 '14 at 4:22
13

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? – Bart Juriewicz Apr 8 '15 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. – ca_peterson Apr 8 '15 at 15:15
4

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].
                           AccountNumber);

// 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].
                             AccountNumber);

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

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.