- 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 04:10 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 02:02 AM
you are using Server side code in client side and hence it's not working
so end date should be exact 3 months from start date?
this should work, I assume both are date type variables
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(selected_date.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 04:17 AM
Hi @Ankur Bawiskar ,
Yes, both are date fields . I tried your code , but its not giving me any date.
BR
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-09-2025 04:43 AM
I have again tried your code now i am getting this error
onChange script error: ReferenceError: selected_date is not defined function () { [native code
- 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