I wanted to use ZOHO Creator to build a holiday request system for my team, so they can request holidays, I can approve them and they can see everyone's bookings on a calendar view. However - I ran into the same problem that it looks lilke many 'Zoho-ers' have come across in the past.....at best you can register a START and END date in a calendar view.....but difficult to get the days in the middle - without manually entering a record for every day.
Having looked through the forums & discussion boards, I couldn't find any real help in solving what seems to have been a problem for several years.
The fact that Creator Calendar is fine if you (A) only want a monthly view (B) only want to enter single day events and (C) can live with only short text fields appearing in the view.
I knew I would have to wait for Zoho to develop parts (A) & (C) - but part (B) seemed something that could be achieved with Deluge script.
So here was my solution....
I built a FORM called 'Master Year Calendar' with two fields, (I'll explain this form later)
DATE FIELD (Date) - Unique
WORK DAY (Decision Box)
Another form called BANK HOLIDAY with two fields (this will store a record of public holidays - Christmas Day etc)
BANK HOLIDAY (Date) -Unique
Description (String)
Then added some script to the Master Year Calendar
- actions
- {
- on add
- {
- Submit
- (
- type = submit
- displayname = "Submit"
- on success
- {
- if ((input.Date_field.getDayOfWeek() = 7) || (input.Date_field.getDayOfWeek() = 1))
- {
- input.WorkDay = false;
- }
- rec = Bank_Holiday [Bank_Holiday == input.Date_field];
- if (rec.Description != null)
- {
- input.WorkDay = false;
- }
- }
- )
What this does is : for every date entered into the FORM, it checks whether it is a working day (Monday to Friday) and whether it is a public holiday. If it is not a weekend and not a public holiday, the check box for 'Work Day' is set to true.
Now the boring bit, I got someone to add in 365 records, one for each day of the year - only took about 15 minutes using a calendar view to simply add a record per day. Couldn't find any way to do this by code - which is not surprising as this is the fundamental problem with using Zoho Calendar to display multi day events - the fact there is no concept of
For-Next loop or
Do-While.
So now I have a master FORM, with a record for every day of the year and a flag if that day is a working day.....
I then Build my Holiday Request Form
Resource (Dropdown list of staff names - could be a lookup of all employees form another form)
Start Date (Date)
End Date (Date)
Approved (Decision) - only visible when using my USERID
I then add another FORM :
Holiday Calendar Bookings - this is the form that will actually store the dates of approved holidays.
Resource (Single Line)
Date (Date)
Work Day (Decision Box)
Now the key bit of script that makes this worthwhile....
Added 'On Edit' script to the Holiday Request form, such that, if I change the status to 'APPROVED=TRUE' - it will then auto-populate every date of the holiday for an individual in
Holiday Calendar Bookings. It does this by using the
Master Year Calendar to act a pseudo '
Do While Loop'
The code explanation is :
Set two variables: START (one day before the first day of their holiday) and END (one day after their holiday finishes)
Then if the record (holiday is approved).....loop the the 'Master Year Calendar' which now has every day of the year in it and if the holiday request matches a particular day of the year, then add an entry into the
Holiday Calendar Bookings, I also populate whether it is a working day or not, because in another form, I count up how many holidays days, that are working days an employee has used, to calculate their holiday balance.
[
I know you can code the selection slightly differently by doing
for each rec in Master_Year_Calendar [(Date_field >= input.Date_From && Date_field <=input.Date_To)]
but I like to use variables where appropriate, such that reading the code later is slightly easier]
- Start = input.Date_From.addDay(-1);
- End = input.Date_To.addDay(1);
- if (input.Approved)
- {
- for each rec in Master_Year_Calendar [(Date_field > Start && Date_field < End)]
- {
- insert into Holiday_Calendar_Bookings
- [
- Resource = input.Resource
- Date_field = rec.Date_field
- Added_User = zoho.loginuser
- Working_Day = rec.WorkDay
- ]
- }
- }
Yes this code does loop through 365 records to sometimes only add three records into your final FORM, but (A) it's server side and (B) it processes instantly.
What you end up with is the basis of a system that you can specify a start and end date and have all the inclusive dates in that period populated, such that a calendar view can easily be used for showing bookings etc.
The image below shows that from just two 'Holiday Requests' one form Neil and one from Marianne, when they were approved, the full dates were populated into the calendar.
Hope someone finds this useful, as it is a routine/approach, I am very likely to use again & again.
Thanks