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

  • 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);