Question

I see scheduler like feature in salesforce but it is somewhat tied with existing features that salesforce provide and no sample source code is provided as far as my research goes.

What I want to do is to create my own scheduler that sends simple email based on date.

Goal:

Custom Object Player has fields

startDate : date like '2010-11-01'
email : text field like foo@bar.com
name : player's name like John.

If today's date is one day before the startDate, I want to send email to the Player. For instance, Player's name is John and if today is 2010-12-10 and one Player's startDate is set to 2010-12-11, email saying "hello John" is sent.

Unfortunately I cannot find good example or tutorial online or salesforce doc how to do this using Apex. Could anyone point out where to get started?

UPDATE

I want to extend the answer from eyescream.

After setting scheduler, you can set what follow up action to take like sending email using template or set custom object fields to some values.

Below I found useful for people using email template in Visualforce format.

I have custom object 'alertTester' which has reference to other object 'custom' and even this object 'custom' has reference to another object 'custom1GrandChild' and all the relationship (up to 3 or 5 layers I think) can be accessed like below.

I've tested below and works fine. Now I'm receiving email with my condition set :)

<messaging:emailTemplate subject="Hello" recipientType="User" relatedToType="alertTester__c" >
<messaging:plainTextEmailBody >

{!relatedTo.name}
{!relatedTo.custom__r.name}
{!relatedTo.custom__r.custom1GrandChild__r.name}


</messaging:plainTextEmailBody>
</messaging:emailTemplate>
Was it helpful?

Solution

Check out solutions that don't involve code before you'll dive deeply to Apex...

Email Alert + Workflow Rule should provide you with all functionality you need in this scenario and involve just clicking without any code.

OTHER TIPS

I'm answering to my own question again..

Below link, search for schedule

http://www.salesforce.com/us/developer/docs/apexcode/index.htm

Looks like Apex has Schedulable interface that I can implements and set up cron task.

Below is sample code provided in the doc:

global class TestScheduledApexFromTestMethod implements Schedulable {

// This test runs a scheduled job at midnight Sept. 3rd. 2022  


   public static String CRON_EXP = '0 0 0 3 9 ? 2022';

   global void execute(SchedulableContext ctx) {
      CronTrigger ct = [SELECT id, CronExpression, TimesTriggered, NextFireTime
                FROM CronTrigger WHERE id = :ctx.getTriggerId()];

      System.assertEquals(CRON_EXP, ct.CronExpression);
      System.assertEquals(0, ct.TimesTriggered);
      System.assertEquals('2022-09-03 00:00:00', String.valueOf(ct.NextFireTime));

      Account a = [SELECT id, name FROM Account WHERE name = 
                  'testScheduledApexFromTestMethod'];
      a.name = 'testScheduledApexFromTestMethodUpdated';
      update a;
   }   
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top