- 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-19-2018 08:57 AM
FormatDate is not working with Jakarta Service Portal. Any idea how to fix it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 10:22 AM
Just to confirm; are you using 'FormatDate' or 'formatDate'? Did you check the browser console for any errors?
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 12:32 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-19-2018 07:17 PM
Those methods aren't available in the Mobile API. You'll need to use another method to achieve the same result. The 'moment.js' library is available in Service Portal, and handles date formatting nicely.
Can you post the exact code you're using?
You can replace this:
//current date
var currentDateObj = new Date();
var currentDateStr = formatDate(currentDateObj, g_user_date_format);
var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
With something like this:
var currentDateNum = moment().valueOf();
Tim
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-20-2018 08:51 AM
There are 3 pairs of dates for me to compare each start date and end date. There are similarities to build upon my code below but adjusted Field name on their respective onChange Client Script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
if (!isLoading) {
if(newValue != '') {
//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('u_project_start');
var startDateNum = getDateFromFormat(startDateStr, g_user_date_format);
//get end date
var endDateStr = g_form.getValue('u_project_complete');
var endDateNum = getDateFromFormat(endDateStr, g_user_date_format);
var diff = endDateNum - startDateNum;
var maxDiff = 30*24*60*60*1000; //30 days * 24 hrs * 60 mins * 60 secs * 1000 ms
if (endDateNum <= 0){
alert('Please use the calendar icon to select a date.');
g_form.setValue('u_project_complete', '');
} else if (endDateNum < currentDateNum) {
alert('You cannot select a date in the past.');
g_form.setValue('u_project_complete', '');
} else if (endDateNum < startDateNum) {
alert('You cannot select an end date prior to the start date.');
g_form.setValue('u_project_complete', '');
}
}
}
}
Thank you,