function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
could & rockcould & rock 

Best practice for notifying exception in future method or batch job

Hi,

 

If the exception happens in UI controller or trigger, the exception can be caught, and then be presented to UI through VF apex:message. I wonder what's Best practice for notifying exception in future method or batch job where there's no UI interface directly to user. I can only think of 2 options, 1) catch the exeption, and put either the raw error message or user-friendly error message into debug log, 2) catch the exception, and throw an new exception with a user-friendly error message; and this exception will be logged into apex job execution result by SFDC.

 

Any other thoughts?

Best Answer chosen by Admin (Salesforce Developers) 
crop1645crop1645

Option 1

  • For batch jobs, I use the finish method to send an email with any caught exceptions to the sysad
  • Same thing for future methods

Option 2 - same as option 1 plus

  • Create a custom object called 'Diag_log__c' or something like this
  • In your caught exception, do any rollbacks and then write a record out to the custom object with as much contextual information your batch or future method has available to it to make diagnosis easier