You need to sign in to do that
Don't have an account?

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();
}
}
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();
}
}
~S
tested and work fine for me.
how to invoke this class to trigger.from the above mentioned class i got an error
Thanks
Galeeb SK
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;
}
}
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