Skip to main content
Hello All,

I have a requirement where i need to send an email notification to the contract owner 30 days before the contract end date , when the contract role is 'SA/EB?TA' etc.Any suggestion  with an example very much appreciated.
13 answers
  1. Apr 6, 2016, 4:22 PM
    Hi

    Please find the below code:

    Here I considered:

    (1) Included Contract's Email.

    (2) Added the code in finish method.

    (3) Implement with Schedulable so that you can write the schedulable code here it self.

     

    global class NotificationEmailtoAccountExecutive implements Database.Batchable < sObject >, Schedulable, Database.Stateful {

    global List<String> errorMessages = new List<String>();

    global Database.QueryLocator start(Database.BatchableContext bc) {

    /* Date d = Date.today();*/

    Date ed = Date.today().addDays(110);

    System.debug(Date.today().addDays(110));

    set<Id> setContractIds = new set<Id>();

    for(Contract_role__c objContract: [SELECT Contract__c FROM Contract_role__c WHERE Role__c = 'Subscription Administrator' AND Contract__r.EndDate =: ed]) {

    setContractIds.add(objContract.Contract__c);

    }

    return Database.getQueryLocator('Select id, Contract_Name__c , EndDate ,Contact_Email__c, Contract_End_Date_2__c, Owner.Email FROM Contract WHERE Id IN: setContractIds');

    }

    global void execute(Database.BatchableContext bc, List < Contract > recs) {

    List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > ();

    for (Contract c: recs) {

    if (c.Contact_Email__c != null) {

    List < String > toAddresses = new List < String > ();

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    toAddresses.add(c.Contact_Email__c);

    toAddresses.add(c.Owner.Email);

    mail.setToAddresses(toAddresses);

    mail.setSubject('Notification Before 110 Days of Contract End Date');

    String messageBody = '<html><body>Hi ' + c.Contract_Name__c + ',<br>Your Contract Expires within 110 Days . <br>Kindly take approriate action.<br><br><b>Regards,</b><br>EDB</body></html>';

    mail.setHtmlBody(messageBody);

    mailList.add(mail);

    }

    }

    Messaging.sendEmail(mailList);

    }

    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 NotificationEmailtoAccountExecutive Finished: ' + 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 });

    }

    global void execute(SchedulableContext SC) {

    NotificationEmailtoAccountExecutive batchable = new NotificationEmailtoAccountExecutive();

    database.executebatch(batchable);

    }

    }

    Please do let me know if it helps you.

    Regards,

    Mahesh
0/9000