Monday, November 14, 2016

Capture errors after using Database DML methods in Salesforce.

List<Contact> lstContact = new List<Contact>();
Contact con = new Contact (lastName = 'Sharma', SQL_Server_Id__c='3',firstName='Abhinav');
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());
    }
}

No comments:

Post a Comment