I have a managed package that users will install to their org. I want to somehow run a task daily that will build an aggregate a list of Events created that day, format it into an email body, and send it to a list of emails (to be pulled in from an external REST API resource).

What is the best way to do this? The code I want to run daily will be an apex class that should run a simple query with a range constraint on the Event.CreatedDate field. I can send an email in apex code with the Messaging.sendEmail() class.

I'm not clear on how to run this code daily, and how to add this "scheduler" to the managed package. It looks like the Scheduler interface is to be scheduled from apex code, so would I schedule the first task during a post install script, and continuously schedule the next task for 24 hours later? What if the chain gets broken?

What is the best way to run this apex class every day at a set time?

2 Answers 2


We use Schedulables for exactly this sort of thing and we do kick them off in the post install script. A good idea is to maintain detail as to the last successful date range for selecting data so if the process fails one day the email for the following day will include all the data previously not sent. This info can be stored in protected custom settings instance(s).

Also, you could consider having other schedulable(s) that re-schedule your planned schedulables if they get unscheduled (we have two of these, so rescheduling is checked for and applied at least every 30 minutes - we have these "watchdog" tasks run hourly, one at 00 and the other at 30 minutes past the hour).

  • so basically in post install, run a scheduler that will reschedule itself every day? how reliable is this? because obviously if the task gets cancelled somehow there's no way to start it back up again.
    – rosghub
    Sep 2, 2020 at 22:02
  • No, the cron schedule definition allows repetition without the need to re-schedule explicitly. The re-scheduling is just there for if someone tries to mess with your scheduled jobs. With the watchdog jobs going too they have to stop these as well to stop the re-scheduling...
    – Phil W
    Sep 2, 2020 at 22:06
  • I see. So the post install schedules the cron job; so if I wanted further control over this like starting and stopping and changing the time, would the best solution be to create a visual force page?
    – rosghub
    Sep 2, 2020 at 22:09
  • To provide control, don't use visualforce but instead write an LWC component and provide a Lightning Page that includes the component then add this into your lightning app. Visualforce is old technology that was replaced by Lightning (aura) rather a while back and this is now being replaced by LWC. LWC is a much nicer technology.
    – Phil W
    Sep 3, 2020 at 6:16

As you wrote you need to write scheduler class that will run the logic.

How to schedule it? It can be done with post install or manually from the developer console or from the UI (See here: https://help.salesforce.com/articleView?id=code_schedule_batch_apex.htm&type=5). This can be done by you or the subscribe admin, you can provide him the manual steps.

Alternatively you can provide simple page with button/settings that allow to schedule it. For example allow to choose the time/interval to run it.

Indeed it might stopped due to several reasons, therefore you should be prepared with the manual steps to schedule it again.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .