You need to sign in to do that
Don't have an account?
Roger Wicki
Database.update() throws dml exception
Hi Community
I am new to using Database.update() method and I thought that using the Database class would not throw any kind of Exceptions. I use this as my source information:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
Any hints on that?
I am new to using Database.update() method and I thought that using the Database class would not throw any kind of Exceptions. I use this as my source information:
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
@TestVisible private void updateExistingOpportunities() { list<Database.SaveResult> results = Database.update(existingOpportunityMap.values()); String errorMessage = ''; for ( Integer i = 0; i < results.size(); i++ ) { if ( !results.get(i).isSuccess() ) { if ( errorMessage.equals('') ) { errorMessage = 'Following Opportunities have failed during the update:\n\n'; } errorMessage += existingOpportunityMap.values().get(i).Name; errorMessage += '\n'; errorMessage += 'https://eu4.salesforce.com/'; errorMessage += existingOpportunityMap.values().get(i).Id; errorMessage += 'Errors:\n'; for ( Database.Error theError : results.get(i).getErrors() ) { errorMessage += theError.getStatusCode(); errorMessage += '\n'; errorMessage += theError.getMessage(); } } } }The above is my method I like to test. I want to force running through the result loop with an error so i get maximum code coverage and see that the output is as I desired it. I tried it with an inserted Opportunity of which I tried putting the CloseDate on null. But this throws a DML Exception. I thought I would not get any exceptions, but be able to read out the Database.result...
Any hints on that?
I think your question may actually be answered in the link you provided:
I think if you change
to
that might solve your issue.
Hope this helps!
Lizzy
All Answers
I think your question may actually be answered in the link you provided:
I think if you change
to
that might solve your issue.
Hope this helps!
Lizzy
Thanks. But I think that's confusing.... So if I change it to false, it will not throw exceptions but display error information in the result array? Got to try that.
Thanks for your help, that actually did it. Either I am really bad at understanding the docs or this disambiguition is not well stated. So a Database.update(list<sObject>, true) is actually the very same as the update list<sObject> in the end.
Glad that worked!
I do see your confusion, it seems odd to have two methods that effectively do the same thing. There are other parameters you can add to database.update() (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database.htm#apex_System_Database_update_3) which allter different aspects of behaviour, but just calling Database.update(list<sObject>, true) is as you say effectively the same as calling the update DML statement.