• samapika singh 3
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

Hi Folks,

 

Requirement: To create a Schedule/Cron/Code job which executes after a specific interval, say after every 1 minute.

 

There is no direct feature in SFDC of doing it but with a tricky way.

Step 1: Create an Apex class which contains the business logic which is required to be executed after specific intervals.

Step 2: Create a Scheduleable Apex Class Like below:

 

global class ScheduleChatter implements Schedulable {
    global void execute(SchedulableContext SC)  {                 

        //The line of code below would contain the required business logic

        TestChatter.initGoogleApp();

        System.debug('-----------ScheduleChatter---------');

        String hour = String.valueOf(Datetime.now().hour());

        String min = String.valueOf(Datetime.now().minute() + 1);

        String ss = String.valueOf(Datetime.now().second()); 

        
String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';

        System.debug('-----------nextFireTime---------' + nextFireTime);

        

ScheduleChatter s = new ScheduleChatter();

         System.schedule('Job Started At ' + String.valueOf(Datetime.now()), nextFireTime, s);

        

}   

}

 

Step 3: This Schedule job is initiated by a code, which can be run on System log screen or via another trigger/class:

ScheduleChatter s = new ScheduleChatter();

String hour = String.valueOf(Datetime.now().hour());

String min = String.valueOf(Datetime.now().minute());

String ss = String.valueOf(Datetime.now().second());

String nextFireTime = ss + ' ' + min + ' ' + hour + ' * * ?';

system.schedule('Start me once', nextFireTime, s); 

 

The Schedulable class will keep on calling itself after every 1 minute and eventually calls the code which is needed to be called after specified interval of time.

 

This code is written as part of R&D.

 

Thanks,

Sam