- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:09 AM
Hi Everyone,
I am working on population of date field in one variable based on selection of other date variable.
I have start date variable that is date format filed, and other filed is end date.
When i select a date in start date the end date field should be populated with date 3 months prior.
I tried this using client script butting getting an error. Here is my code
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 04:46 AM
updated now
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
if (newValue === '')
g_form.clearValue('end_date');
if (newValue != '') {
var startDt = new Date(getDateFromFormat(newValue, g_user_date_format));
startDt.setDate(startDt.getDate() + 90);
g_form.setValue('end_date', startDt.toISOString().slice(0, 10));
}
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:19 AM
Hi @Atchutaram
Sample code:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var selected_date = new Date(getDateFromFormat(newValue, g_user_date_time_format));
selected_date.setDate(selected_date.getDate() + 90);//add 90 days to the selected date.
var selected_dateStr = formatDate(selected_date, g_user_date_time_format);
g_form.setValue('u_expected_end', selected_dateStr); // change field name as required.
}
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
06-09-2025 01:33 AM
Does this work on change of my start date, if yes where are we getting that variable?
BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 01:49 AM
I have tried the code but it not working its giving me some random date not 3 months prior date.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 02:06 AM
Try this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var selected_date = new Date(getDateFromFormat(newValue, g_user_date_time_format));
selected_date.setDate(selected_date.getDate() - 90); //add 90 days prior to the selected date.
g_form.addInfoMessage(selected_date);
var selected_dateStr = formatDate(selected_date, g_user_date_time_format);
g_form.setValue('u_expected_end', selected_dateStr);
}
I just tried in my pdi and its setting 90 days prior to the expected start value in expected end field.
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