show current date and time on a field

roomawakar
Tera Contributor

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

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@roomawakar 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron
Tera Patron

@roomawakar 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

@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.

@roomawakar 

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.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Community Alums
Not applicable

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', '');
  }
}