- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-18-2022 11:46 PM
Hi Community,
How to make end date auto populate to a year after based on start date?
For example, when I select 19-May-2022 in start date, the end date should auto populate to 19-May-2023.
How can I achieve this ? Kindly, please help.
Start and End Date is using Date type. not Date/Time.
Script Include:
var StartEndDate = Class.create();
CIMBStartEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDate: function() {
var date = this.getParameter('sysparm_date');
var year = 1;
var gdt = new GlideDate(date);
gdt.addYearsUTC(year);
return gdt.getNowDate();
},
type: 'StartEndDate'
});
Client Script:
OnChange Start Date
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('StartEndDate');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', g_form.getDisplayValue('start_date')); // give here date variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setDisplayValue('end_date', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below
}
I've tried this code but it's not working.
Solved! Go to Solution.
- Labels:
-
Service Portfolio Management
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 02:17 AM
try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
if(oldValue != newValue){
var dateMS = getDateFromFormat(newValue, g_user_date_format);
dateMS += 365 * 24 * 60 * 60 * 1000; // 365 days means 1 year
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('end_date', formatDate(newDT, g_user_date_format));
}
//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
05-19-2022 03:30 AM
Hi
There were a few rectifications in your scripts. Please check the updated scripts below:
Client Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('StartEndDate');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', newValue); // give here date variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
//alert(answer);
g_form.setValue('u_end_date', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below
}
Client Script Screenshot:
Script Include:
var StartEndDate = Class.create();
StartEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDate: function() {
var date = this.getParameter('sysparm_date') + ' 00:00:00';
gs.info('[DEBUG] Date recieved: ' + date);
var year = 1;
var gdt = new GlideDateTime(date);
gdt.addYearsUTC(year);
gs.info('[DEBUG] Returning Date: ' + gdt);
return gdt.getDate();
},
type: 'StartEndDate'
});
Script Include Screenshot:
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 08:06 AM
Hi
Are you still facing any issues?
If not then please mark my answer as correct/helpful so that others may also get the solution for a similar issue.
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 11:36 PM
Hi
Are you still facing issues with your requirement?
If it is solved then I urge you to rate my answer so that other community users facing similar issues might benefit from this!!
Please mark my answer as correct if this solves your issues!
If it helped you in any way then please mark helpful!
Thanks and regards,
Kartik
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 12:53 AM
Hi Nur
Verify if you are using proper field names or not.Also share the code you tried?
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 01:00 AM
Start and End Date is using Date type. not Date/Time.
Script Include:
var StartEndDate = Class.create();
CIMBStartEndDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
calculateDate: function() {
var date = this.getParameter('sysparm_date');
var year = 1;
var gdt = new GlideDate(date);
gdt.addYearsUTC(year);
return gdt.getNowDate();
},
type: 'StartEndDate'
});
Client Script:
OnChange Start Date
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var ga = new GlideAjax('StartEndDate');
ga.addParam('sysparm_name', "calculateDate");
ga.addParam('sysparm_date', g_form.getDisplayValue('start_date')); // give here date variable name
ga.getXMLAnswer(function(answer){
if(answer != ''){
g_form.setDisplayValue('end_date', answer); // give proper variable name here
}
});
//Type appropriate comment here, and begin script below
}
I've tried this code but it's not working.