0

Below is the code I am executing.

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);

When I used system.debug(Limits.getDmlStatements()); at the end it gave me result as 4.

  1. How many times a rollback executes generally. Will it executes recursively?
  2. Also is it the case that the Database.rollback and savepoints count towards the total number of DML statements between them?
2

A rollback executes once and sets the database to exactly the state it was in when at the Savepoint. As for your limits question, it is easy to verify for yourself with the following anonymous script:

Savepoint initialState = Database.setSavepoint();
system.debug(Limits.getDmlStatements()); // yields 1

insert new Account(Name='Demo');
system.debug(Limits.getDmlStatements()); // yields 2

Database.rollback(initialState);
system.debug(Limits.getDmlStatements()); // yields 3
  • Hi Adrian,could you help me with understanding point: "References to savepoints cannot cross trigger invocations because each trigger invocation is a new trigger context. If you declare a savepoint as a static variable then try to use it across trigger contexts, you will receive a run-time error." – Pankaj Andhale Sep 22 '18 at 4:46
  • @PankajAndhale If you have another question, please ask it in a separate post. – Adrian Larson Sep 22 '18 at 5:01
2

How many times a rollback executes generally. Will it executes recursively?

You can have up to 5 save points active at once per transaction. Rolling back the second save point in five, for example, will roll back to that save point, invalidating the third, fourth, and fifth in the process.

Also is it the case that the Database.rollback and savepoints count towards the total number of DML statements between them?

Yes, you don't get any of your limits back just because you rolled back. The rollback itself counts as one. This also includes heap limits, CPU limits, etc.

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.