Thursday 31 March 2016

Apex: Creating CRON Expression dynamically from VF page

CRON Expression:

A command to an operating system or server for a job that is to be executed at a specified time.

CRONTrigger:

An object that contains schedule information for a schedule job.This object contain all the information related to any schedule job in your organization. Read More Here!

Visualforce Page for Dynamic CRON Creation:

The use case this page covers is when users want to schedule  a class from VF page.User have choice to set schedule date and time according to his convenience from any day of week.

We will be using Multiselect picklist to show days of weeks as user might want to schedule it for more than 1 day.



We will use single select picklist in order to capture preferred time from user.


VF Page Code:

     <apex:pageBlock id="pb4">
            <apex:pageBlockSection title="Frequency" id="pbs10" collapsible="false" 
             columns="1">
            
                <apex:selectList label="Recurs Every Week On" multiselect="true"  
                 size="5" id="FrequencyWeekly" value="{!SelectedFreqValue}">
                    <apex:selectOptions value="{!OppWeekDays}"  />
                       
                </apex:selectList>
               
                <apex:selectList label="Preferred Start Time" multiselect="false"  
                 size="1" id="Time" value="{!SelectedTime}">

                    <apex:selectOptions value="{!FrequencyTime}"/>
                </apex:selectList>
                    
                <apex:outputLabel styleclass="labelCol vfLabelColTextWrap" 
                 value="Exact time will be dependent on Job queue activity" 
                 style="text-align:left !important; font-size: 10px !important;"/>
            </apex:pageBlockSection>
            
        </apex:pageBlock>

Controller Class Code

//Defining Properties for capturing Frequency
    public String SelectedFreqValue {get;set;} 
    public string SelectedTime {get;set;}

//Defining List of selectoption
public List<selectOption> OppWeekDays{get;set;}
 public List<selectoption> FrequencyTime{get;set;}

/****************************************************getFrequencyTime**********************************/
    //get frequency time
    public List<selectOption> getFrequencyTime(){
        
        FrequencyTime = new List<selectoption>();
        
        FrequencyTime.add(new selectOption('0',Constants.CONST_TWELVE_AM));
        FrequencyTime.add(new selectOption('1',Constants.CONST_ONE_AM));
        FrequencyTime.add(new selectOption('2',Constants.CONST_TWO_AM));
        FrequencyTime.add(new selectOption('3',Constants.CONST_THREE_AM));
        FrequencyTime.add(new selectOption('4',Constants.CONST_FOUR_AM));
        FrequencyTime.add(new selectOption('5',Constants.CONST_FIVE_AM));
        FrequencyTime.add(new selectOption('6',Constants.CONST_SIX_AM));
        FrequencyTime.add(new selectOption('7',Constants.CONST_SEVEN_AM));
        FrequencyTime.add(new selectOption('8',Constants.CONST_EIGHT_AM));
        FrequencyTime.add(new selectOption('9',Constants.CONST_NINE_AM));
        FrequencyTime.add(new selectOption('10',Constants.CONST_TEN_AM));
        FrequencyTime.add(new selectOption('11',Constants.CONST_ELEVEN_AM));
        FrequencyTime.add(new selectOption('12',Constants.CONST_TWELVE_PM));
        FrequencyTime.add(new selectOption('13',Constants.CONST_ONE_PM));
        FrequencyTime.add(new selectOption('14',Constants.CONST_TWO_PM));
        FrequencyTime.add(new selectOption('15',Constants.CONST_THREE_PM));
        FrequencyTime.add(new selectOption('16',Constants.CONST_FOUR_PM));
        FrequencyTime.add(new selectOption('17',Constants.CONST_FIVE_PM));
        FrequencyTime.add(new selectOption('18',Constants.CONST_SIX_PM));
        FrequencyTime.add(new selectOption('19',Constants.CONST_SEVEN_PM));
        FrequencyTime.add(new selectOption('20',Constants.CONST_EIGHT_PM));
        FrequencyTime.add(new selectOption('21',Constants.CONST_NINE_PM));
        FrequencyTime.add(new selectOption('22',Constants.CONST_TEN_PM));
        FrequencyTime.add(new selectOption('23',Constants.CONST_ELVEN_PM));
        
        return FrequencyTime;
    }


/*************************************************getOppWeekDays****************************************/
    //get renewal opp week days list
    public List<SelectOption> getOppWeekDays(){
        
        OppWeekDays = new List<selectoption>();
        
        OppWeekDays.add(new selectOption('SUN',CONST_SUNDAY));
        OppWeekDays.add(new selectOption('MON',CONST_MONDAY));
        OppWeekDays.add(new selectOption('TUES',CONST_TUESDAY));
        OppWeekDays.add(new selectOption('WED',CONST_WEDNESDAY));
        OppWeekDays.add(new selectOption('THUR',CONST_THURSDAY));
        OppWeekDays.add(new selectOption('FRI',CONST_FRIDAY));
        OppWeekDays.add(new selectOption('SAT',CONST_SATURDAY));
        
        return OppWeekDays;
    }

Create a constant class where you can add all times you want to show on page.Like i create a 
class name "Constant" and add times from "12 AM" till "11 PM" and names of days from Sunday till Saturday.


CRON Creation Method


public string GetCronStringForSchedule(){
        Integer Schdtime = Integer.valueOf(SelectedTime);
         string sch = '';
            string a = '';
            SelectedFreqValue= SelectedFreqValue.replaceAll( '\\s+', '');
            SelectedFreqValue = SelectedFreqValue.remove(')');
            SelectedFreqValue = SelectedFreqValue.remove('(');
            List<String> ListOfValues = SelectedFreqValue.split('\\,');
            if(ListOfValues.size() >0)
                a = ListOfValues.get(0)+ '-' + ListOfValues.get(ListOfValues.size()- 1);
            else
                a = ListOfValues.get(0);
            sch = '0 0 '+ Schdtime +' ? * '+ a;
    }

public string JobSchedular(){
string sch = GetCronStringForSchedule();
      string  jobID = system.schedule(BATCH_JOB_NAME, sch, batchjob);
return jobID;
}

Save this job ID some where so you can re use it in future if you want to abort this job from page.

Hope this helps you. Happy Coding !!

1 comment: