3

Is Apex transaction control equivalent to .Net transaction scope where it only rolls back the transaction within scope?

Will it rollback all the records that has been inserted between Database.setSavePoint and Database.rollback at global level or function level?

Function level is anything within the scope. In the case below, it will rollback A, B, and C

Function A(){
     Database.setSavePoint();
     //insert record A
     //insert record B
     //insert record C
     Database.rollback()
}

An example of Global level is, User1 trigger FunctionA (above), and when above function is inserting record B, another user (User2) inserts a RecordD from other function.

In this case, when FunctionA executes Database.rollback, will it rollback RecordD inserted by User2?

2 Answers 2

5

Every separate request made by a user is an isolated transaction. In general, no transaction can directly interfere with another transaction (so, for example, a SavePoint will not undo records in a separate transaction).

Database.setsavePoint sets a save point in the current transaction, and Database.rollback undoes any changes that occurred from the time Database.setsavepoint was called. Note that it applies to the current transaction, not the current "function." For example:

void methodA() {
    SavePoint sp = Database.setSavePoint();
    methodB();
    Database.rollback(sp);
}
void methodB() {
    insert new Account(Name='Test Account');
}

In this example, the new account created in methodB() is rolled back in methodA(). However, any other accounts created by other users in other transactions will not be rolled back.

Note that SavePoint has some restrictions on its use. For example, if a SavePoint is set in triggerA, it is an error to use that same SavePoint in triggerB (say, by storing the SavePoint in a static variable).

0
  1. Is Apex transaction control equivalent to .Net transaction scope where it only rolls back the transaction within scope? Yes
  2. Will it rollback all the records that has been inserted between Database.setSavePoint and Database.rollback at global level or function level? (Yes it will rollback)
  3. Database.rollback() runs in user context so any record inserted by user 2 will not effect.

Read more about transaction control here

Apex & .NET Basics trailhead will help you in transition from .NET to SFDC

You must log in to answer this question.

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