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
Force2b_MikeForce2b_Mike 

Scheduling automated eMails

I have a client that wants to send automated emails (that have to be done through Apex) based on specific dates. For example:

 

  • Send an eMail 2 days before the 'Close Date'
  • Send an eMail once per week between the Start Date and Close Date

 

Unfortunately I can't use standard time-dependent workflows because the eMails they want to send cannot be sent through the normal email process (apex needed to build the email).

 

My original thought was to use a Time-Dependent Workflow rule to initiate the eMail, however that has two problems:

 

  1. Can't send an Outbound Message to an Apex class
  2. Can't find a way to repeat a message every week between two dates

 

I thought about using a Trigger to schedule an Apex job, but you cannot schedule more than 10 classes at the same time, which I think means I could not have more then 10 records scheduled for an eMail (there could potentially be hundreds). Or, does this really mean that a single class can be scheduled multiple times, but no more than 10 different classes can be scheduled?

 

Anyway, not sure the best way to make this happen without writing a great deal of custom code. I really wanted to give the client as much control  as possible using Workflow Rules, but that option is looking doubtful. 

 

I'm curious how others would handle this need.

 

Thanks,

 

Mike

Anand@SAASAnand@SAAS

Specific to your use case, you can schedule two batch jobs:

1. daily batch that looks at any records where CloseDate > TODAY()

2. Weekly batch that loogs at any records where CLOSEDATE > TODAY()

 

As regards to your question, you can schedule a class multiple times but you can only have a total of 10 scheduled jobs at a given point in time.

 

Force2b_MikeForce2b_Mike

Thanks for the suggestions. I was hoping to avoid writing code that polls records for emails to send, but it is an excellent option.

 

Mike

Pal2011Pal2011

Hi,

 

By scheduling batch apex how can I send more than 10 Emails, as only 10 sendEmail method invocations are allowed. I need to send email notification to all account objects whose rebate is due on particular day of month. In my case I need to create different email with different subject. I figured out, I cannot call more than 10 sendEmail methods per transaction.

 

What should I do in this case?

Force2b_MikeForce2b_Mike

A single call to sendEMail() can pass in a list of Messaging.SingleEmailMessage - list<Messaging.SingleEmailMessage> msgs = new list<Messaging.SingleEmailMessage>. Just add all your messages into a list collection and do one mass send at each batch iteration of collectively in the finish if you use a persistent list; or break it up into batches of 100 if that works. Keep in mind that you're still limited by your organization's daily single email limit.

 

Best Regards,

 

Mike

Pal2011Pal2011

Hi Mike,

 

Thanks for the reply.

 

I'm still not sure how can I send more than 100 single emails per day as SingleEmailMessage is limited to 100. I'm using Batch Apex to send emails.

 

Can you please help me, how can I create batch so I can send more than 100 email messages from my batch apex.

 

Thanks,