I am inserting record using the allOrNothing = false option to only rollback the transaction when a certain type of error occurs (Goal: Allow the insert of Custom Records even if some of them fail as duplicates).

That's what I need:

for(Database.SaveResult sr : Database.insert(chunk, false)) {
    for(Database.Error error : sr.getErrors()) {
        if(error.isOfType(..)) {
           throw error.getException();

but it seems the Database.SaveResult and its Database.Errors seem not to store Exceptions that happened during the insert. I dont' want to throw a custom exception because DMLExceptions are just perfect.

2 Answers 2


allOrNone=false does not generate a DMLException, because of the nature of how it works.

If you really wanted to throw an exception, you'd have to redo whichever record failed. I suspect something like this:

Database.SaveResult[] results = Database.insert(chunk, false);
for(Integer index = 0, size = results.size(); index < size; index++) {
    if(!results[index].isSuccess()) {
        for(Database.Error error : sr.getErrors()) {
            if(!error.isOfType(..)) {
                insert results[index];

There is no exception to re-throw. That's how partial database operations work.

If you want a DmlException in this case, you can at least throw a DmlException.

if (errorCondition)
    throw new DmlException(errorMessage);

Note that if you do throw an exception of any kind, the entire transaction is rolled back, so there won't be any partial saves either unless you catch this exception further upstream.

  • I don't want a new exception. I want the old with all that Stacktrace information from the original. Oct 5, 2017 at 15:06
  • 1
    @RobertSösemann There is no exception to re-throw. That's what happens when you execute a partial operation.
    – Adrian Larson
    Oct 5, 2017 at 15:42

You must log in to answer this question.

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