- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-23-2020 09:13 AM
We have a need to require a requested_completion_date to be at least 5 business days, if you select a date less than 5 business days you get an error and it clears the field. My question is do I have to write a script include with this or can I write an onChange client script to accomplish this? The schedule we use is '8-8 weekdays excluding holidays'.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2020 08:53 AM
Resolved:
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_date', g_form.getValue('requested_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select date after 5 business days');
g_form.clearValue('requested_completion_date');
}
});
}
Script Include:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var dateSelected = this.getParameter('sysparm_date');
var nowDateTime = new GlideDateTime();
var days = 5;
//var dur = new GlideDuration(60*60*24*days*1000);
var dur = new GlideDuration(days*43200*1000);
// paste the sys_id of the 8*8 weekday schedule excluding holidays
var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d'); //8-8 weekdays excluding holidays
var finalTime = schedule.add(nowDateTime, dur,'');
var updatedGdt = new GlideDateTime(dateSelected);
var finalTimeGdt = new GlideDateTime(finalTime);
if(updatedGdt < finalTimeGdt){
return 'true';
}
return 'false';
},
type: 'u_userInfoAjax'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-25-2020 09:55 AM
Added that script and script include... now it does nothing. I dont get the alert at all.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2020 08:53 AM
Resolved:
onChange Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// Call the GA function
var ga = new GlideAjax('u_userInfoAjax');
ga.addParam('sysparm_name', "getInfo");
ga.addParam('sysparm_date', g_form.getValue('requested_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select date after 5 business days');
g_form.clearValue('requested_completion_date');
}
});
}
Script Include:
var u_userInfoAjax = Class.create();
u_userInfoAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getInfo: function(){
var dateSelected = this.getParameter('sysparm_date');
var nowDateTime = new GlideDateTime();
var days = 5;
//var dur = new GlideDuration(60*60*24*days*1000);
var dur = new GlideDuration(days*43200*1000);
// paste the sys_id of the 8*8 weekday schedule excluding holidays
var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d'); //8-8 weekdays excluding holidays
var finalTime = schedule.add(nowDateTime, dur,'');
var updatedGdt = new GlideDateTime(dateSelected);
var finalTimeGdt = new GlideDateTime(finalTime);
if(updatedGdt < finalTimeGdt){
return 'true';
}
return 'false';
},
type: 'u_userInfoAjax'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-27-2020 11:28 PM
Hi,
I think you have taken 8-8 means 12 hours and hence
60*60*12 = 43200
Did you accidentally mark your own answer as correct as compared to the one which helped in scripting
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2022 07:15 AM
Hi Ankur,
I tried the proposed solution, But script include always return true only.
I observe date formate difference.
Could you please help me how to resolve this issue.
getInfo: function(){
var dateSelected = this.getParameter('sysparm_date');
var nowDateTime = new GlideDateTime();
var days = 5;
var dur = new GlideDuration(60*60*24*days*1000);
// paste the sys_id of the 8*5 weekday schedule excluding holidays and weekends 090eecae0a0a0b260077e1dfa71da828
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
var finalTim = schedule.add(nowDateTime, dur,'');
gs.info("check dates"+"---"+date2);
var updatedGdt = new GlideDateTime(dateSelected);
var finalTimeGdt = new GlideDateTime(finalTim);
gs.info("check dates"+'---==='+updatedGdt+'---'+dateSelected+"---=="+finalTimeGdt+"----"+finalTim(2));
if(dateSelected < finalTimeGdt){
return 'true';
}
return 'false';
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-26-2022 11:41 PM
Hi Ankur,
Colud you please help me how to convert the date formates.
I have similar requirement, I used same code but slected date and current date has different date formate.