function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Galeeb SKGaleeb SK 

Displaying Holidays

Hi
I am wrote a trigger to display no of holidays like sun days,saturdays etc in the year.But iam fail to display proper out put.Incase we defining any thing in company information like holiday,business hours,fiscal year etc.tell me where can i make a mistake, my code is 

APEX CLASS:

public class calculateWorkingDaysBetweenTwoDates{

public static void  WorkingDaysBetweenTwoDates(Date date1,Date date2 , id holidayId){


               List<Holiday__c> holidays=[Select Name, Start_Date__c,End_Date__c From Holiday__c ];
               
                set<date> holidayslist = new set<date>();
                
                                for (Holiday__c h : holidays){
                                
                                holidayslist.add(h.End_Date__c );
                                
                                }
                Integer allDaysBetween = date1.daysBetween(date2 );
                
                Integer allWorkingDays=0;
                
                for(Integer k=0;k<allDaysBetween ;k++ ){
                
                    if(holidayslist.add(date1.addDays(k))){
                    
                        allWorkingDays++;
                    } 
                }
                Holiday__c hh = new Holiday__c();
                
                hh.id= holidayId;
                
                hh.Working_Days__c = allWorkingDays;
                
                update hh;
                

      }
}
APEX TRIGGER:
trigger days on Holiday__c (after insert) {


 if(trigger.isInsert){
 
 for(Holiday__c hh:trigger.new){
 
    date date1 = date.today();
    
    date date2 = date.today().addMonths(11);
    
    calculateWorkingDaysBetweenTwoDates.WorkingDaysBetweenTwoDates(date1, date2,hh.id);
    
    system.debug('hh.Working_Days__c:: '+hh.Working_Days__c);
 }

}
}
Thanks 
Galeeb
Vijay NagarathinamVijay Nagarathinam
Hi Galeeb,

Create Holidays. Admin Setup --> Comapany Info --> Holidays
 
Once you have configured the holidays, Write an Apex trigger (before insert) for the Custom object to check for holidays and add the other business logic to exclude that day, 

Thanks,
Vijay
Galeeb SKGaleeb SK
Hi Vijay,
Iam also tried several times configured holidays it won't be work properly.Suppose i define holiday as sunday in Holidays.start date is 3/1/2016 and end date is 25/12/2016.Actual out put is 52 but it display 32.Tell me where can i make mistake.
Thanks
Galeeb