5

There is a brief note at http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dml_convertLead.htm about opt_allOrNone preventing partial success of the convertLead DML operation:

The optional opt_allOrNone​​ parameter specifies whether the operation allows partial success. If you specify false for this parameter and a record fails, the remainder of the DML operation can still succeed.

If any lead conversion database operation (e.g. insertion of contact) fails with opt_allOrNone = true, are other operations rolled back?

-1

Nothing is rolled back, the DML operation simply fails. In fact, if you set a rollback point, unless your code reached the rollback point, the rollback won't occur.

In the situation you describe, if that's the behavior you desire, you'd need to set a rollback point then do a try/catch block on the DML operation where the catch block includes the rollback if the operation fails. It would look something like this:

// Set the savepoint
Savepoint sp = Database.setSavepoint();

try{

   list<Database.SaveResult> AcctCtsInsertResults = Database.Insert(AcctCts,true);

   } catch (System.DmlException e) {

   // Recover from exception by using rollback to savepoint

   Database.rollback(sp);

   // Good idea to perform a System.assert here that original values 
   // in list AcctCts has been restored. Ideally, you'd create the restore
   // point before doing all of the work to prepare your list for insertion.

  } 

Here's an example of code I've used for partial success when the option is set to false:

Map<Id,string> IdtoEvntNotes = new map<Id,string>();

// after insertion (failures allowed), check for failures and debug

if(ToInsert.IsEmpty() == false){

list<Database.SaveResult> InsertResults = Database.Insert(ToInsert,false);

    // Iterate through the Save Results
    // and create list of error messages
    // plus add error messages to IdtoEvntNotes map

    for(Integer i=0; i<InsertResults.size(); i++){
    If (!InsertResults[i].isSuccess()) IdtoEvntNotes.put( InsertResults[i].Id , 'Error reported was: ' + InsertResults[i].getErrors()[0].getMessage() + ' ' + IdtoEvntNotes.get(InsertResults[i].Id)); 

    }
}

The above code allows you to keep track of which Id's didn't insert and the 1st DML error for that operation that occurred using a map. Note that there may have been several DML errors associated with that insert operation on any particular record in the list, but this method only allows you to capture the 1st error in your map.

  • The actual question is not whether the rollback to a certain SavePoint will occur, but rather whether it is possible that the operation will be partially successful. I'm just verifying if I understood the note correctly (i.e. that partial success is not possible). – ipavlic Apr 5 '13 at 14:34
  • My bad in misunderstanding your question. If its set to false, partial success is indeed possible! When set to true, it is not. – crmprogdev Apr 5 '13 at 15:07

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.