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 

Display Working Days

Hi,
i have to display no of holidays in a month/year through apex triggers .i am failed to display holidays .Tell me where can i make a mistake in class or trigger.please tell me the solution.
Thanks
Galeeb SK

apex class:

public class calculateWorkingDaysBetweenTwoDates{

public Integer WorkingDaysBetweenTwoDates(Date date1,Date date2){

               List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];

                Integer allDaysBetween = date1.daysBetween(date2);
                Integer allWorkingDays=0;
                for(Integer k=0;k<allDaysBetween ;k++ ){
                    if(checkifItisWorkingDay(date1.addDays(k),holidays)){
                        allWorkingDays++;
                    } 
                }

                return allWorkingDays;

      }

public boolean checkifItisWorkingDay(Date currentDate,List<Holiday> holidays){
                 Date weekStart  = currentDate.toStartofWeek();
                for(Holiday hDay:holidays){
                        if(currentDate.daysBetween(hDay.ActivityDate) == 0){
                                 return false;
                        }
                }
               if(weekStart.daysBetween(currentDate) ==0 || weekStart.daysBetween(currentDate) == 6){
                       return false;
                } else 
                       return true;
  }
  }

apex trigger:

trigger days on Holiday__c (after insert) {

//for(Holiday__c h2 : trigger.new){

 if(trigger.isInsert){
 
calculateWorkingDaysBetweenTwoDates.WorkingDaysBetweenTwoDates();


}
}
ssssssssssssss

private Integer getDiffBusinessDays(Date startdt, Date enddt) {
 	Date tempdate = null;
 	if (startdt > enddt) {
 	tempdate = enddt;
 	enddt = startdt;
 	startdt = tempdate;
 	}
Integer i = Math.mod((date.newinstance(1985, 6, 24)).daysBetween(startdt),7); // 24/6/85 was a monday
Map<Integer, Map<Integer, Integer>> m = new Map<Integer, Map<Integer, Integer>> {
0 => new Map<Integer, Integer> { 1 => 2 , 2 => 3 , 3 => 4 , 4 => 5 , 5 => 5 , 6 => 5 },
1 => new Map<Integer, Integer> { 1 => 2 , 2 => 3 , 3 => 4 , 4 => 4 , 5 => 4 , 6 => 5 },
2 => new Map<Integer, Integer> { 1 => 2 , 2 => 3 , 3 => 3 , 4 => 3 , 5 => 4 , 6 => 5 },
3 => new Map<Integer, Integer> { 1 => 2 , 2 => 2 , 3 => 2 , 4 => 3 , 5 => 4 , 6 => 5 },
4 => new Map<Integer, Integer> { 1 => 1 , 2 => 1 , 3 => 2 , 4 => 3 , 5 => 4 , 6 => 5 },
5 => new Map<Integer, Integer> { 1 => 0 , 2 => 1 , 3 => 2 , 4 => 3 , 5 => 4 , 6 => 5 },
6 => new Map<Integer, Integer> { 1 => 1 , 2 => 2 , 3 => 3 , 4 => 4 , 5 => 5 , 6 => 5 }
};
Integer i2 = Math.mod((startdt.daysBetween(enddt)),7);
Integer i3 = (m.get(i)).get(i2);
if (i2 == null || i2 < 1 || i2 > 6) {
if (i >= 0 && i <= 4) { i3 = 1; } 
else  { i3 = 0; }
}
i3 = i3 + 5 * (Math.floor( ((Decimal) startdt.daysBetween(enddt)).divide(7,4))).intValue(); 
if (tempdate != null) i3 *= -1; // negative number of days
return i3;
    }
This should work I guess.


~S
Waqar Hussain SFWaqar Hussain SF
Use this one method in your class instead of using two methods
 
public class calculateWorkingDaysBetweenTwoDates{
public Integer WorkingDaysBetweenTwoDates(Date date1,Date date2){

               List<Holiday> holidays=[Select h.StartTimeInMinutes, h.Name, h.ActivityDate From Holiday h];
				set<date> holidayslist = new set<date>();
								for (Holiday h : holidays){
								holidayslist.add(h.ActivityDate);
								}
                Integer allDaysBetween = date1.daysBetween(date2);
                Integer allWorkingDays=0;
                for(Integer k=0;k<allDaysBetween ;k++ ){
                    if(holidayslist.add(date1.addDays(k))){
                        allWorkingDays++;
                    } 
                }
                return allWorkingDays;

      }
}

tested and work fine for me.
Galeeb SKGaleeb SK
Hi Vickey,

how to invoke this class to trigger.from the above mentioned class i got an error
Thanks
Galeeb SK
Waqar Hussain SFWaqar Hussain SF
trigger days on Holiday__c (after insert) {

 if(trigger.isInsert){
	date date1 = date.today();
	date date2 = date.today().addMonths(11);
	integer woerkingdays = calculateWorkingDaysBetweenTwoDates.WorkingDaysBetweenTwoDates(date1, date2);
	system.debug('woerkingdays:: '+woerkingdays);
 }


}
Galeeb SKGaleeb SK
Hi vickey 
it will display error like

Error: Compile Error: Method does not exist or incorrect signature: calculateWorkingDaysBetweenTwoDates.WorkingDaysBetweenTwoDates(Date, Date) at line 7 column 26

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);
     hh.Working_Days__c= calculateWorkingDaysBetweenTwoDates.WorkingDaysBetweenTwoDates(date1, date2);
    system.debug('hh.Working_Days__c:: '+hh.Working_Days__c);
 }

}
}
apex class:

public class calculateWorkingDaysBetweenTwoDates{
public Integer WorkingDaysBetweenTwoDates(Date date1,Date date2){

               List<Holiday__c> holidays=[Select Name,StartTimeInMinutes__c, ActivityDate__c From Holiday__c ];
                set<date> holidayslist = new set<date>();
                                for (Holiday__c h : holidays){
                                holidayslist.add(h.ActivityDate__c);
                                }
                Integer allDaysBetween = date1.daysBetween(date2);
                Integer allWorkingDays=0;
                for(Integer k=0;k<allDaysBetween ;k++ ){
                    if(holidayslist.add(date1.addDays(k))){
                        allWorkingDays++;
                    } 
                }
                return allWorkingDays;

      }
}
Waqar Hussain SFWaqar Hussain SF
try this code.
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);
 }

}
}
apex class:

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

               List<Holiday__c> holidays=[Select Name,StartTimeInMinutes__c, ActivityDate__c From Holiday__c ];
                set<date> holidayslist = new set<date>();
                                for (Holiday__c h : holidays){
                                holidayslist.add(h.ActivityDate__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;
                

      }
}

 
Galeeb SKGaleeb SK
Hi Vickey
It's working fine but checked the out put but it is different.actually iam defined in company profile settings  create a holiday as sunday ,start date is 1st jan2016,end date is 31st dec2016.but it display working days as 334,actually as per  our requirement working days are 313.tell me where can i make a mistake in defining in company profile.

Thanks
Galeeb SK