Populating date on the basis of work days selection
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 03:39 AM - edited 02-14-2024 03:42 AM
Hi Team,
I have 4 checkboxes :
- 3 working days
- 7 working days
- 15 working days
- 30 working days
And there is one date field, If I select either one of checkbox then based on that date should update on date field and it should exclude weekends.
Please help me by providing answer?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 03:44 AM
You can write onchange client script on that choice field
Below template you can use and modify
(function () {
var checkboxes = [
'3_working_days',
'7_working_days',
'15_working_days',
'30_working_days'
];
checkboxes.forEach(function (checkbox) {
g_form.addCheckBoxChangeCallback(checkbox, function () {
updateDateField();
});
});
function updateDateField() {
// Get the selected checkbox value
var selectedCheckbox;
checkboxes.forEach(function (checkbox) {
if (g_form.getValue(checkbox) == 'true') {
selectedCheckbox = checkbox;
}
});
if (selectedCheckbox) {
// Calculate the target date based on the selected checkbox
var targetDate = calculateWorkingDays(new Date(), parseInt(selectedCheckbox));
// Update the target date field
g_form.setValue('target_date_field', targetDate);
}
}
function calculateWorkingDays(startDate, daysToAdd) {
// Function to calculate working days excluding weekends
var currentDate = new Date(startDate);
var addedDays = 0;
while (addedDays < daysToAdd) {
currentDate.setDate(currentDate.getDate() + 1);
// Check if the current day is not a weekend (Saturday or Sunday)
if (currentDate.getDay() !== 0 && currentDate.getDay() !== 6) {
addedDays++;
}
}
return currentDate;
}
})();
Kindly mark helpful/accepted if it helps you.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2024 04:03 AM
Hello @priyanka1028,
You can create a schedule which runs for the working days, and you can use the below script to get the date.
var currentDate = new Date();
var days = (value based on the checkbox) assuming 3;
var hrs = 3*8; //assuming 8 working hours perday;
var duration = new GlideDuration(60*60*1000*hrs) //this takes input as milliseconds so converting number of hrs into mill seconds
var schedule = new GlideSchedule('your schedule sys_id');
var finalDate = schedule.add(currentDate,duration);
current.your_date_field = finalDate;
Please mark this Accepted if this worked.