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

Ehab Pilloor
Mega Sage

Hi @roomawakar,

You can use an onChange client script, this will take date and time from browser:

function onChange(control, oldValue, newValue, isLoading) {
  if (isLoading || newValue == '') {
    return;
  }
  if (newValue == 'true') {
    var now = new Date();
    var formatDate = 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('u_exception_granted_date', formatDate);
  } else {
    g_form.setValue('u_exception_granted_date', '');
  }
}

Regards,

Ehab Pilloor

@Ehab Pilloor  @Community Alums 

Nice coincidence, same script from both of you !

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

@Ankur Bawiskar 

EhabPilloor_0-1751983300557.png

It works.

There are same answers from different people in every other questions, so shouldn't be a surprise. I am not competing with you, you are there at the top. I get to learn from your answers. Just so happened that @Community Alums posted the solution too.

 

Regards,

Ehab Pilloor

@Ehab Pilloor 

No worries !

Keep learning and helping !

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