Populate End Date based on Start Date
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2022 05:07 AM
Hello Guys,
I need to populate end date based on start date. on the form when we already have start date displayed. but when state changes I want end date to be auto populated with start date + 10days.
Below is the Before BR rule, I am using but no luck.
var sDate = new GlideDateTime(current.start_date);
sDate.addDays(10);
current.end_date= sDate;
Any help is appreciated.
Thanks,
Rocky.
- Labels:
-
Script Debugger
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2022 09:27 AM
Hi Rocky,
Have you tried it?
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-09-2022 09:42 AM
Hi Rocky,
Please check with below script in your onChange Client script on start_date:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var dateValue = g_form.getValue("start_date");
if (dateValue) {
var startDate = new Date(dateValue);
var startDateStr = formatDate(startDate, g_user_date_format);
var newDateNumericValue = getDateFromFormat(startDateStr, g_user_date_format);
var endDateNumericValue = newDateNumericValue + (10 * 86400000);
var endDate = new Date(endDateNumericValue);
var endDateStr = formatDate(endDate, g_user_date_format);
g_form.setValue("end_date", endDateStr);
}
}
If you want to do it in BR then use below script:
var startDate = new GlideDateTime(current.start_date);
startDate.addDays(10);
var dateValue = startDate.getDate();
var dateNumericValue = dateValue.getNumericValue();
current.end_date.setDateNumericValue(dateNumericValue);
Please mark my respsone as helpful/correct, if it answer your question.
Thanks