What is the problem with database update?

874    Asked by AishwaryaJhadav in Salesforce , Asked on Sep 22, 2022

 I have some accounts to update but some validation rules can fail.

So to avoid this I use the Database. Update() function to update accounts even if some DML exception occurs.

try {
        acc.Name = 'test';
    }
 system. Debug ('one');
 system. Debug('two');
} catch(Exception e) {
    system. Debug (txt);
}
    List accounts = [select Id from Account];
  for(Account acc : accounts) {
 List result Database. Update(accounts, false);
    String txt = e.getMessage();

But only the first log is displayed. I don't have the second one and I don't reach the catch block. I can see that some validation rules fail on some account names.

Do you know what happens ? If a DML exception occurs on a Database. Update() what happens ?

Answered by AI

The reason your catch block is not being executed is because of the false parameter you are passing in: Database update(accounts, false); That parameter is called is_all_or_none meaning treat the transaction atomically. If something fails, then make the entire transaction fail. The reason for atomicity is pretty standard here...if one of my records fails to save, if an error occurs, I want to ensure that everything I attempted will roll back, preventing data from being out of sync.

When you set this to false, what that means is that if one record has an exception, validation rule failure (which actually is just a thrown exception), Trigger validation failure (myObje.addError(...) which also is just a thrown exception) that one record will fail to save, but any other records in the batch will complete just fine. In your description, you write:

So to avoid this I use the Database. Update() function to update accounts even if some DML exception occurs.

This is a misunderstanding of the purpose as you can hopefully now see. Using Database. Update isn't a way to bypass validation rules. It is a way to prevent one validation rule failure from causing many good records from failing. To get your save to work, you need to understand what your validation rule failure is, and then attempt to save data that meets the validation criteria that have been set in your system. You could also evaluate whether this validation rule is actually meaningful anymore and make a choice to remove it, but I would not recommend this without thoughtful review, and checking with others who are using your org. As to why the second System. Debug() is not being called (system. Debug('two')) that is extremely puzzling. Are you certain it is not? Finally, when you use the is_all_or_none=false version of DML (it also exists for insert, upsert, delete, and undelete), the way you look for failed records is as follows:

List

 results = Database. Update(accounts, false);

for (Database.SaveResult result : results) {

  if (!result.isSuccess()){

    for (Database.Error err : result.getErrors()){

      System. Debug('Error: '+ err.getStatusCode() + ' ' + err.getMessage());

    }

  }

}



Your Answer

Interviews

Parent Categories