- 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-18-2022 11:50 PM
Did you try something? Where are you stuck? I believe there are many community threads over this.
If you have not yet started check for link.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 12:43 AM
Hi,
I've followed this thread, but the end date is still null.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2022 12:33 AM
Hi
It seems you need to achieve this via Client Script. You can follow the community thread to accomplish your requirement.
OR
You can opt for Calculated Field in case you have the fields on the table (not the variables). For this you can follow the below-provided approach:
- Go Dictionary for End Date, change view to Advanced View.
- Go to the Calculated Value Tab:
- Write the below-provided script:
(function calculatedFieldValue(current) { var dateTime = new GlideDateTime(); dateTime.setValue(current.u_start_date + ' 00:00:00'); dateTime.addYearsLocalTime(1); return dateTime.getDate(); })(current);
This will ensure the End Field is automatically populated (on save/submit/update) based on the value in Start Date.
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:50 AM
Hi Kartik,
I need to make the end date auto populate to next one year ONCE I've entered the start date.
How can I achieve this ?
I've tried this link. But the end date still null.
How to achieve this ? Thanks