If start time of Change is in business hours, a new mandatory field needs to display.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-28-2024 12:44 AM
We have a requirement on Change Requests, that a new mandatory field be visible to users if they select a state time for their Change that is within business hours. I know how to set this to appear Mon-Fri, but I need to be able to also get it to only show if they select the start time as being between 08:00 and 18:00 Mon-Fri. How would I achieve this with a Client Script please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2024 04:02 AM
Wouldn't it work the same if you have the same code on the end date field? So if the end date field changes to inside business hours, it shows the fields as well?
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-24-2024 05:17 AM
For Changes that end within business hours, sure. However, say one starts 19:00 Monday and ends Wednesday 20:00, it's crossing business hours on the Tues and Weds, but wouldn't trigger as start and end times are outside of business hours. I need to somehow cater for that too.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-30-2024 05:24 AM
Then add more logic: if both are outside of business hours, but last longer than 14 hours (duration), it also needs to trigger.
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2024 12:13 AM
Any thoughts on how to achieve this with the previous script? Still learning scripting so struggling to figure out terminology required etc.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-02-2024 01:06 AM
The non-business hours are between 18:00 and 8:00 which means you have 14 hours. In case it's more than 14 hours, you will have it running over business hours. Include that in your script as well, something like below (it should run onChange on both start_date and end_date):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var startTime = g_form.getValue('start_date');
var endTime = g_form.getValue('end_date');
var duration = calculateDuration(startTime, endTime);
var showField = isWithinBusinessHours(newValue) || duration > 14;
g_form.setMandatory('u_workinghours', showField);
g_form.setVisible('u_workinghours', showField);
}
function isWithinBusinessHours(dateStr) {
var date = new Date(dateStr);
var dayOfWeek = date.getDay();
var hours = date.getHours();
return dayOfWeek > 0 && dayOfWeek < 6 && hours >= 8 && hours <= 18;
}
function calculateDuration(start, end) {
var startDate = new Date(start);
var endDate = new Date(end);
var diff = endDate - startDate;
// Convert duration from milliseconds to hours
return diff / (1000 * 60 * 60);
}
Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark