start date should be today + 2 business days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 08:51 PM
Posting this again as I could not get it working:
Hi
Can someone please help me with catalog client script (on change) and script include
I have a date variable in the new hire form.
The requirement is users should only be allowed to select the "start date" as current date + 2 business days (exclude sat-sun).
Example: If I'm filling the form today , (Oct 11) the start date should be Oct 14 or further. User should not be be able to select an ealier date and should give an error " Invalid date"
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-10-2023 10:58 PM
Hi @Thomas99
Give below approach a try...
#Client Script
var ga = new GlideAjax('global.ScheduleScriptUtilAJAX') //Your Script Include AJAX
ga.addParam('sysparm_name', 'isValidDate');
ga.addParam('sysparm_selected_date', newValue);
ga.getXMLAnswer(function(response) {
if (!response) {
g_form.clearValue('start_date'); //Your variable
g_form.showFieldMsg('start_date', 'Invalid Selected Date'); //Your message
}
});
#Script Include
/**
* Validate the selected date
* @return {Boolean}: True/False
*/
isValidDate: function() {
var selected_date = this.getParameter('sysparm_selected_date'); //The selected date
var gdtSelectedDate = new GlideDateTime(selected_date);
var scheduleID = '89570e354771751083058977536d438c'; //Schedule 24x5
var gdtToday = new GlideDateTime();
var schedule = new GlideSchedule(scheduleID);
var duration = '2 00:00:00';
var gd = new GlideDuration(duration);
var min_start_date = schedule.add(gdtToday, gd);
return min_start_date.getDisplayValue() <= gdtSelectedDate;
},
Let me know if it works for you.
Cheers,
Tai Vu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-12-2023 05:20 AM
Hi @Thomas99 ,
Can you please try the below onChange client script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var currentDate = new Date();
var minStartDate = calculateMinStartDate(currentDate, 2);
var startDateField = g_form.getControl('start_date'); // Replace 'start_date' with the actual field name
var selectedStartDate = new Date(startDateField.value);
if (selectedStartDate < minStartDate) {
g_form.addErrorMessage('Invalid date. Start date must be at least two business days from today.');
startDateField.value = ''; // Clear the field
}
function calculateMinStartDate(currentDate, days) {
// Add 'days' business days to the current date, excluding weekends
var minDate = new Date(currentDate);
while (days > 0) {
minDate.setDate(minDate.getDate() + 1);
if (minDate.getDay() !== 0 && minDate.getDay() !== 6) {
days--;
}
}
return minDate;
}
}
Please let me know if this works for you and mark helpful if its worked.
Thanks,
Dipen
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 10:40 AM
This worked too... but I decided to go with script include. I will mark the solution as accepted on Thursday.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-21-2023 08:23 AM - edited 10-21-2023 09:09 AM
Hi Dipen,
This works well, but I want to error to appear on the field and not in the top of the form.
I tried:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2023 06:21 AM
Hi @Thomas99
I tried the following code in my PDI and it is working fine. Please try and let me know your feedback.
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('ValidateDate');
ga.addParam('sysparm_name', 'dateValidation');
ga.addParam('sysparm_date', newValue);
ga.getXML(CallBack);
function CallBack(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'true') {
alert("pls select atleast 48hrs or more from now");
g_form.setValue('lpar_refreshed', '');
}
}
}
Script Include:
var ValidateDate = Class.create();
ValidateDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
dateValidation: function() {
var selected_date = new GlideDateTime(this.getParameter('sysparm_date'));
var today = new GlideDateTime();
var dur = new DurationCalculator();
// loads "8-5 weekdays excluding holidays" schedule
dur.setSchedule('090eecae0a0a0b260077e1dfa71da828');
dur.calcScheduleDuration(today, selected_date);
var secs = dur.getSeconds();
var days = secs / (9 * 60 * 60);
if (days < 2) {
return 'true';
}
return 'false';
},
type: 'ValidateDate'
});
Please mark my answer helpful and accept as solution if it helped you ✔️👍
Anvesh