如何在Apex中实现调度程序?

发布于 2024-10-01 22:22:01 字数 1369 浏览 4 评论 0 原文

我在 salesforce 中看到类似调度程序的功能,但它在某种程度上与 salesforce 提供的现有功能相关,并且就我的研究而言,没有提供示例源代码。

我想做的是创建自己的调度程序,根据日期发送简单的电子邮件。

目标:

自定义对象玩家有字段

startDate : date like '2010-11-01'
email : text field like [email protected]
name : player's name like John.

如果今天的日期是开始日期的前一天,我想向玩家发送电子邮件。 例如,玩家的名字是 John,如果今天是 2010-12-10 并且一个玩家的 startDate 设置为 2010- 12-11,发送了“hello John” 电子邮件。

不幸的是,我无法在网上找到好的示例或教程,也找不到如何使用 Apex 执行此操作的 salesforce 文档。 谁能指出从哪里开始?

更新

我想扩展eyescream的答案。

设置调度程序后,您可以设置要采取的后续操作,例如使用模板发送电子邮件或将自定义对象字段设置为某些值。

我发现下面对于使用 Visualforce 格式的电子邮件模板的人很有用。

我有自定义对象“alertTester”,它引用了其他对象“custom”,甚至这个对象“custom”也引用了另一个对象“custom1GrandChild”,并且所有关系(我认为最多 3 或 5 层)都可以像下面这样访问。

我在下面进行了测试并且工作正常。现在我收到了包含我的条件集的电子邮件:)

<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>

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 [email protected]
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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

所有深爱都是秘密 2024-10-08 22:22:01

在深入了解 Apex 之前,请先检查不涉及代码的解决方案...

电子邮件警报 + 工作流规则应该为您提供此场景中所需的所有功能,并且只需单击即可,无需任何代码。

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.

梦过后 2024-10-08 22:22:01

我再次回答我自己的问题。

在下面的链接中,搜索 schedule

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

看起来 Apex 有可调度的接口,我可以实现并设置 cron 任务。

以下是文档中提供的示例代码:

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;
   }   
}

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;
   }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文