- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 02:27 PM
Hi Everyone,
I have a requirement on a knowledge article in servicenow, I have a field called "valid to" and a custom field called valid for. The "valid to" field is a date field and the valid for field is a choice field with 4 options. These are "1 Month", "3 Months", "6 Months", "12 Months". I require the following functionality, If Valid for 1 month is selected it sets the date in the Valid to field to a month from today's date automatically. When 3 months option is selected set the valid to date to 3 months from today's date automatically. When 6 months option is selected it sets the valid to date to 6 months from today's date automatically and when 12 Months option is selected it sets the valid to fields date to 12 months from today's date automatically. Can anyone help me with a script for this? or has one that would make this work?
Kind Regards,
Luke
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 10:18 PM
Hi @Luke James ,
You can write an On Change client script and Script include
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
g_form.clearValue('valid_to');
var ga = new GlideAjax('CommunityTesting'); // Name of the Script Include
ga.addParam('sysparm_name', 'getDate');
ga.addParam('sysparm_date', g_form.getValue('u_valid_time')); // Valid time field with 3 months, 9 months
ga.getXML(answer);
}
function answer(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
// g_form.addInfoMessage(answer);
g_form.setValue('valid_to',answer);
}
Script Include:
var CommunityTesting = Class.create();
CommunityTesting.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDate: function() {
var valid_date = new GlideDate();
var selected_month = this.getParameter('sysparm_date');
if(selected_month == 3){
valid_date.addMonths(3);
}
if(selected_month == 9 ){
valid_date.addMonths(9);
}
return valid_date;
},
type: 'CommunityTesting'
});
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 09:52 PM
Hello,
You can use something like
addMonthsLocalTime() or addMonthsUTC() to your current "Valid to" field to get required value based on your selection in "Valid for" , just use combination of onCHange and script include.
Sample example :
var gdt = new GlideDateTime("2011-08-31 08:00:00");
gdt.addMonths(2);
gs.print(gdt.getDate());
Other useful links :
Example links where similar thing attempted
I hope this helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2025 10:18 PM
Hi @Luke James ,
You can write an On Change client script and Script include
Client script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Type appropriate comment here, and begin script below
g_form.clearValue('valid_to');
var ga = new GlideAjax('CommunityTesting'); // Name of the Script Include
ga.addParam('sysparm_name', 'getDate');
ga.addParam('sysparm_date', g_form.getValue('u_valid_time')); // Valid time field with 3 months, 9 months
ga.getXML(answer);
}
function answer(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
// g_form.addInfoMessage(answer);
g_form.setValue('valid_to',answer);
}
Script Include:
var CommunityTesting = Class.create();
CommunityTesting.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getDate: function() {
var valid_date = new GlideDate();
var selected_month = this.getParameter('sysparm_date');
if(selected_month == 3){
valid_date.addMonths(3);
}
if(selected_month == 9 ){
valid_date.addMonths(9);
}
return valid_date;
},
type: 'CommunityTesting'
});
If the above information helps you, Kindly mark it as Helpful and Accept the solution.
Regards,
Najmuddin.