+ Start a Discussion
Vishvesh BhandariVishvesh Bhandari 

Capturing errors without try catch.

Hi,

Could anyone help me on how to capture errors after using Database DML methods in Salesforce instead of using try/catch methods?

Thanks
Vishvesh.
Best Answer chosen by Vishvesh Bhandari
Shubham NandwanaShubham Nandwana
Hi Vishvesh,
You can use getError() to see what errors have occurred.
getErrors()-: If an error occurred, returns an array of one or more database error objects providing the error code and description. If no error occurred, returns an empty set.
 
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());
    }
}
Refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm

Mark it as solved if it helps.

Shubham Nandwana
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
 

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

You can use database methods instead.

Refer below link.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_database.htm
 
Best Regards,
Sandhya
Shubham NandwanaShubham Nandwana
Hi Vishvesh,
You can use getError() to see what errors have occurred.
getErrors()-: If an error occurred, returns an array of one or more database error objects providing the error code and description. If no error occurred, returns an empty set.
 
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());
    }
}
Refer to https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_saveresult.htm

Mark it as solved if it helps.

Shubham Nandwana
AppPerfect Corp.
salesforce@appperfect.com
408-252-4100
http://www.appperfect.com/services/salesforce/
Salesforce Development & Operations Experts
 
This was selected as the best answer