Skip to main content
imran m (infosys) が「#Apex」で質問
Hi Team ,

Could you please help me below apex batch job need to run every minute

but no lock it's executng every 1hour please find below screenshot

is it possible to run batch every minute

global class ServiceClaimBatchimplements Database.Batchable<SObject>, Database.AllowsCallouts, Database.Stateful{

    private  void rescheduleBurstSMSResponse(){

        try{

        Datetime dt = system.now().addMinutes(1);          

        String day = string.valueOf(system.now().day());

        String month = string.valueOf(system.now().month());

        String hour = string.valueOf(system.now().hour());

        String minute = string.valueOf(system.now().minute());

        String second = string.valueOf(system.now().second());

        String year = string.valueOf(system.now().year());

        

        String cronExpression = '' + dt.second() + ' ' + dt.minute() + ' ' + dt.hour() + ' ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year();

        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;

 

            

            if(Test.isRunningTest()) {

            //This will throw Divide by 0 error and code will move to catch block

            Integer divideError = 123/0;

        }

                System.schedule(cronExpression, strSchedule, new ScheduleServiceClaim());

        }catch(Exception ex){

            System.debug(ex);

            System.debug(ex.getMessage());

            System.debug(ex.getLineNumber());

        }

    }

 ...........................................................

global class ScheduleServiceClaim implements Schedulable {

    global void execute(SchedulableContext sc){

        Database.executeBatch(new ServiceClaimBatch());

    }

}

Will appricate as best answer please help me it is killing my head 
3 件の回答
  1. 2022年3月21日 11:33
    Yes It is possiable. 

    Salesforce does not allow to schedule bach class every minute. But you can use trick to schedule it for every minute.

    Use a function and run a for loop to schedule the class for every minute for 1 hr. 

    Example :

    Like  schedule 4.05 ---> after one hr it will run at 5.05.

    then for 4.06 ---> it will run again at 5.06.

    for 4.07 ---> will again run at 5.07.

    This is how schedule it for 60 times for every minute. Every minute will execute after 1 hr which is allowed.

    Here is the function and CRON EXPRESSION that runs every minutes

     

    // CRON EXPRESSION to schedule per minute in salesforce

    global static void runSchedule(){

    for(Integer i =0; i < 60; i++){

    String CRON_EXP = '0 '+i+' * * * ?';

    GenericRecordSchedulableClass gensSch = new GenericRecordSchedulableClass();

    System.schedule('Generic every min'+i, CRON_EXP, gensSch);

    }

    }

    If it helped you. support this answer.

     
0/9000