- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2021 10:27 PM
How to add one year to a date field using client script(on change) + script include
There are two fields
Start date and End date
whenever I will select Start date I want to put one year to the End date by using on change client script
Solved! Go to Solution.
- Labels:
-
Incident Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2021 10:29 PM
Hi,
Sample script below using GlideAjax
Script Include: It should be client callable
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDate: function(){
var date = this.getParameter('sysparm_date');
var year = 1;
var gdt = new GlideDateTime(date);
gdt.addYearsUTC(year);
return gdt.getDate();
},
type: 'checkRecords'
});
onChange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', g_form.getValue('start_date')); // give here date variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('end_date', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2021 10:29 PM
Hi,
Sample script below using GlideAjax
Script Include: It should be client callable
var checkRecords = Class.create();
checkRecords.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDate: function(){
var date = this.getParameter('sysparm_date');
var year = 1;
var gdt = new GlideDateTime(date);
gdt.addYearsUTC(year);
return gdt.getDate();
},
type: 'checkRecords'
});
onChange client script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('checkRecords');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', g_form.getValue('start_date')); // give here date variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setValue('end_date', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2021 11:53 PM
Glad to help.
Please mark response helpful as well.
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-02-2021 10:30 PM
Hi,
it can be done without Script Include as well i.e. directly using client script
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-18-2022 11:54 PM
Hi Ankur,
How to do this without Script Include ?
What is the code if directly using client script?
Thanks