- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2014 07:25 AM
Hi,
We have a variable in a catalog item called oos_end which is a date/time field.
We need to stop people setting this field to a date in the past - apparently common sense isn't the answer. nor is sacking the culprits - so I'm looking at a Catalog Client Script - type "on Change" - running on that catalog item and variable
I can't use gs.nowDateTime() so can anyone suggest an alternative please? I've had a trawl through other client scripts we have but they seem to compare the values in two existing fields/variables rather than "now".
thanks in advance for any pointers
(Current release: Berlin)
---------------------
function onChange(control, oldValue, newValue, isLoading) {
if(isLoading){
return;
}
var now = ?????? ;
if(newValue < now){
alert("Polite message about setting dates in the past etc");
g_form.setValue("variables.oos_end",'');
}
}
Solved! Go to Solution.
- Labels:
-
Service Mapping

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-17-2014 12:16 PM
It sounds as if you want to check the date the person selected in the variable and warn the user if they selected a date in the past. Using the suggestion by Lam_Hoang, we can perform a date/time check on the selected value and return a true/false value that we can throw an alert with.
Step 01. Create a script include that you can call from your Catalog Client Script:
Name: dateTimeUtilAjax
Client callable: true
Script:
var dateTimeUtilAjax = Class.create();
dateTimeUtilAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkNowDateTime: function() {
var firstDT = this.getParameter('sysparm_fdt');
var nowDT = gs.nowDateTime();
var diff = gs.dateDiff(firstDT, nowDT, true);
if (diff > 0) {
return false;
} else {
return true;
}
},
getNowDateTime: function() {
return gs.nowDateTime();
}
});
Step 02. Create your Catalog Client Script:
Name: Warn if oos_end is before now
Applies to: A Catalog Item
Type: onChange
Catalog item: ***WHATEVER YOUR ITEM IS***
Variable name: oos_end
Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
if (newValue != oldValue) {
var startDate = newValue;
var ga = new GlideAjax('dateTimeUtilAjax');
ga.addParam('sysparm_name', 'checkNowDateTime');
ga.addParam('sysparm_fdt', startDate);
ga.getXML(checkDate);
}
}
function checkDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
alert("You must select a date in the future");
g_form.setValue('oos_end', '');
}
}
Let me know if you are still having difficulty.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-18-2014 04:48 AM
I am using a similar script.
I have a variable by the name rental_start_date. The rental start should contains values only after one week from now.
CheckStartDate: function() {
var start = this.getParameter('sysparm_date');
var DayOneWeek = gs.daysAgo(-7);
gs.log(DayOneWeek , "rental");
if(start < DayOneWeek){
return false;
}else{
return true;
}
},
This is my script include. 'start' variable has the value o selected date and DayOneWeek has the value of day after one week.
the gs log statement gives a proper date but while selecting the date it allows me to select dates less than one week.
any idea how to tackle this ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2015 10:50 AM
I know this question has been answered, and quite competently at that.
I like the solution with the Ajax call to the script include, I use a similar call in many cases myself.
That said, I wanted to post yet another alternative to consider: a "before" business rule to check the date and abort the transaction if the date appears to be in the past.
There's an example of how to abort an update at Scripting in Business Rules - ServiceNow Wiki
Basically it's this line:
The benefits of a business rule is that it is more efficient than the ajax call, the business rule is "honored" by visual task boards, whereas client scripts are not ( as far as I can determine ) and you can combine multiple checks in one business rule.
Did I mention that you can use gs.nowDateTime() in a business rule?
Of course, the feedback that the date is in the past, only occurs when the form is submitted, saved or updated. So if you want instantaneous feedback to your users when they set the incorrect date, the client script is the way to go.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-04-2023 05:58 PM
or maybe you can use JS function like below:
var mynow = new Date();