Skip to main content
Roger Wicki (Architonic AG) a posé une question dans #Apex
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

@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?
4 réponses
  1. 14 juin 2016, 14:14
    Hi Roger,

    I think your question may actually be answered in the link you provided:

    "One difference between the two options is that by using the Database class method, you can specify whether or not to allow for partial record processing if errors are encountered. You can do so by passing an additional second Boolean parameter. If you specify false for this parameter and if a record fails, the remainder of DML operations can still succeed. Also, instead of exceptions, a result object array (or one result object if only one sObject was passed in) is returned containing the status of each operation and any errors encountered. By default, this optional parameter is true, which means that if at least one sObject can’t be processed, all remaining sObjects won’t and an exception will be thrown for the record that causes a failure."

    When you call Database.update(), you need to pass in a second parameter, false, which allows for partial processing and returns an array of result objects. If you don't pass this second parameter, it defaults to true, and if any object errors an exception gets thrown.

    I think if you change

    list<Database.SaveResult> results = Database.update(existingOpportunityMap.values());

    to 

    list<Database.SaveResult> results = Database.update(existingOpportunityMap.values(), false);

    that might solve your issue.

    Hope this helps!

    Lizzy
0/9000