function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rocio VivancoRocio Vivanco 

How to write a batch job to only send an email reminder?

Hi all,

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: 
 
...
global database.querylocator start(Database.BatchableContext BC)
{
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, Sobject[] scope)
...

and the sendEmail is then used in a Finish part of the code.
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.

Would be enough if I write this code?:
 
private void sendEmail(){

        Messaging.reserveSingleEmailCapacity(2);
        

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        // Strings to hold the email addresses to which you are sending the email.
        String[] toAddresses = new String[] {[Select EmailAddress__c From CS_EmailAddresses__c Limit 1].EmailAddress__c};
          
        // Assign the addresses for the To and CC lists to the mail object.
        mail.setToAddresses(toAddresses);
        
        // Specify the address used when the recipients reply to the email. 
        mail.setReplyTo('mycompany@mydomain.com');
        
        // Specify the name used as the display name.
        mail.setSenderDisplayName('Salesforce Email Reminder');
        
        // Specify the subject line for your email address.
        mail.setSubject('Yearly Email Reminder');
        
        // Set to True if you want to BCC yourself on the email.
        mail.setBccSender(false);
        
        // Optionally append the salesforce.com email signature to the email.
        // The email address of the user executing the Apex Code will be used.
        mail.setUseSignature(false);
        
        // Specify the text content of the email.
        //mail.setPlainTextBody('Please configure this');
        
        mail.setHtmlBody('Your Invitee batch job has been processed.<br/><br/>Best Regards);
        
        // Send the email you have created.
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}

 
Best Answer chosen by Rocio Vivanco
Raj VakatiRaj Vakati
You can do it with  Apex Scheduler. Create an apex class as shown below. Then Schedule it by using 
The System.Schedule method to run every year.
 
global class sendEmail implements Schedulable {
   global void execute(SchedulableContext SC) {
sendEmail();
   }

   
  global void sendEmail(){

        Messaging.reserveSingleEmailCapacity(2);
        

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        // Strings to hold the email addresses to which you are sending the email.
        String[] toAddresses = new String[] {[Select EmailAddress__c From CS_EmailAddresses__c Limit 1].EmailAddress__c};
          
        // Assign the addresses for the To and CC lists to the mail object.
        mail.setToAddresses(toAddresses);
        
        // Specify the address used when the recipients reply to the email. 
        mail.setReplyTo('mycompany@mydomain.com');
        
        // Specify the name used as the display name.
        mail.setSenderDisplayName('Salesforce Email Reminder');
        
        // Specify the subject line for your email address.
        mail.setSubject('Yearly Email Reminder');
        
        // Set to True if you want to BCC yourself on the email.
        mail.setBccSender(false);
        
        // Optionally append the salesforce.com email signature to the email.
        // The email address of the user executing the Apex Code will be used.
        mail.setUseSignature(false);
        
        // Specify the text content of the email.
        //mail.setPlainTextBody('Please configure this');
        
        mail.setHtmlBody('Your Invitee batch job has been processed.<br/><br/>Best Regards);
        
        // Send the email you have created.
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
   }


From Developer console run this code. Make sure change the cron expression.
sendEmail sendEmailJob = new sendEmail();
        String sch = '-- CRON EXPRESSION';
        system.schedule('sendEmailJob', sch, sendEmailJob);

 


 

All Answers

Raj VakatiRaj Vakati
You can do it with  Apex Scheduler. Create an apex class as shown below. Then Schedule it by using 
The System.Schedule method to run every year.
 
global class sendEmail implements Schedulable {
   global void execute(SchedulableContext SC) {
sendEmail();
   }

   
  global void sendEmail(){

        Messaging.reserveSingleEmailCapacity(2);
        

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        // Strings to hold the email addresses to which you are sending the email.
        String[] toAddresses = new String[] {[Select EmailAddress__c From CS_EmailAddresses__c Limit 1].EmailAddress__c};
          
        // Assign the addresses for the To and CC lists to the mail object.
        mail.setToAddresses(toAddresses);
        
        // Specify the address used when the recipients reply to the email. 
        mail.setReplyTo('mycompany@mydomain.com');
        
        // Specify the name used as the display name.
        mail.setSenderDisplayName('Salesforce Email Reminder');
        
        // Specify the subject line for your email address.
        mail.setSubject('Yearly Email Reminder');
        
        // Set to True if you want to BCC yourself on the email.
        mail.setBccSender(false);
        
        // Optionally append the salesforce.com email signature to the email.
        // The email address of the user executing the Apex Code will be used.
        mail.setUseSignature(false);
        
        // Specify the text content of the email.
        //mail.setPlainTextBody('Please configure this');
        
        mail.setHtmlBody('Your Invitee batch job has been processed.<br/><br/>Best Regards);
        
        // Send the email you have created.
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    }
}
   }


From Developer console run this code. Make sure change the cron expression.
sendEmail sendEmailJob = new sendEmail();
        String sch = '-- CRON EXPRESSION';
        system.schedule('sendEmailJob', sch, sendEmailJob);

 


 
This was selected as the best answer
Rocio VivancoRocio Vivanco
Thanks Rav for your answer. It is not working. The error I get is:

Scheduler: failed to execute scheduled job: jobId: 7070Y00001wqU9g, class: common.apex.async.AsyncApexJobObject, reason: The daily limit for the org would be exceeded by this request

it doesn't send the email either.
Rocio VivancoRocio Vivanco
As not many answer threads in the Developers forum I opened a thread here:

https://salesforce.stackexchange.com/questions/206900/is-there-a-way-to-skip-the-start-and-execute-methods-in-an-apex-job

I still can not handle this. I would appreciate any help.
Rocio VivancoRocio Vivanco
Hi Rav, thank you very much for your help :). Yesterday the job didn't do anything because I ran out of the daily limit an org has for scheduling jobs.
I tried it today and it sends me the email once (just as desired). My only worry is that when monitoring the job, this is still with status Queued. Should that be important?:

User-added image
Rocio VivancoRocio Vivanco
Ok, I found this very helpful:

https://help.salesforce.com/articleView?id=000186598&type=1