- 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-23-2020 09:33 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 09:52 AM
Hi,
you can use onchange client script and GlideAjax for this
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('request_completion_date'));
ga.getXMLAnswer(function(answer){
if(answer == 'true'){
alert('Please select within 5 business days');
g_form.clearValue('request_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);
// paste the sys_id of the 8*5 weekday schedule excluding holidays and weekends
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
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 ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
03-23-2020 11:18 AM
This seems to almost be working. I used it and it's throwing the alert no matter what I select for a date.
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*5*1000);
// paste the sys_id of the 8*8 weekday schedule excluding holidays and weekends
var schedule = new GlideSchedule('2218ff1bdba8eb40fb6e753a8c96198d');
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'
});
It needs to be any day AFTER 5 business days so I adjusted the var dur line to reflect 5 days not 24 and I added my schedule sys_id.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-23-2020 07:55 PM
Hi,
just a change in the alert from my side:
alert('Please select date after 5 business days');
Can you share the updated code?
the code should work ideally. Just try to add some logs
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
Hey booher04,
Try this Code:
1) Onload Script:
function onLoad() {
//Type appropriate comment here, and begin script below
if(g_form.getValue('your date field name') == '')
{
var da = new GlideAjax('ClientDateTimeUtils'); // script include name
da.addParam('sysparm_name','getDefaultEndDate');// Function name
da.getXMLWait();
var end_date = da.getAnswer();
if(end_date != '')
{
g_form.setValue('your date field name'',end_date);
}
}
}
2) Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDefaultEndDate: function() {
var startDate = gs.nowDateTime();
var addQuantity = this.getParameter('sysparm_quant');
var end_date = new GlideDateTime();
end_date.setDisplayValue(startDate);
end_date.addSeconds(259200);// Equivalent to 3 days
end_date = end_date.getDisplayValue();
return end_date;
},
});
3) Onchange Script: This should be on change of your date field
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var da = new GlideAjax('ClientDateTimeUtils');
da.addParam('sysparm_name','getDefaultEndDate');
da.getXMLWait();
var end_date = da.getAnswer();
if(g_form.getValue('Your Date field name') < end_date)
{
alert('Date should be After 3 days from current date time');
g_form.clearValue('Your Date field name');
return;
}
}
Make a changes in number of days as per your requirement.
Mark Answer as Correct or helpful if it works for you.
Thanks,
Namrata