We have a page inserting three different object types. Historically, it has attempted to insert one and, assuming no errors (try and catch), moved to the next. Until all objects were inserted. If there was an error on the third object type, it would delete the first two inserted objects, give the user a chance to fix the errors, and then try again.

We are now using different permission sets, and not all users have the ability to delete these objects. Is there a way to check for insert errors all at once so we can avoid deleting the already inserted object?

1 Answer 1


Yes, you can use Savepoints for Transaction Control

The code will look like below:

Savepoint sp = Database.setSavepoint();    
 insert obj1;
 insert obj2;
 insert obj3;
Catch(Exception ex){
 //If an exception occurs, rollback the transaction --> no records will be inserted
 Database.rollback( sp );
  • A little embarrassed I didn't think to use a save point. Thanks Akram!
    – mplax
    Nov 9, 2017 at 17:14

You must log in to answer this question.

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