- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 12:06 PM
Hi All,
I found below details from one of the thread to restrict past date selection in catalog item however the same is not working in Normal table form, please suggest how to achieve the same in normal table forms (reference- https://community.servicenow.com/thread/159148😞
____________________________________________________________________________
OnChange Client Script : field name : date_time
Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading){ return; }
if(newValue != ''){
var ga = new GlideAjax('CheckDate');
ga.addParam('sysparm_name', 'chkCatDate');
ga.addParam('sysparm_date',g_form.getValue('date_time'));
ga.getXML(DatParse);
}
function DatParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date cannot exist in past.");
g_form.setValue('date_time', ''); // Empty the variable.
}}}
___________________________________________________________________________
Now write a script include as follows :-
Name : CheckDate
Client callable : true
Script :
var CheckDate = Class.create();
CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCatDate : function() {
var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 12:36 PM
Hello
Try this and let me know if i can help you with anything.
Script Include:
Name:DateValidation
Client callable: Checked
var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate: function() {
var ActualEndDate = this.getParameter('sysparm_end_date');
return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;
},
type: 'DateValidation'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('DateValidation');
ga.addParam('sysparm_name','validateDate');
ga.addParam('sysparm_end_date',g_form.getValue('u_business_need_by_date'));//give your date field
ga.getXML(ProcessResult);
function ProcessResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 0)
{
g_form.clearValue('u_business_need_by_date'); //give your date field
alert('Business need date should not be in the Past.');
}
}
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 01:10 PM
Hi Harel,
I tried as you suggested but its not working:
Client Script: (field name is u_friday_review_date)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax("NowDateCalc");
ga.addParam("sysparm_name", "getNow");
ga.getXML(getDate);
}
function getDate(response) {
var my_date = g_form.getValue('u_friday_review_date');
var rightNow = response.responseXML.documentElement.getAttribute("answer");
if (my_date < rightNow) {
alert("Please select future Friday Review Date.");
g_form.setValue('date_needed', '');
}
}
________________________________________________________________________________________
Script Include:
var RestrictingPastDate = Class.create();
RestrictingPastDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNow : function() {
return gs.now();
},
type: 'RestrictingPastDate'
});
please let me know if I am missing anything in above scripts.
fyi: we are on Jakarta
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 12:36 PM
Hello
Try this and let me know if i can help you with anything.
Script Include:
Name:DateValidation
Client callable: Checked
var DateValidation = Class.create();
DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateDate: function() {
var ActualEndDate = this.getParameter('sysparm_end_date');
return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;
},
type: 'DateValidation'
});
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('DateValidation');
ga.addParam('sysparm_name','validateDate');
ga.addParam('sysparm_end_date',g_form.getValue('u_business_need_by_date'));//give your date field
ga.getXML(ProcessResult);
function ProcessResult(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 0)
{
g_form.clearValue('u_business_need_by_date'); //give your date field
alert('Business need date should not be in the Past.');
}
}
}
Please mark my response as correct and helpful if it helped solved your question.
-Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-13-2017 01:39 PM
Thank You Prateek !!! it worked.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2020 12:59 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-27-2020 01:57 PM
gs.now() -- Gives you current date
ActualEndDate - is the value end user has picked
gs.dateDiff(gs.now(),ActualEndDate, true)-- will print you the difference between these two dates in seconds
If you divide these seconds with 86400, you'll get the result in days.(24hrs*60mins*60Seconds)
Please mark my response as correct and helpful if it helped solved your question.
-Thanks