0
votes

I have written batch apex code which will send a Good morning message to my Chatter group. I want to write scheduler class for this batch apex.

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

    }

}
2

2 Answers

0
votes

Only one line of code needed to be executed in anonymous block

System.schedule('Every 0th min of hour', '0 0 * * * ?', new scheduledMerge());

0
votes

Then as you say, you need to make a schedulable class... and execute your batch class from there.

global class schedule implements Schedulable {

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

}

Check out the documentation for schedule classes:

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