Error Notification while working with a scheduled job
Steps:
1. Create a custom object to store error information whenever the job throws an error and set up a flow on this object to send email alerts if there is an error.
Suppose: 'ErrorLog__c' is the custom object and 'Trace__c' is a custom field
2. In the apex class that you schedule, create a new 'Error Log' object in catch statements to track specific error information when the job fails.
try{
//Some DML statement
} catch (Exception e) {
ErrorLog__c log = new ErrorLog__c();
log.trace__c = 'Type: ' + e.getTypeName() + '\n' + 'Cause: ' + e.getCause() + '\n' + 'Message: '
+ e.getMessage() + '\n' + 'Line #: ' + e.getLineNumber() + '\n' + e.getStackTraceString() + '\n'
+ 'Some Custom Variable Information From Class: ' + myClassVariable;
insert log;
}
//Some DML statement
} catch (Exception e) {
ErrorLog__c log = new ErrorLog__c();
log.trace__c = 'Type: ' + e.getTypeName() + '\n' + 'Cause: ' + e.getCause() + '\n' + 'Message: '
+ e.getMessage() + '\n' + 'Line #: ' + e.getLineNumber() + '\n' + e.getStackTraceString() + '\n'
+ 'Some Custom Variable Information From Class: ' + myClassVariable;
insert log;
}
3. Set up a flow on the 'Error Log' object to send email whenever a new error log is created.
Note: Monitor this object and periodically delete old error logs
Reference: https://help.salesforce.com/s/articleView?id=000387981&type=1
Comments
Post a Comment