0

I have a Schedulable class named 'DeletarSemCheckinSchedule' that runs some DML's. i also have another Schedulable class named 'AgendarSchedulesCheckin'. The 'AgendarSchedulesCheckin' should be called by itself every hour and the 'DeletarSemCheckinSchedule' should be called sometimes by 'AgendarSchedulesCheckin' either.

If I call the 'AgendarSchedulesCheckin' inside Developer Console with the code bellow the job is executed, but both 'DeletarSemCheckinSchedule' and 'AgendarSchedulesCheckin' aren't called again.

AgendarSchedulesCheckin abc = new AgendarSchedulesCheckin();
abc.execute(null);

AgendarSchedulesCheckin:

    global class AgendarSchedulesCheckin Implements Schedulable {
    global void execute(SchedulableContext sc)
    {  
        Datetime horaAtual = datetime.now();
        Date dataAtual = horaAtual.date();
        Integer horaJob = horaAtual.hour();
        Integer diaJob;
        Integer mesJob;
        //Integer minute = horaAtual.minute();

        if(horaJob == 23){
            dataAtual = dataAtual+1;
            horaJob = 0;
        }else{
            //minute += 2; -- para teste
            horaJob += 1;
        }        

        diaJob = dataAtual.day();
        mesJob = dataAtual.month();

        String cron_exp = '0 40 '+horaJob+' '+diaJob+' '+mesJob+' ?';
        String jobName = 'jobTeste';
        System.Schedule(jobName, cron_exp, new AgendarSchedulesCheckin());

        PredialReservas__c PredialReservas = new PredialReservas__c();
        PredialReservas = [SELECT MinutosCheckin__c FROM PredialReservas__c WHERE Name = 'configBasica'];

        Integer tempoParaCheckin = (Integer) PredialReservas.MinutosCheckin__c;        
        Integer tamanhoFor = (60/tempoParaCheckin);        
        Integer minutoAux = 0;

        for(Integer x = 0; x < 3; x++){
            cron_exp = '0 '+minutoAux+' '+horaJob+' '+diaJob+' '+mesJob+' ?';
            jobName = 'jobTeste '+x;
            System.Schedule(jobName, cron_exp, new DeletarSemCheckinSchedule());
            minutoAux += tempoParaCheckin;
        }
    }

}

Can anyone help me to fix this?

Thanks in advance.

  • Are there any errors? – Eric Dec 14 '17 at 6:06
  • @Eric No, no errors appers. The Schedule Job are not created on the second time. – Bruno Perdigão Dec 14 '17 at 6:14
  • @Bruno Perdigão Probably you must be getting . System.AsyncException: The Apex job named "xyz" is already scheduled for execution. try to make jobName unique. eg. Add jobName = 'jobTeste '+x+System.now(); at both places where you are scheduling job. – Amit Dec 14 '17 at 9:44
0

Problem solved with one Schedule Class calling itself like this:

Integer tempoParaCheckin = (Integer) PredialReservas.MinutosCheckin__c;     
    System.debug('tempoParaCheckin - '+tempoParaCheckin);
    Integer qtdVezesRodaACadaHora = (60/tempoParaCheckin);
    System.debug('qtdVezesRodaACadaHora - '+qtdVezesRodaACadaHora);

    for (Integer i = 0; i < qtdVezesRodaACadaHora; i++) {
        String minuto = String.valueOf(i * (tempoParaCheckin));
        String CRON_EXP = '0 '+ minuto +' * * * ?';
        System.debug('CRON_EXP - '+CRON_EXP);
        //String CRON_EXP = '0 '+ minuto +' 5 * * ?';
        String jobName = 'Expira Reserva Predial '+i;
        System.schedule(jobName, CRON_EXP, new AgendarExpiraCheckin());
    }
| improve this answer | |
  • 1
    super important to make jobname unique for all time as duplicate jobnames will be a major headache to deal with. Don't use i, use something like the job's start datetime – cropredy Dec 17 '17 at 6:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.