
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 10:10 PM
Hi Community,
I have 2 variables named-
Required date and expired date. The requirement is that maximum selectable 'expired date' should be 'required date' + 3 months. Can someone help me with this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 11:06 PM
Hi
Try the below...working..tested..
Write OnChange client script on Expired Date
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var getStartDate = g_form.getValue('u_start_date');
var ga = new GlideAjax('SetEndDate');
ga.addParam('sysparm_name', 'ED');
ga.addParam('sysparm_start_date', getStartDate);
ga.getXML(HelloWorldParse);
function HelloWorldParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (newValue > answer) {
g_form.setValue('u_end_date_new', '');
g_form.addErrorMessage('End Date needs to be within 3 months');
}
}
}
Script Include
var SetEndDate = Class.create();
SetEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
ED: function() {
var getNum = this.getParameter('sysparm_start_date');
var gdt = new GlideDateTime();
gdt.setDisplayValue(getNum);
var newDate = gdt.addMonths(3);
return gdt.getDate();
},
type: 'SetEndDate'
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 10:22 PM
Hi,
Read this article, it can help you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 10:29 PM
Hi Sanket,
There is no way to compare dates on client side script. You need to write onChange client script on expired date field and use GlideAjax to get Date Difference.
Below thread will help you with more clarification
Find difference between two dates
In above thread, they sent date difference to client side then you can use code like if difference is more than 90 days then throw error else no error.
Let me know if you have any further queries.
Please mark this as Correct or Helpful if it helps.
Thanks and Regards,
Abhijit
Regards,
Abhijit
ServiceNow MVP

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 10:34 PM
HI,
Below is the script include code:-
var myDateUtil = Class.create();
myDateUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getEndDate:function()
{
var dt1=this.getParameter('sys_parm_issue_dt');
var how_many_months=this.getParameter('sys_parm_how_many_months');
var dt=new GlideDateTime(dt1);
dt.addMonthsLocalTime(parseInt(how_many_months));
return dt.getDate();
},
type: 'myDateUtil'
});
Below is the onChange client script date field:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if(newValue === '')
{
g_form.clearValue('end_date');
}
var ga = new GlideAjax('myDateUtil');
ga.addParam('sysparm_name','getEndDate');
ga.addParam('sys_parm_issue_dt',newValue);
ga.addParam('sys_parm_how_many_months',3);
ga.getXML(cb);
// the callback function for returning the result from the server-side code
function cb(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer)
g_form.setValue('end_date',answer);
}
//Type appropriate comment here, and begin script below
}
Please mark this as Correct or Helpful if it helps.
Thanks
RS
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2022 10:42 PM
Hi
You can achieve this using before update BR also and set conditions as per your requirement. Below is script:
You can replace field names with your own field names.
(function executeRule(current, previous /*null when async*/ ) {
var end_date = current.u_end_date;
var start_date = current.u_start_date;
var gd = new GlideDateTime(start_date);
gd.addMonthsLocalTime(3);
var date = gd.getDate();
if (date != end_date) {
gs.addErrorMessage('Should be +3 minths to start Date');
current.setAbortAction(true);
}
Thanks,
Lovely.