- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2016 03:28 AM
Hello All,
Hopefully a quick one - I'm having trouble getting a Client Script to work properly - I have a date field 'ends' (type is 'Date'), which we need to ensure is in the future.
I am using the following Client Script and Script include, but I receive an onChange script error. Any suggestions would be greatly appreciated.
Thanks,
Charles
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
// assuming datefield is a string
var ajax = new GlideAjax('CheckDateAjax');
ajax.addParam('sysparm_name','compareDateToNow');
ajax.addParam('sysparm_datefield', ends);
ajax.getXML(SetValues);
function SetValues(response) {
var answer = response.responseXML.documentElement.getAttribute("answer") * 1;
if(answer > 0){
g_form.showFieldMsg('Please ensure that the Expiry date is in the future!','error');
return false;
}
}
}
Script Include:
var CheckDateAjax = Class.create();
CheckDateAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
compareDateToNow: function () {
var date1 = new GlideDateTime();
var date2 = new GlideDateTime();
date2.setDisplayValue(this.getParameter('sysparm_datefield'));
return date1.compareTo(date2);
}
});
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-14-2016 03:36 AM
No need to use the server one for this, try this in your client script:
g_user_date_format is a global variable that gives you the user's date format
and formatDate and getDateFromFormat are two useful functions for working with dates on the client
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
var startDateNum = getDateFromFormat(newValue, g_user_date_format);
if (startDateNum <= currentDateNum) {
g_form.showFieldMsg('Please ensure that the Expiry date is in the future!', 'error');
return false;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2018 01:33 PM
Do you need this to only work in the Service Portal, or in the platform UI as well?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2018 01:46 PM
I would like to have Service Portal and platform UI both respond to the same script. Now, it works for platform UI but not for Service Portal form.
Thank you,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2018 11:49 AM
Hello timmo,
If I want to make the Service Portal form to respond the script, what is the fix?
I am not concerned the platform UI now. All scripts work perfectly with the platform UI.
Thank you,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-22-2018 02:03 PM
Try this:
if (typeof(moment) === "undefined"){
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
//get start date
var startDateStr = g_form.getValue('end_date');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
//get end date
var endDateStr = g_form.getValue('extension_date');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
} else {
// current date
currentDateNum = moment().valueOf();
//get start date
var startDateNum = moment(g_form.getValue('end_date'));
//get end date
var endDateNum = moment(g_form.getValue('extension_date'));
}
This will work in both. It check if the 'moment' library exists (only in SP), and if it does, use that for the date formatting instead of the platform/native UI methods.
Hope that helps!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-04-2018 01:32 AM
You can also use formatDate and other date validation scripts in the service portal if you include them as a dependency. See the last post from jacebensen here : https://community.servicenow.com/community?id=community_question&sys_id=c7f20ba1dbd8dbc01dcaf3231f961958
I had the same problem but the link above allowed me to continue using the existing validation scripts.