Setting the date field using the UI Policy

Sandhya23
Tera Guru

 

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:

 

javascript
function onCondition() { g_form.setValue('XYZ', formatDate(new Date(), g_user_date_format)); }

Question:
Is this the correct approach?

Currently, the field is being populated, but not in the desired format. it is populated as below screenshot:

Sandhya23_0-1737543795142.png

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!

1 ACCEPTED SOLUTION

Sandhya23
Tera Guru

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.

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
        if (newValue === '40') {
        var currentDateTime = new Date();
        var formattedDateTime = formatDateTime(currentDateTime);
        g_form.setValue('ordered_date', formattedDateTime);
    }
   
    function formatDateTime(date) {
        var day = padZero(date.getDate());
        var month = padZero(date.getMonth() + 1);
        var year = date.getFullYear();
        var hours = padZero(date.getHours());
        var minutes = padZero(date.getMinutes());
        var seconds = padZero(date.getSeconds());
        return day + '/' + month + '/' + year + ' ' + hours + ':' + minutes + ':' + seconds;
    }

    function padZero(num) {
        return (num < 10 ? '0' : '') + num;
    }
}

View solution in original post

11 REPLIES 11

@Sandhya23 

Thank you for marking my response as helpful.

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

@Sandhya23 

Hope you are doing good.

Did my reply answer your question?

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

Sandhya23
Tera Guru

I tried the following code in my UI Policy script. While this sets the date field value, the date value is inconsistent  such as - 28/01/2025 22:57:3 OR 28/1/2025 15:1:35 and hovering the mouse to the date field sometime says 2 hours ago or 55 year ago. Would appreciate if anyone can share what is wrong with the following code.

 
 
function onCondition() {
    var currentDate = new Date();
    var day = String(currentDate.getUTCDate()).padStart(2, '0');
    var month = String(currentDate.getUTCMonth() + 1).padStart(2, '0');
    var year = currentDate.getUTCFullYear();
    var hours = String(currentDate.getUTCHours()).padStart(2, '0');
    var minutes = String(currentDate.getUTCMinutes()).padStart(2, '0');
    var seconds = String(currentDate.getUTCSeconds()).padStart(2, '0');
    var date = day + "/" + month + "/" + year + '' + hours + ":" + minutes + ":" + seconds;
    g_form.setValue('actual_delivery_date', date);

 

Regards,

Sandhya

Hi @Ankur Bawiskar 

 

Any thoughts on my last message please ?

 

 

Regards,

@Sandhya23 

the inconsistency is because of the logged in user's date format.

Try the script I shared and it will handle the different date formats for different logged in user

I believe I have provided enough guidance and a working script.

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