Activity Forums Salesforce® Discussions How to capture errors after using Database DML methods in Salesforce?

  • shariq

    Member
    September 22, 2018 at 4:57 pm

    List<Contact> lstContact = new List<Contact>();
    Contact con = new Contact (lastName = 'Zaa', SQL_Server_Id__c='3',firstName='Jitendra');
    lstContact.add(con);
    //.. Other Contact records added in List
    Database.UpsertResult[] results = Database.upsert( lstSGAccOppInsert, Contact.SQL_Server_Id__c.getDescribe().getSObjectField() ,false ) ;

    for(Integer i=0;i<results.size();i++){
    if (!results.get(i).isSuccess()){
    Database.Error err = results.get(i).getErrors().get(0);
    System.debug('Error - '+err.getMessage() + '\nStatus Code : '+err.getStatusCode()+'\n Fields : '+err.getFields());
    }
    }

  • Parul

    Member
    September 22, 2018 at 5:52 pm

    List<Account> acs = new List<Account>();
    acs.add(new Account());
    List<Database.SaveResult> srs = Database.insert(acs, FALSE);
    String msg = '';
    for(Integer idx = 0; idx < srs.size(); idx++) {
    Database.SaveResult sr = srs[idx];
    if (!sr.isSuccess()) {
    msg += 'Account: ' + acs[idx].Name + ' failed!';
    for (Database.Error er : sr.getErrors()) {
    msg += 'Error (' + er.getStatusCode() + '):' + er.getMessage();
    msg += '\r\n';
    }
    }
    }
    System.debug(msg);

  • Parul

    Member
    September 22, 2018 at 6:02 pm

    Returned Database Errors
    While DML statements always return exceptions when an operation fails for one of the records being processed and the operation is rolled back for all records, Database class methods can either do so or allow partial success for record processing. In the latter case of partial processing, Database class methods don’t throw exceptions. Instead, they return a list of errors for any errors that occurred on failed records.

    The errors provide details about the failures and are contained in the result of the Database class method. For example, a SaveResult object is returned for insert and update operations. Like all returned results, SaveResult contains a method called getErrors that returns a list of Database.Error objects, representing the errors encountered, if any.

    Example
    This example shows how to get the errors returned by a Database.insert operation. It inserts two accounts, one of which doesn’t have the required Name field, and sets the second parameter to false: Database.insert(accts, false);. This sets the partial processing option. Next, the example checks if the call had any failures through if (!sr.isSuccess()) and then iterates through the errors, writing error information to the debug log.

     

    // Create two accounts, one of which is missing a required field
    Account[] accts = new List<Account>{
    new Account(Name='Account1'),
    new Account()};
    Database.SaveResult[] srList = Database.insert(accts, false);

    // Iterate through each returned result
    for (Database.SaveResult sr : srList) {
    if (!sr.isSuccess()) {
    // Operation failed, so get all errors
    for(Database.Error err : sr.getErrors()) {
    System.debug('The following error has occurred.');
    System.debug(err.getStatusCode() + ': ' + err.getMessage());
    System.debug('Fields that affected this error: ' + err.getFields());
    }
    }
    }

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos