0

I have a batch class where the requirement is to get the actual count of success and failure records then send an email to different email ids one for failure and the other for success with the record counts. I have written the code and sharing here the snippet, am I doing this right?

  • Seems correct! Do you having any issue with that. However you are sending detail of apex job not the record details. – Ashwani Aug 20 '15 at 14:22
  • (1) The batch class needs to implement Database.stateful (2) Your finish() method could be coded more cleanly through use of ternary operators so you don't have to duplicate all the text. Emails should go out with success AND failure counts since you are allowing for partial successes; (3) global access modifier is not required, you can use public – cropredy Aug 20 '15 at 16:46
  • @crop1645 I included the If statement for sending separate mails one with with success count and the other with failed count. A new requirement has come where I have to include the details of the failed record with failed count in the email to the failure email id. – user13277 Aug 20 '15 at 17:49
2

Some remarks:

  1. The batch class needs to implement Database.stateful so you can accumulate success and fail counts
  2. Emails should go out with success AND failure counts since you are allowing for partial successes
  3. Global access modifier is not required, you can use public
  4. Your counting of errors doesn't look right - as you are allowing for partial successes (allOrNothing is false), then the catch block will never be invoked when a row in scope fails.

With respect to #4, you'll need to do something like this in execute(...):

public void execute(Database.BatchableContext BC, List<Case> scope) {
 try{
    for(Case caseRecord:scope){
       .. your if logic here ..
           listCaseUpdate.add(caseRecord);
    }
     //Update Case List     
    Database.SaveResult[] sveResult = Database.update(listCaseUpdate, false);
    ... your logger ...
    for(Database.SaveResult sr:sveResult)
       if(sr.isSuccess()) successCount++;
       else failurecount ++;
}
catch (Exception e) { 
     ...your logger ..
     failureCount = failureCount + scope.size(); // entire scope failed
}
}  
| improve this answer | |
  • Got ur point but the requirement is to send email to 2 different email id's one only for success and the other for failure. So what I'm doing is in the finish method i've put an if else for getting the job according to its status and then sending email to respective email id's. – user13277 Aug 21 '15 at 6:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy