Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

What would be a better way of error handling in batch apex other than using a n object to insert errors. Can we send error messages to the user in an email from finish method.

Currently, I'm doing something like this.

try{
            Counter += ListtoInsert.size();
            database.insert(ListtoInsert);
        }Catch(exception e){
            system.debug('Exception Caught:'+e.getmessage());
        }

But I'm just debugging them. How can i send error messages to the user.

Or is there any better approach in addressing the issue.

share|improve this question
2  
You can send the errors in a email from the finish method. Just create a variable to gather them from your batch execute method, then format your mail and send it out in the finish method. – Jesse Milburn Mar 10 at 20:55
    
@JesseMilburn You probably should have added that as an answer-- that's basically what I said. – sfdcfox Mar 10 at 21:03
up vote 6 down vote accepted

Yes, you can certainly send an email to the user; in fact, this is my preferred method to avoid spamming users. You can do this by way of the Database.Stateful.

public class MyBatchable implements Database.Batchable<SObject>, Database.Stateful {
    Exception[] errors = new Exception[0];
    public Iterable<SObject> start(...) {
        ...
    }
    public void execute(...) {
        try {
            ...
        } catch(Exception e) {
            errors.add(e);
        }
    }
    public void finish(...) {
        if(!errors.isEmpty()) {
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSubject('Errors occurred during batch process.');
            mail.setTargetObjectId(UserInfo.getUserId());
            mail.setSaveAsActivity(false);
            mail.setPlainTextBody(buildBodyFor(errors));
            Messaging.sendEmail(new Messaging.Email[] { mail });
        }
    }
}

You might also include a way to send emails if too many errors have stacked up, or limit yourself to some number of exceptions (say, the first 50 or 100). Keep in mind that you do have a limited amount of memory available, so if you're concerned about having a large number of errors, consider logging them to the database and then querying them back in your finish method.

share|improve this answer
    
I've found I need to add a try/catch around the Messaging.sendEmail in this scenario as often with packages and sandboxes etc... the user running the batch can't send an outbound email. Just something to watch out for is the batch might otherwise work and then fall over in the finish method. – Daniel Ballinger Mar 10 at 23:15

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.