- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 02:56 AM - edited 01-22-2025 03:03 AM
Hi All,
Requirement:
Populate a date field in a table with the current date when the XYZ field has a specific value. Additionally, users should be able to modify the date field if needed.
To achieve this, I have created a UI Policy with the condition that the XYZ field matches the specified value.
In the UI Policy Script field, I added the following script:
Question:
Is this the correct approach?
Currently, the field is being populated, but not in the desired format. it is populated as below screenshot:
Expected Outcome:
The field should be populated with the current date in the format dd/mm/yyyy hr:mm:ss (e.g., 22/01/2025 16:47:56).
Can someone help me achieve this?
Thanks!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2025 04:04 PM
Hi All,
I used the client script with the following codes and it worked the way I wanted. Though of sharing it as it might be helpful for others.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 03:02 AM
you can use onChange on that field and set current date as per logged in user's format
Is it a date field or date/time field?
use g_user_date_time_format in below script if it's date/time
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading) {
return;
}
g_form.clearValue('XYZ');
if (newValue == "1") {
var today_date = new Date();
var today_date_str = formatDate(today_date, g_user_date_format);
g_form.setValue('XYZ', today_date_str);
}
}
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
01-22-2025 03:34 AM
Hey Ankur,
Thank you for your prompt response. In the meantime, I tried the following code directly in the UI Policy, and it worked exactly as I wanted.
Script:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-22-2025 03:44 AM
the script I shared should also work fine.
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
01-22-2025 05:05 AM
Hi Ankur,
I have not tried this yet because I needed it in the UI Policy. and the one above which I shared worked fine for me.
I will definitely try the code you have recommended and let you know the results .
Regards,