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

Hi @Sandhya23 ,

 

Can you try below code.

function onCondition() {
    var currentDate = new Date();  // Get the current date and time
    var year = currentDate.getFullYear();
    var month = String(currentDate.getMonth() + 1).padStart(2, '0');  
    var day = String(currentDate.getDate()).padStart(2, '0');  
    var hours = String(currentDate.getHours()).padStart(2, '0');  
    var minutes = String(currentDate.getMinutes()).padStart(2, '0');  
    var seconds = String(currentDate.getSeconds()).padStart(2, '0');  

    // Create the formatted date string
    var formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

    // Set the value of the field
    g_form.setValue('actual_delivery_date', formattedDate);
}

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

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;
    }
}