Aptitude (12) C (2) C++ (10) CSS (1) Data Structure (2) English (7) General (14) HTML (3) Integration (1) Itinerary and Travel Cost (1) Java (29) JQ (5) JS (5) JSP (1) Lightning (14) logical (3) Math (1) Music Notation (17) MySql (4) Network (1) Regex (1) salesforce (131) Servlet (4) SF Admin (31) SF Dev (102) SQL (4) SQL_SERVER (1) StudyPoints (2) ytChannels (2) अन्ताक्षरी (4)

Schedule a Batch/Scheduler in every 5Mins

Schedule Class:

global class SchedulerClass implements Schedulable 
{
   global void execute(SchedulableContext SC) 
   {
      SchedulerClass sClass=new SchedulerClass ();
      Database.executeBatch(sClass,100); 
   }
}

==========================================================


Batch Class:

global class BatchJob implements  Database.Batchable<sObject>
{
   global Database.QueryLocator start(Database.BatchableContext BC)
    {
     return Database.getQueryLocator([Select id from .....]);  
    }
    
   
    global void execute(Database.BatchableContext BC, List<Case> listofCases) 
    {
    ...........

     
     }

global void finish(Database.BatchableContext BC)
    {
       string jobId;
        list<CronTrigger> CronJOB = [Select Id, CronJobDetail.Name, State, NextFireTime From CronTrigger where CronJobDetail.Name = 'JOB'];
        if(CronJOB.size() > 0)
        jobId = CronJOB [0].id;
        
        datetime nextScheduleTime = system.now().addMinutes(5);
        integer Hour = nextScheduleTime.Hour();
        integer min = nextScheduleTime.Minute();
        string CRON_EXP= '0 '+min+' '+Hour+' * * ?';
       if(jobId != null && jobId!= '')
        system.abortjob(jobId);
        system.schedule ('JOB', CRON_EXP, new SchedulerClass());
    }
    
 
}

No comments: