Scheduler class for Batch apex in Salesforce



我已经编写了批处理顶点代码,它将向我的Chatter组发送Good morning消息。我想为这个批处理顶点编写调度程序类。

global class PostMessage Implements Database.batchable<sObject>
{
    global Database.queryLocator start(Database.BatchableContext BC)
    {
        return Database.getqueryLocator([Select id,Name from CollaborationGroup where Name='Techila Group' LIMIT 1]); 
    }
    global void execute(Database.BatchableContext BC,List<Account> acct)
    {
        FeedItem post=new FeedItem();
        post.ParentID='0F9280000000B0t';
        post.createdbyID=UserInfo.getuserId();
        post.Body='Good Morning';
        insert post;
    }
    global void finish (Database.BatchableContext BC)
    {
    }
}

在匿名块中只需要执行一行代码

System.schedule("每小时第0分钟","0 0***?",new scheduledMerge());

然后,正如您所说,您需要创建一个可调度的类。。。并从那里执行您的批处理类。

global class schedule implements Schedulable {
   global void execute(SchedulableContext SC) {
       PostMessage batch = new PostMessage();
       Database.executeBatch(batch, 200);
   }
}

查看时间表类的文档:

https://developer.salesforce.com/docs/atlas.en-us.192.0.apexcode.meta/apexcode/apex_scheduler.htm

最新更新