+ Start a Discussion
Vijay ummedaVijay ummeda 

how to execute batch apex excluding holidays

Hi Team,

How can we execute a batch apex which is scheduled to run from monday to friday and exculde any public holidays  it should nor run on any public holidays.

One way is we can use schedulable class but how can we exclude a public holiday 


Thanks,
Vijay.
Best Answer chosen by Vijay ummeda
SwethaSwetha (Salesforce Developers) 
HI Vijay,
Check https://salesforce.stackexchange.com/questions/32387/excluding-weekend-and-holidays-from-my-days-number that discusses similar scenario.

Use the Business Hours object(https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm) to attain this functionality.

Example code:
Date txnDate = System.today();
    Date systemDate = txnDate.addDays(10);

    Integer holidaysCount = 0;
    BusinessHours bh;
    List<BusinessHours> temp = [SELECT ID, Name, IsDefault, IsActive From BusinessHours 
                                    WHERE IsDefault = true 
                                    OR Name = 'BANK'];
    for (BusinessHours b : temp) {
        if (b.IsDefault) {
            bh = b;
        }
    }
    while (txnDate != systemDate) {
        Datetime now = Datetime.newInstance(txnDate.year(), txnDate.month(), txnDate.day(), 0, 0, 0);        
        Boolean isHoliday = !BusinessHours.isWithin(bh.Id, now);
        System.debug('The value of isHoliday is : '+isHoliday);
        if (isHoliday) {
            holidaysCount++;
        }
        txnDate = txnDate.addDays(1);
    }
    System.debug('The holidays are : '+holidaysCount);

If this information helps, please mark the answer as best.Thank you

All Answers

VinayVinay (Salesforce Developers) 
You can try using Holiday object to exclude holidays.  Check below reference.

https://developer.salesforce.com/forums/?id=9060G000000Xeq3QAC

Thanks,
SwethaSwetha (Salesforce Developers) 
HI Vijay,
Check https://salesforce.stackexchange.com/questions/32387/excluding-weekend-and-holidays-from-my-days-number that discusses similar scenario.

Use the Business Hours object(https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_businesshours.htm) to attain this functionality.

Example code:
Date txnDate = System.today();
    Date systemDate = txnDate.addDays(10);

    Integer holidaysCount = 0;
    BusinessHours bh;
    List<BusinessHours> temp = [SELECT ID, Name, IsDefault, IsActive From BusinessHours 
                                    WHERE IsDefault = true 
                                    OR Name = 'BANK'];
    for (BusinessHours b : temp) {
        if (b.IsDefault) {
            bh = b;
        }
    }
    while (txnDate != systemDate) {
        Datetime now = Datetime.newInstance(txnDate.year(), txnDate.month(), txnDate.day(), 0, 0, 0);        
        Boolean isHoliday = !BusinessHours.isWithin(bh.Id, now);
        System.debug('The value of isHoliday is : '+isHoliday);
        if (isHoliday) {
            holidaysCount++;
        }
        txnDate = txnDate.addDays(1);
    }
    System.debug('The holidays are : '+holidaysCount);

If this information helps, please mark the answer as best.Thank you
This was selected as the best answer