Guest User

Untitled

a guest
Nov 19th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. global class SendNotificationBatch implements Database.Batchable<sObject>, Schedulable, Database.Stateful {
  2.  
  3. //Variable Section
  4. global FINAL String strQuery;
  5. global List<String> errorMessages = new List<String>();
  6.  
  7. global SendNotificationBatch() {
  8. this.strQuery = getBatchQuery();
  9. }
  10.  
  11. //Returns the Query String to Batch constructor to fetch right records.
  12. private String getBatchQuery() {
  13. String strQuery = 'SELECT Id, CloseDate, Owner.Email FROM Opportunity WHERE CloseDate < TODAY';
  14. return strQuery;
  15. }
  16.  
  17. //Batch Start method
  18. global Database.QueryLocator start(Database.BatchableContext BC) {
  19. return Database.getQueryLocator(strQuery);
  20. }
  21.  
  22. //Batch Execute method calls findCostForWoD method
  23. global void execute(Database.BatchableContext BC, List<sObject> scopeList) {
  24. System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size());
  25.  
  26. List<Opportunity> oppList = (List<Opportunity>) scopeList;
  27. if(!oppList.isEmpty()) {
  28. List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
  29. for (Opportunity prod : oppList)
  30. {
  31.  
  32. Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
  33. String[] toAddresses = new String[] {prod.Owner.Email};
  34. Message.setToAddresses(toAddresses);
  35. Message.SaveAsActivity = false;
  36. mailList.add(Message);
  37.  
  38. }
  39. if(!mailList.isEmpty()) {
  40. try{
  41. Messaging.sendEmail(mailList);
  42. }
  43. catch (Exception ex) {
  44. errorMessages.add('Unable to send email to Tech: '+ ex.getStackTraceString());
  45. }
  46. }
  47. }
  48. }
  49.  
  50. //Batch Finish method for after execution of batch work
  51. global void finish(Database.BatchableContext BC) {
  52. AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()];
  53.  
  54. // Send an email to the Apex job's submitter notifying of job completion.
  55. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
  56. String[] toAddresses = new String[] {aaj.CreatedBy.Email};
  57. mail.setToAddresses(toAddresses);
  58. mail.setSubject('JOB Salesforce Send Notification Batch: ' + aaj.Status);
  59. String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.n';
  60. bodyText += 'Number of Error Messages ' + errorMessages.size() + 'n';
  61. bodyText += 'Error Message' + String.join(errorMessages, 'n');
  62. mail.setPlainTextBody(bodyText);
  63. Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
  64. }
  65.  
  66. //Method which schedules the ProductDownloadBatch
  67. global void execute(SchedulableContext sc) {
  68. SendNotificationBatch snInstance = new SendNotificationBatch();
  69. ID batchprocessid = Database.executeBatch(snInstance);
  70. }
Add Comment
Please, Sign In to add comment