fill a date on a form based in other field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2025 02:09 AM
I have two fields: Start Date and Initial Date, both of type Date (not Date/Time).
When the user selects the Group "PANP", I need the Initial Date to be automatically set to 1 month and 7 days after the selected Start Date.
I’ve already tried using a Script Include and a Client Script (onChange), but either it doesn’t work, or it behaves incorrectly — for example, if the Start Date is 12/03/2025, it reads it as December 3rd instead of March 12th.
We use the day/month/year format.
Has anyone done something similar or knows how to make this work properly?
Thanks in advance,
Marisa
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2025 02:20 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2025 03:14 AM
Hi @MARISA INACIO1,
To achieve your requirement where the Initial Date is automatically set to 1 month and 7 days after the Start Date when the Assignment Group is PANP, the most reliable and maintainable solution is to make Initial Date a calculated field.
- Navigate to the Dictionary Entry for the field Initial Date (u_initial_date) in the Requested Item (sc_req_item) table.
- Under the Calculated Value tab:
- Calculated: Checked (true)
- Calculation type: Script
- Calculation script:
(function calculatedFieldValue(current) {
if(current.assignment_group.name == 'PANP'){
var startDate = new GlideDate(current.u_start_date);
startDate.addMonths(1);
startDate.addDays(7);
return startDate;
}
return current.u_initial_date;
})(current);
Important Notes:
- Ensure your Start Date (u_start_date) is of type Date, not Date/Time.
- The calculation will only apply when the record is created or updated server-side.
- GlideDate handles the date formatting internally, so you don’t need to worry about the day/month/year interpretation issue.
Thanks & Regards,
Muhammad Iftikhar
If my response helped, please mark it as the accepted solution so others can benefit as well.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-03-2025 03:24 AM
it's possibly because of the date format.
share your client script and script include here.
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
10-03-2025 05:19 PM
If you want Initial Date to auto populate as one month and seven days after Start Date (only when the Group is PANP), a reliable approach is to use an onChange client script on the Start Date field (or Group) that, when the condition is met, reads the Start Date using getDateFromFormat(...), adds one month and seven days using JavaScript Date methods (or via a server GlideDate/GlideDateTime through GlideAjax if you prefer server logic), then formats the result back to the user date format and sets it into Initial Date, and if the condition is not met clear the field....
if (g_form.getValue('group') === 'PANP' && newValue) {
var startDate = new Date(getDateFromFormat(newValue, g_user_date_format));
if (isNaN(startDate.getTime())) {
return;
}
var month = startDate.getMonth();
startDate.setMonth(month + 1);
startDate.setDate(startDate.getDate() + 7);
var initialDateStr = formatDate(startDate, g_user_date_format);
g_form.setValue('initial_date', initialDateStr);
} else {
g_form.setValue('initial_date', '');
If you found my response helpful, please mark it as ‘Accept as Solution’ and ‘Helpful’. This helps other community members find the right answer more easily and supports the community.
Kaushal Kumar Jha - ServiceNow Consultant - Lets connect on Linkedin: https://www.linkedin.com/in/kaushalkrjha/
