• Elwic87
  • NEWBIE
  • 10 Points
  • Member since 2021

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 5
    Replies
I'm using the following code:


global class SendNotificationBatch implements Database.Batchable<sObject>, Schedulable, Database.Stateful { //Variable Section global FINAL String strQuery; global List<String> errorMessages = new List<String>(); global SendNotificationBatch() { this.strQuery = getBatchQuery(); } //Returns the Query String to Batch constructor to fetch right records. private String getBatchQuery() { String strQuery = 'SELECT Id, CloseDate, Owner.Email FROM Opportunity WHERE CloseDate < TODAY'; return strQuery; } //Batch Start method global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(strQuery); } //Batch Execute method calls findCostForWoD method global void execute(Database.BatchableContext BC, List<sObject> scopeList) { System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size()); List<Opportunity> oppList = (List<Opportunity>) scopeList; if(!oppList.isEmpty()) { List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>(); for (Opportunity prod : oppList) { Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {prod.Owner.Email}; Message.setToAddresses(toAddresses); Message.SaveAsActivity = false; mailList.add(Message); } if(!mailList.isEmpty()) { try{ Messaging.sendEmail(mailList); } catch (Exception ex) { errorMessages.add('Unable to send email to Tech: '+ ex.getStackTraceString()); } } } } //Batch Finish method for after execution of batch work global void finish(Database.BatchableContext BC) { AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {aaj.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('JOB Salesforce Send Notification Batch: ' + aaj.Status); String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n'; bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n'; bodyText += 'Error Message' + String.join(errorMessages, '\n'); mail.setPlainTextBody(bodyText); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } //Method which schedules the ProductDownloadBatch global void execute(SchedulableContext sc) { SendNotificationBatch snInstance = new SendNotificationBatch(); ID batchprocessid = Database.executeBatch(snInstance); } }

but I get this error when executing:

Error MessageUnable to send email to Tech: Class.SendNotificationBatch.execute: line 41, column 1

I'm assuming it has to do with opportunity products, when I just want to look at the CloseDate field on the Opportunity object. Any ideas on what to change?  Thanks!
I'm using the following code:


global class SendNotificationBatch implements Database.Batchable<sObject>, Schedulable, Database.Stateful { //Variable Section global FINAL String strQuery; global List<String> errorMessages = new List<String>(); global SendNotificationBatch() { this.strQuery = getBatchQuery(); } //Returns the Query String to Batch constructor to fetch right records. private String getBatchQuery() { String strQuery = 'SELECT Id, CloseDate, Owner.Email FROM Opportunity WHERE CloseDate < TODAY'; return strQuery; } //Batch Start method global Database.QueryLocator start(Database.BatchableContext BC) { return Database.getQueryLocator(strQuery); } //Batch Execute method calls findCostForWoD method global void execute(Database.BatchableContext BC, List<sObject> scopeList) { System.debug(LoggingLevel.INFO, '== scopeList size ==' + scopeList.size()); List<Opportunity> oppList = (List<Opportunity>) scopeList; if(!oppList.isEmpty()) { List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>(); for (Opportunity prod : oppList) { Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {prod.Owner.Email}; Message.setToAddresses(toAddresses); Message.SaveAsActivity = false; mailList.add(Message); } if(!mailList.isEmpty()) { try{ Messaging.sendEmail(mailList); } catch (Exception ex) { errorMessages.add('Unable to send email to Tech: '+ ex.getStackTraceString()); } } } } //Batch Finish method for after execution of batch work global void finish(Database.BatchableContext BC) { AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {aaj.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('JOB Salesforce Send Notification Batch: ' + aaj.Status); String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n'; bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n'; bodyText += 'Error Message' + String.join(errorMessages, '\n'); mail.setPlainTextBody(bodyText); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } //Method which schedules the ProductDownloadBatch global void execute(SchedulableContext sc) { SendNotificationBatch snInstance = new SendNotificationBatch(); ID batchprocessid = Database.executeBatch(snInstance); } }

but I get this error when executing:

Error MessageUnable to send email to Tech: Class.SendNotificationBatch.execute: line 41, column 1

I'm assuming it has to do with opportunity products, when I just want to look at the CloseDate field on the Opportunity object. Any ideas on what to change?  Thanks!