fbpx

Free Salesforce Developers Tutorials >

Chapter 8 - Apex DML and Database Methods >

Transaction Control and Rollback

Transaction Control and Rollback

What You’ll Learn


S2 Labs

Savepoint setSavepoint():

Returns a savepoint variable that can be stored as a local variable which we can use with a rollback method to restore the database to that point.

Void rollback(Savepoint sp):

Restores the database to the state specified by the savepoint argument. Any emails submitted since the last savepoint are also rollbacked and not sent.

Savepoint sp1 = new Database.setSavepoint();
List < Account > accList = [SELECT name, phone FROM account WHERE name like‘ Acc % ’];
integer i = 1;
for (Account a: accList) {
    A.phone = i + ’’;
    i += 111;
}
update accList;
Savepoint sp2 = Database.setSavepoint();
for (Account b: accList) {
    b.name = b.name + ’ss’;
}
update accList;
Savepoint sp3 = Database.setSavepoint();
if (roll = 1) Database.rollback(sp1);
else if (roll = 2) Database.rollback(sp2);
else if (roll = 3) Database.rollback(sp3);
salesforce-developer

Download Study Material

Get access to exclusive study material for Salesforce Certification and ace your exams!

Download Now

Our Salesforce Certification Courses

Hey there! Glad you made it through our Salesforce Developer Training for beginners . But wait! We've got some high-in-demand Salesforce courses for you to take your Salesforce skills to the next level, making you a desired professional in the Salesforce job market.

Post a Comment

Your email address will not be published. Required fields are marked *