If I call Database.update(records, false); will only the failed records be rolled back or will there be no rollback whatsoever?

Imagine this scenario, record is updated, kicks of an insert (inside trigger) of another record which fails.. I want this to rollback to the beginning (essentially reverting the update and the failed insert).

Would this rollback that singular record to the beginning because it's tracked within a given transaction? or would I end up with a record that updated and a failed insertion?

Database.xxx(someSObjectList,false); //allorNothing=false

This will not save the records that failed some validation or .addError(..) but will save the records that pass.

As such, if you have related objects that were successfully CRUD earlier in the transaction and now must be rolled back (while preserving the related objects for the passed records), then you must implement your own undo logic to clean up an inconsistent state.

This can get complicated based on the object graph you are constructing/updating as perhaps you

  • inserted a Opportunity O
  • inserted two OpportunityLineItem for Opportunity O using allorNothing=false. but one failed. Do you undo the insertion of the Opportunity? or, because one OLI passed, do you leave the Opportunity but somewhat inconsistent?
| improve this answer | |

This is not a valid scenario. Firstly, lets consider you are updating few sObjects of objA for which you are invoking Database.update(objAs,false) and in the trigger of objA, you are trying to insert few related (or unrelated) objects objB and you have DML statement Database.insert(objBs,false) in which 1 record fails - in which case remaining objects objB are created. On the contrary if you say insert objBs then the whole transaction will be reverted and even objAs are not updated.

You should notice that there can be any number of objA records and any number of objB records and there will not be 1-to-1 relation. So, when 1 record of objB fails to insert, you do not have any scope of saying which record in objAs should be reverted.

| improve this answer | |

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.