1

I am trying to insert 2 objects and catch the error and show it in visualforce page but when I catch the error of second object first object I insert do not get rollback.

                 lead objlead = new lead(); 
                 objlead = data;
                 try{  
                    upsert objlead;
                    } 
           catch(DMLException e){

                       ApexPages.addMessages(e);
                       return null;
                }

                 test__c objtest = new test__c();
                 objtest = objWrap.objeoi;
                 objtest.Lead__c = objlead.id;
                  try{  
                     insert objeoi;
                    } 
           catch(DMLException e){


                        ApexPages.addMessages(e);
                        return null;
                }

i use roll-back but it is not good solution. i want alternative.

4
  • Please explain why Roll back is not a good solution??
    – Mr.Frodo
    Commented Jul 3, 2018 at 6:02
  • when we define a savepoint it takes the snap of entire database. A system has no idea what is going to change in its database after that. and when an error occurs it will put it to that database point. so my concern is when 50 users are using a same visual force page and error occur in one person it will affect other user data as well.
    – ajay rawat
    Commented Jul 3, 2018 at 6:24
  • Is it just your thought or it happened to you, because in my opinion every user has their own transaction and i think it only rollbacks that transaction.
    – Mr.Frodo
    Commented Jul 3, 2018 at 6:26
  • ok thanks, I am currently using the same. Hope it will not affect other user data. thanks for your time @Mr.Frodo
    – ajay rawat
    Commented Jul 3, 2018 at 6:34

2 Answers 2

5

Actually you can use Database.setSavePoint to rollback and show error message, Multiple users using same page shouldn't have any problem because every user has their own transaction:

Savepoint dbSave = Database.setSavepoint();  
try{  
    insert myObj;
    insert myOtherObj;
}catch(DMLException e){ //undo all of the failed operation.
    ApexPages.addMessages(e);
    Database.rollback(dbSave);

}
3
  • yes I did the same but database.setsavepoint() take the snap of entire database and it 50 users are using it. and one makes an error then entire database gets rollback.
    – ajay rawat
    Commented Jul 3, 2018 at 5:55
  • 1
    @ajayrawat Are you sure, I think every user has their own transaction. AFAIK it shouldn't rollback other user's transactions.
    – Mr.Frodo
    Commented Jul 3, 2018 at 5:58
  • 2
    @ajayrawat Database.setSavepoint doesn't "take the snap of entire database." It creates a transaction restore point, where any changes that occur in the current transaction are tracked, and can be rolled back with Database.rollback. Two users performing actions at the exact same time won't affect each other.
    – sfdcfox
    Commented Jul 3, 2018 at 6:51
0

You can also use the

upsert(recordToUpsert, externalIdField, allOrNone)

type of the upsert method. With the last parameter you can say that you don't want to upsert any records of one fails.

1
  • but they are 2 different objects if I do this problem remains the same. if error occur in second object it will not insert 2 object but still insert the first one.
    – ajay rawat
    Commented Jul 3, 2018 at 6:04

You must log in to answer this question.

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