Skip to content

Instantly share code, notes, and snippets.

@arun12209
Last active October 12, 2022 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arun12209/5c3cbc502c5890767ab67a253afa14fc to your computer and use it in GitHub Desktop.
Save arun12209/5c3cbc502c5890767ab67a253afa14fc to your computer and use it in GitHub Desktop.
Utility class : Create Error method is here to create the error log object record
public with sharing class Util {
/**
* @author SFDC Lessons
* @description Method to create the error log record
* @param List<Database.SaveResult> saveResults
* @param List<SObject> objectList
* @param String topic
* @return void
*/
public static void createError(List<Database.SaveResult> saveResults, List<SObject> objectList, String topic){
try{
//error object list
List<Error_Log__c> errorList = new List<Error_Log__c>();
for( integer i=0; i < saveResults.size(); i++ ){
if (saveResults.get(i).isSuccess()) {
//add the code here if you want to do something in success case
}//error case
else if(! saveResults.get(i).isSuccess()){
// Operation failed, so get all errors
Database.Error dbError = saveResults.get(i).getErrors().get(0);
system.debug('Failed record id: '+ (String) objectList[i].get('Id'));
String recordId, recordName = '';
if(objectList.size()>0){
recordId = (String) objectList[i].get('Id');
recordName = (String) objectList[i].get('Name');
}
Error_Log__c err = new Error_Log__c();
err.Error_Message__c = dbError.getMessage();
err.Stack_Trace__c = String.valueOf(dbError.getStatusCode());
err.Record_ID__c = recordId;
err.Record_Name__c = recordName;
err.Topic__c = topic;
//add error in the list
errorList.add(err);
}
}
if (errorList.size()>0) {
insert errorList;
}
}catch(Exception e){
System.debug('Exception has occured! ' + e.getMessage());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment