Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
Add Comment
Please, Sign In to add comment