0

Any idea of how to calculate the number of holidays between two given dates?

Object: Time_Off__c

Fields: Start_date__c, End_date__c , Included_Holidays__c presenting the number of holidays between Start_date__c and End_date__c

 public static List<Holiday> lstHolidays = new List<Holiday>();
/*************** Holidays days number between Start 'n End date ***********/
public static void MyHolidays(List<Time_Off__c> lstTimeOff){
    try{
    lstTimeOff=[Select Start_date__c, End_date__c,Included_Holidays__c From Time_Off__c];
    lstHolidays = [Select ID,ActivityDate From Holiday ];
    for (Time_Off__c TO: lstTimeOff){
        for(Holiday h: lstHolidays){
        if(h.ActivityDate >= TO.Start_date__c &&    h.ActivityDate<= TO.End_date__c)
        System.debug('###h.ActivityDate'+ h.ActivityDate);
        TO.Included_Holidays__c +=1;    
        }
    }
    update lstTimeOff;
    }
catch(Exception e){
     System.debug('###Exception E :'+ e.getMessage());
            }
 }

And my trigger is:

trigger TimeOffAfterInsert on Time_Off__c (after insert) {
    if(!Trigger.New.isEmpty()){
    TRTimeOffTreatement.MyHolidays(Trigger.New);
}
}

Seems logic but I still have this exception error:

System.NullPointerException: Attempt to de-reference a null object: Class.TRTimeOffTreatement.MyHolidays: line 16, column 1

means this line TO.Included_Holidays__c += lstHolidays.size();

  • 1
    You need to clarify what you mean by "holidays". Some fixed holiday calendar or Salesforce's Holiday objects or just weekends or...? – Keith C Apr 25 '14 at 16:06
  • Concerning weekends it's done I can have the number of weekends between two dates in formula field! I'm talking here about an already specified Holidays in salesforce's holidays as you said. I wrote an apex code for that matter but seems not working, See my edit – LoveLace Apr 25 '14 at 20:53
1

There are these issues with the currently posted code:

  • as this is an "after" trigger, any change you make to Included_Holidays__c will not be persisted; I can't see why this shouldn't be a "before" trigger instead
  • your NullPointerException is probably because TO.Included_Holidays__c starts out null; check if it is null and just assign the value instead in that case
  • you have a query inside a trigger loop which would very likely cause a governor limit to be hit if any kind of bulk insert of Time_Off__c is done
  • you are querying every Time_Off__c at the start of your code (likely to hit a governor limit) instead of just using the list passed in from the trigger

I am not very familiar with the Holiday object but various posts mention that one record can represent a recurring sequence. So you may need to add further logic (and perhaps there is API that can help - BusinessHours.isWithin mentions that it checks holidays).

  • Included_Holidays__c has a default value 0 => not null And yes, didnt pay attention to the SOQL into the for loop, but I have to select the activitydate between start and end date for each time off !!! – LoveLace Apr 26 '14 at 15:09
  • The error is persisting !! See my edited code – LoveLace Apr 26 '14 at 18:06
  • @KawtarBRAZI Your currently posted appears incomplete and I doubt would compile as it includes h.size() where h is not a list. I suggest you post an exact copy of the code that produces the error and the full error if you want further comment. – Keith C Apr 26 '14 at 23:18
  • Here is a correct code executing without error, but my included_holidays__c field is not calculated as I want. See updated code above – LoveLace Apr 27 '14 at 23:23
  • Found a solution with apex code, using a formula field, I can retreive both weekends and holidays (I've already inserted ) Here you R: – LoveLace Apr 28 '14 at 12:11
0

Setup -> Company Profile -> Holidays

Then you could code it something like this:

Date s = date.newInstance(2014,04,01); //Get your start and end dates however you want
Date e = date.newInstance(2014,12,31);
Holiday[] h = [Select ID From Holiday Where ActivityDate >= :s AND ActivityDate <= :e];
Integer numOfHolidays = h.size();
  • I've edited my code, but I have an exception raised! :/ . See my edit above – LoveLace Apr 26 '14 at 12:51
  • Change the += to simply =. Also, in your case you are going to need to get the list of holidays outside the loop and cycle through them for each TO record and check the values to see if the dates are within range. You code edit changes your use case so you no longer want to do it the way I have given See Keith C's post below – Eric Apr 26 '14 at 14:41
  • I put += because I've inisialised included_holidays__c field to 0 to avoid the exception – LoveLace Apr 26 '14 at 15:12

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.