0

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

Yes, you can use Savepoints for Transaction Control

The code will look like below:

Savepoint sp = Database.setSavepoint();    
try{
 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 '17 at 17:14

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.