- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:25 PM
Hi All,
I have a date variable 'abc'. I have another 2 variables 'policy' which refers to a custom table xyz and the multiple choice variable 'related to'. The xyz table has integer field 'expiration' and a string field 'name.
The requirement is, if the user select a policy for that policy variable and the value of that multiple choice variable 'related to' is neither then the value of that date variable 'abc' should auto set to current date + the value of the integer field 'expiration' of the selected policy and the date variable 'abc' will be read only but user should not be able to select any date greater than the current date + the value of the integer field 'expiration' of the selected policy.
I have written a script include and on change client script on policy variable.
Script Include:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var exemptionValue = g_form.getValue('related_to'); // Correct field name
alert("Exemption Value: " + exemptionValue);
if (exemptionValue === 'neither') {
var ga = new GlideAjax('ExpirationDate');
ga.addParam('sysparm_name', 'CheckExpirationDate');
ga.addParam('sysparm_sys_id', newValue);
alert("Policy Sys ID: " + newValue);
ga.getXML(callback);
}
function callback(response) {
if (response.responseXML) {
var expirationValue = response.responseXML.documentElement.getAttribute('answer');
alert("Expiration Value: " + expirationValue);
if (expirationValue) {
expirationValue = parseInt(expirationValue, 10);
var gdt = new GlideDateTime();
gdt.addDays(expirationValue);
var formattedDate = gdt.getDate().toISOString().split('T')[0];
g_form.setReadOnly('abc', false);
g_form.setValue('abc', formattedDate);
alert("Formatted Expiration Date: " + formattedDate);
} else {
g_form.setReadOnly('abc', false);
alert("No expiration value returned.");
}
} else {
alert("Error: The response is empty or invalid.");
}
}
}
Script include is working as getting all the logs.
In client script getting alert("Expiration Value: " + expirationValue); but the date variable is not getting auto set to the current date + the value of the integer field 'expiration' of the selected policy.
Can you please help me out. It is urgent.
Thanks&Regards,
Abhisek Chattaraj.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:49 PM
refer below thread for reference: https://www.servicenow.com/community/itsm-forum/on-change-script-having-problems-setting-a-date-time...
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:52 PM
something like this
Script Include:
var ExpirationDate = Class.create();
ExpirationDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
CheckExpirationDate: function() {
var policy = this.getParameter('sysparm_sys_id');
var expiration = '';
if (policy) {
var policyGR = new GlideRecord('xyz');
if (policyGR.get(policy)) {
expiration = policyGR.u_expiration;
var gdt = new GlideDateTime();
gdt.addDaysUTC(expiration);
expiration = gdt.getDate();
gs.log("expiration value is:" + expiration);
} else {
gs.log("no record found for: " + policy);
}
} else {
gs.log("No policy Sys ID provided.");
}
return expiration || '';
},
type: 'ExpirationDate'
});
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var exemptionValue = g_form.getValue('related_to'); // Correct field name
alert("Exemption Value: " + exemptionValue);
if (exemptionValue === 'neither') {
var ga = new GlideAjax('ExpirationDate');
ga.addParam('sysparm_name', 'CheckExpirationDate');
ga.addParam('sysparm_sys_id', newValue);
alert("Policy Sys ID: " + newValue);
ga.getXML(callback);
}
function callback(response) {
var expirationValue = response.responseXML.documentElement.getAttribute('answer');
if (expirationValue) {
alert("Expiration Value: " + expirationValue);
g_form.setReadOnly('abc', false);
g_form.setValue('abc', expirationValue);
alert("Formatted Expiration Date: " + expirationValue);
} else {
g_form.setReadOnly('abc', false);
alert("No expiration value returned.");
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:47 PM
Hello @abhisek
You are using GlideDateTime() in client side where as it is server side API.
In your script include, you are returning just number of day, instead have GlideDateTime in script include only and add number of days there then return Date string value from script include to client script. That should work.
Thank you,
Ali
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:48 PM
you are using GlideDateTime in client side which won't work
why not add days in server side and return the updated value and simply set it in client script?
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:49 PM
refer below thread for reference: https://www.servicenow.com/community/itsm-forum/on-change-script-having-problems-setting-a-date-time...
Thank you,
Ali
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-02-2025 10:52 PM
something like this
Script Include:
var ExpirationDate = Class.create();
ExpirationDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
CheckExpirationDate: function() {
var policy = this.getParameter('sysparm_sys_id');
var expiration = '';
if (policy) {
var policyGR = new GlideRecord('xyz');
if (policyGR.get(policy)) {
expiration = policyGR.u_expiration;
var gdt = new GlideDateTime();
gdt.addDaysUTC(expiration);
expiration = gdt.getDate();
gs.log("expiration value is:" + expiration);
} else {
gs.log("no record found for: " + policy);
}
} else {
gs.log("No policy Sys ID provided.");
}
return expiration || '';
},
type: 'ExpirationDate'
});
Client script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var exemptionValue = g_form.getValue('related_to'); // Correct field name
alert("Exemption Value: " + exemptionValue);
if (exemptionValue === 'neither') {
var ga = new GlideAjax('ExpirationDate');
ga.addParam('sysparm_name', 'CheckExpirationDate');
ga.addParam('sysparm_sys_id', newValue);
alert("Policy Sys ID: " + newValue);
ga.getXML(callback);
}
function callback(response) {
var expirationValue = response.responseXML.documentElement.getAttribute('answer');
if (expirationValue) {
alert("Expiration Value: " + expirationValue);
g_form.setReadOnly('abc', false);
g_form.setValue('abc', expirationValue);
alert("Formatted Expiration Date: " + expirationValue);
} else {
g_form.setReadOnly('abc', false);
alert("No expiration value returned.");
}
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader