+ Start a Discussion
TheLearnerTheLearner 

Including bankholidays--urgent

HI Experts,



My requirment is that i need to calculate bankholidays which are coming on weekdays, i have object called Holidaylist(Holiday_List__c) for this object there fiels called holiday date(Holiday_Date__c) this object contain all the holiday list( Boxing day=28/12/2015,christimas day=25/12/2015).

If Non Working Day = Check (true) then Estimate Start Date, Estimate End Date, Actual Start Date, Target Stop and Work Stop can be any date including non working days (i.e. Monday to Friday, Saturday, Sunday or Bank Holiday).
If Non Working Day = Unchecked (false) then Estimated Start Date, Estimated End Date, Actual Start Date, Target Stop and Work Stop can only be working day (i.e. Monday to Friday excluding bank holidays).


for the above requirement(which boled above) i wrote trigger  weekends and weekdays im not getting how to calculate the bankholidays. could anyone help me.

trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update)
{
    
     for(SW_Notice__c swnotice : trigger.new)
     {

            if(!swnotice.Non_Working_Day__c)
            {

                if(((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if(((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if(((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if(((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    

}
Best Answer chosen by TheLearner
Abhishek BansalAbhishek Bansal
Hi,

I have updated the code.
Now it will work fine.
 
trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update)
{
	List<Holiday_List__c> holidayList = new List<Holiday_List__c>([Select Holiday_Date__c from Holiday_List__c]);
	Set<Date> holidayDayeSet = new Set<Date>();
	
	for(Holiday_List__c holiday : holidayList){
		holidayDayeSet.add(holiday.Holiday_Date__c);
	}
     for(SW_Notice__c swnotice : trigger.new)
     {

            if(!swnotice.Non_Working_Day__c)
            {

                if(holidayDayeSet.contains(swnotice.Estimated_Start_Date__c) || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Estimated_End_Date__c) || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Actual_Start_Date__c) || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Target_Stop__c) || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Works_Stop__c) || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    

}
Let me know if still there is an issue.

Thanks,
Abhishek.
 

All Answers

Abhishek BansalAbhishek Bansal
Hi,

I have added the validation for Holidays date also in your trigger.
Please change your trigger code with below code :
 
trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update)
{
	List<Holiday_List__c> holidayList = new List<Holiday_List__c>([Select Holiday_Date__c from Holiday_List__c]);
	Set<Date> holidayDayeSet = new Set<Date>();
	
	for(Holiday_List__c holiday : holidayList){
		holidayDayeSet.add(holiday.Holiday_Date__c);
	}
     for(SW_Notice__c swnotice : trigger.new)
     {

            if(!swnotice.Non_Working_Day__c)
            {

                if(holidayDayeSet.contains(Estimated_Start_Date__c) || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if(holidayDayeSet.contains(Estimated_End_Date__c) || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(holidayDayeSet.contains(Actual_Start_Date__c) || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if(holidayDayeSet.contains(Target_Stop__c) || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if(holidayDayeSet.contains(Works_Stop__c) || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    

}
Now your trigger will also throw error if any of the date fields contains holiday date in it.

Please let me knoe if you need more help on this.

Thanks,
Abhishek
 
TheLearnerTheLearner
Hi Abhishek,

One more thing i forgot to tell you there is no relation between Holidaylist object and sw notice object. if nonworking day check is checked it should allow any kind of date holiday or sunay or saturday, if the nonworkingday is uncheked it need to throw error.
Abhishek BansalAbhishek Bansal
Yes i know your requirement and according to that i have update the trigger code which i have already given you.
Please test your all possible cases with the trigger and let me know if any of the case is not working for you.

Thanks,
Abhishek
TheLearnerTheLearner
HI Abhishek,
Thanks for the ur qucik reply,
Its thrwoing error because holidaylist object contain  only one field that is Holiday_Date__c, but we wrote like this

: Compile Error: Variable does not exist: Estimated_Start_Date__c at line 21 column 44
"if(holidayDayeSet.contains(Estimated_Start_Date__c) || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')"
Abhishek BansalAbhishek Bansal
Hi,

I have updated the code.
Now it will work fine.
 
trigger CheckonNonWorkingday on SW_Notice__c (before insert,before update)
{
	List<Holiday_List__c> holidayList = new List<Holiday_List__c>([Select Holiday_Date__c from Holiday_List__c]);
	Set<Date> holidayDayeSet = new Set<Date>();
	
	for(Holiday_List__c holiday : holidayList){
		holidayDayeSet.add(holiday.Holiday_Date__c);
	}
     for(SW_Notice__c swnotice : trigger.new)
     {

            if(!swnotice.Non_Working_Day__c)
            {

                if(holidayDayeSet.contains(swnotice.Estimated_Start_Date__c) || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated start date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Estimated_End_Date__c) || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Estimated_End_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Estimated End date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Actual_Start_Date__c) || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sun')
                {

                    swnotice.addError('Actual start date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Target_Stop__c) || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Target_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Target Stop date must be a Weekday');

                }

                else if(holidayDayeSet.contains(swnotice.Works_Stop__c) || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sat' || ((DateTime)swnotice.Works_Stop__c).format('E') == 'Sun')
                {

                    swnotice.addError('Work Stop date must be a Weekday');

                }

            }

        }

    

}
Let me know if still there is an issue.

Thanks,
Abhishek.
 
This was selected as the best answer
TheLearnerTheLearner
Hi Abhishek,

Im getting this error, could you check this once please

Compile Error: Incompatible element type Datetime for collection of Date at line 57 column 29    

These field date typees are belwo
Actual_Start_Date__c(Date,time)
Target_Stop__c(Date)
Works_Stop__c(Date)
Abhishek BansalAbhishek Bansal
Please provide the datatypes of following fields :
  1. Holiday_Date__c (Holiday List Object)
  2. Estimated_Start_Date__c
  3. Estimated_End_Date__c
And please send me the complete code of ur trigger because the code which i have given you is just of 56 lines and your error is coming on 57 no. line so please send me the complete code.

Thanks,
Abhishek
TheLearnerTheLearner

Hi Abhishek,

Im getting this error, could you check this once please

Compile Error: Incompatible element type Datetime for collection of Date at line 57 column 29

 else if(holidayDayeSet.contains(swnotice.Actual_Start_Date__c) || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sat' || ((DateTime)swnotice.Actual_Start_Date__c).format('E') == 'Sun')    

These field date typees are belwo
Actual_Start_Date__c(Date,time)
Target_Stop__c(Date)
Works_Stop__c(Date)
Estimated_Start_Date__c(Date)
Estimated_End_Date__c(Date)
Holiday_Date__c(Date)
Abhishek BansalAbhishek Bansal
Hi,

Please change the above line as follows :
 
else if(holidayDayeSet.contains(Date.newInstance(swnotice.Actual_Start_Date__c.year(),swnotice.Actual_Start_Date__c.month(),swnotice.Actual_Start_Date__c.day()) || swnotice.Actual_Start_Date__c.format('E') == 'Sat' || swnotice.Actual_Start_Date__c.format('E') == 'Sun')

Please update that line from above line and let me know if you have any issue.

Thanks,
Abhishek
 
TheLearnerTheLearner
HI abhishek,

Thanks for the code its wroking fine, If i enter the Estimated start date as holiday date its throwing "Estimated start date must be a Weekday",
i need error that Estimated start date should not be holiday , could you help me please.
Abhishek BansalAbhishek Bansal
Do you need different messages for weekends and Holidays ??
TheLearnerTheLearner
Yes,
Already you mentioned the weekdays, if its true it will include all the days so there no issues, but if the checkbox is unchecked then we need to day weekdays so i need holidays messges for the uncchek check box
Abhishek BansalAbhishek Bansal
Hi,

It will require some code level changes so i would request to close this question here and post a new question regarding the error messages.
Please send me the link of new question and i will help you as soon as possible.

Close this question here and post your new requirement in another question.

Thanks,
Abhishek.
TheLearnerTheLearner
HI Abhishek,

Thanks this is my new post. could you help me please https://developer.salesforce.com/forums/ForumsMain?id=906F0000000BXtLIAW
Abhishek BansalAbhishek Bansal
Hi,

Thankyou for following the right way of asking the questions on developer forum.
As a best practise please mark this question as SOLVED so that it would be helpful for others to find a solution and also avoids the confusion.

In the mean time i am modifying your code and will reply you very soon on the new question.

Thanks,
Abhishek