0

I was searching for a possibility to send a yearly email reminder when the 1st. February in that year comes. The only criteria to fire the email reminder is that so long it is the 1st. February in the year, the reminder has to be fired.

I tried to let a workflow rule or process to make this using a Date formula field, but this didn't work.

I was then thinking of implementing a batch apex job and schedule it to run every 1st February of every year. But this batch job should only send an email, nothing else. When I see batch job codes, they all include a start and an execute method.

The point is, I just want to send an email. The batch code doesn't need to be executed for specific or all accounts. It is about sending a notification like "Please don't forget to configure this on the 1st February", nothing else.

I have written the following code:

global class Mail_reminder implements Database.Batchable<SObject>, 
Database.AllowsCallouts{

global final Integer lim = (Integer) [Select Batch_Limit__c From 
CS_ER__c Limit 1].Batch_Limit__c;
global String query;
Date TODAYDATE = Date.today();

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

    if (query == '' || query == null) {
        query = 'select Id, Name, Current_Year_Date__c from Account LIMIT 
        1';                 
    }
    return Database.getQueryLocator(query);
}


 global void execute(Database.BatchableContext BC, list<Account> accounts){
    List<Account> listToUpdate = new List<Account>(); 
    for( Account a: (List<Account> ) accounts) 
    {   
            a.Current_Year_Date__c = TODAYDATE;     

        listToUpdate.add(a); 
    } 

    update(listToUpdate);  


}


global void finish(Database.BatchableContext BC){
    Boolean condition = [Select Id From Account LIMIT 1].isEmpty();

    if(!condition ) {
        Mail_reminder batch1 = new Mail_reminder();
        Database.executebatch(batch1,lim);
        sendEmail();
    }
}

private void sendEmail(){

    Messaging.reserveSingleEmailCapacity(2);


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


    String[] toAddresses = new String[] {[Select EmailAddress__c From 
    CS_ER__c Limit 1].EmailAddress__c};

    mail.setReplyTo('myemailaddress@mydomain.com');

    // Specify the name used as the display name.
    mail.setSenderDisplayName('This is test');

    // Specify the subject line for your email address.
    mail.setSubject('This is a test');

    // Set to True if you want to BCC yourself on the email.
    mail.setBccSender(false);

    mail.setUseSignature(false);

    // Specify the text content of the email.
    //mail.setPlainTextBody('This is a test');

    mail.setHtmlBody('Some text');

    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
   }
 }

where CS_ER__c is a custom setting. Batch_Limit__c is a field in this custom setting with value 5000. EMailAddress__c is a field in the custom setting with my email address.

When I run this job, it runs many times endless, so it sends emails to me many times. I then had to delete the job because it was fulling my mailbox.

As I am not intending to update any Account record and only send an email but the batch job needs a start and an execute DML operation, I thought I could handle this just querying accounts with LIMIT 1. But why is this job running endless or sending me emails many times?.

If I adjust the Batch_Limit_c with value 1 instead of 5000, the job doesn't send the email.

How can I adjust this?. I would appreciate your tips.

3

Use a Scheduable interface, not a Batchable interface:

public class SendReminderEmail implements Schedulable {
  public void execute(ScheduableContext context) {
    sendEmail();
  }
  void sendEmail() {
    // ...
  }
}

Then, you schedule it using execute anonymous:

System.schedule('Yearly Email','0 0 0 1 2 ?', new SendReminderEmail());

This will run every year at midnight, February 2nd.

1
  • Thank you very much sfdcfox for your answer, I thought there was no other possibility.
    – user10267
    Feb 4 '18 at 18:29

Your Answer

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