- 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 01:49 AM
Can you try using getValue instead of getDisplayValue for getting the start_date field value from the form? Also use setValue at the end for updating the field.
- 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 08:37 PM
Hope you are doing good.
Did my reply answer your question?
If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.
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-25-2022 01:33 AM
Hope you are doing good.
Did my reply answer your question?
If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.
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 04:11 AM
Hi
If my response is helpful, then Please mark as Correct Answer/Helpful.
Please check and let us know.
Thanks š
Shakeel Shaik.
Shakeel Shaik š