• I have rejected Workflow Rules, since email alerts can't be sent at the exact same time every week.
  • I have rejected schedulable Reports/Dashboards since emails can't be sent to email fields of records and the email content can't be customized.

What are the Apex topics I should look into, to send weekly emails (every Monday) to email-fields of records of a custom object? Can you provide me with code snippets?


The key ingredients for this are going to be the following, mostly from the Apex Developers Guide. You should have some familarity with Object Orientated programming, writing unit tests (ala JUnit/NUnit) and using the development tools. Code snippets can be found in the linked documentation.

  • Apex Scheduler. You need to create an Apex class (e.g. WeeklyEmailScheduler.cls) that implements the Schedulable interface. Once you do this, the class will appear to you under the Setup > Develop > Classes > Schedule Apex button. And you can then define the schedule for it. Keep in mind the following from the docs however 'Salesforce only adds the process to the queue at the scheduled time. Actual execution may be delayed based on service availability.'
  • Batch Apex. Depending on the amount of records your processing. In the execute method of the above class you need to run a Batch Apex job (via Database.excecuteBatch) to process them. This is typically (and recommended in the docs) to be implemented in another Apex class (e.g. WeeklyEmailBatch.cls) implementing the Database.Batchable interface. Note also that there is some potential lag due to service availability in using Batch Apex jobs.
  • Email Classes. Inside the execute method of your Batch Apex class you will receive your records for processing. I recommend using the Messaging.SingleEmailMessage to send your messages, it has the most flexibility for custom objects and can infact be sent in bulk, since the Messaging.sendEmail takes a list of these. Also note that there are limits on the amount of emails you can send in 24hr period.
  • Apex Tests. In order to deploy your code to a Production environment you will need to create another Apex class (e.g. WeeklyEmailTests.cls) (or reuse one above) and add test methods. Here you can either develop upfront (test driven development) or write retrospectively (maybe better if your still learning) more Apex code that executes the above classes and uses System.assert methods to confirm the code is working as expected.
    • In your case its hard to assert the actual emails so you may want to make provision in your code to store the SingleEmailMessage object before its sent to the Messaging.sendEmail function in some place the tests can find it (a static variable or returned from one of your methods).
  • Tools and Process. You write the above code in one of many development tools, Force.com IDE (Eclipse Pluging), your own editing and Ant or the web based Developer Console available once logged into Salesforce. Once your done developing the code you will likely need to Package it or push it into your Product org via Ant/Eclipse/Change Sets. This last bit can be the most challanging if your new to the platform, but the above should get you going or at least give you an idea of the cost/complexity of this option.

Hope this helps!



One topic that is necessary to understand the scheduler interface

The other will be to undesrtand outbound email topic to send mails


These two can be combined for your requirement but before writing any code ordesigning its really necessary to look for data volumes and the governor limits of all these features .

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.