- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2025 06:25 AM
Hi All,
I have a requirement to show a field based on the another field. Two fields are :
1. DLP Exception Granted of type (True/False)
2. Exception Granted date of type (date/time)
Now, the requirement is to show the field -"Exception Granted date" on the click of "DLP Exception Granted" and the second field should show the current date and time on the field.
The first part , i have achieved through UI Policy but unable to set the current date and time.
Can someone please help me out on setting the current date and time on the field -"Exception Granted date"?
Thanks,
Rooma
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2025 06:32 AM
don't use UI policy as it will require script.
Use onChange client script and set the current date/time using this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('dateTimeField');
if (newValue.toString() == 'true') {
var today_date = new Date();
var today_date_time_str = formatDate(today_date, g_user_date_time_format);
g_form.setValue('dateTimeField', today_date_time_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
07-08-2025 06:32 AM
don't use UI policy as it will require script.
Use onChange client script and set the current date/time using this
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '')
g_form.clearValue('dateTimeField');
if (newValue.toString() == 'true') {
var today_date = new Date();
var today_date_time_str = formatDate(today_date, g_user_date_time_format);
g_form.setValue('dateTimeField', today_date_time_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
07-08-2025 07:49 AM
@Ankur Bawiskar With Client script, i required the UI Policy as well to hide and unhide the field -"Exception Granted date". Without UI Policy , it was not hiding the field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-08-2025 08:35 AM
you can enhance the script as this to show/hide
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) {
return;
}
if (newValue == '') {
g_form.clearValue('dateTimeField');
g_form.setDisplay('dateTimeField', false);
}
if (newValue.toString() == 'true') {
var today_date = new Date();
var today_date_time_str = formatDate(today_date, g_user_date_time_format);
g_form.setValue('dateTimeField', today_date_time_str);
g_form.setDisplay('dateTimeField', true);
}
}
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
07-08-2025 06:34 AM
Sure Rooma! You’ve already got the first part (showing/hiding the field based on “DLP Exception Granted”) with a UI Policy — that’s perfect.
Now let’s talk about the second part: setting the current date and time in the “Exception Granted date” field when that checkbox is checked.
Step 1: Add a UI Policy Action
In the same UI Policy where you show the “Exception Granted date” field:
Add a UI Policy Action on the “Exception Granted date” field.
Set “Default value” → leave it blank.
But this alone won’t set the current datetime; you need a script.
Step 2: Use a Client Script (onChange Client Script)
Create an onChange Client Script on the “DLP Exception Granted” field (the checkbox):
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == oldValue) {
return;
}
if (newValue == 'true') {
// Set the current date and time when the checkbox is checked
var now = new Date();
var gdt = now.getFullYear() + '-' +
String(now.getMonth() + 1).padStart(2, '0') + '-' +
String(now.getDate()).padStart(2, '0') + ' ' +
String(now.getHours()).padStart(2, '0') + ':' +
String(now.getMinutes()).padStart(2, '0') + ':' +
String(now.getSeconds()).padStart(2, '0');
g_form.setValue('exception_granted_date', gdt);
} else {
// Optionally clear the date when unchecked
g_form.setValue('exception_granted_date', '');
}
}