Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Sandhya23 

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.

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

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:

function onCondition() {
var currentDate = new Date();
var date = currentDate.getDate()+"/" +currentDate.getMonth()+1 +"/"+ currentDate.getFullYear()+" "+currentDate.getHours()+":"+currentDate.getMinutes()+":"+currentDate.getSeconds()
g_form.setValue('ordered_date',date);
}
 
Regards,

@Sandhya23 

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.

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

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,