We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

restirction due date is not working in SOW view

Sriram Pusuluri
Tera Contributor

Hi,

 

I have written onchange client script, below is my script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '')
        return;

    if (newValue == oldValue)
        return;

    var format = g_user_date_time_format; // or g_user_date_format
    var dateMs = getDateFromFormat(newValue, format);

    var selectedDate = new Date(dateMs);
    selectedDate.setHours(0,0,0,0);

    var today = new Date();
    today.setHours(0,0,0,0);

    if (selectedDate < today) {

        g_form.clearValue('due_date');

        setTimeout(function() {
            g_form.showFieldMsg(
                'due_date',
                'Past dates are not allowed. Please select Today or Future.',
                'error'
            );
        }, 100);

    } else {
        g_form.hideFieldMsg('due_date');
    }
}

 

Code is working fine in Native view, when I am working on SOW it is not working.  

 

Field is due_date and Type is Date/Time

1 ACCEPTED SOLUTION

@Sriram Pusuluri 

this UI policy worked for me in both

Even if you give 1 min in past it gives error

AnkurBawiskar_0-1771224728071.pngAnkurBawiskar_1-1771224737800.png

Output

due date not in past 1 min also not working.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

View solution in original post

8 REPLIES 8

Ankur Bawiskar
Tera Patron

@Sriram Pusuluri 

why not use UI policy with no script and it will work in both sides?

AnkurBawiskar_0-1771222851829.png

 

AnkurBawiskar_1-1771222862103.png

 

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Ankur Bawiskar
Tera Patron

@Sriram Pusuluri 

try this if you want to use script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '')
        return;

    g_form.hideFieldMsg('due_date');

    var today = new Date().getTime();
    var selectedDate = new Date(newValue).getTime();

    if (today > selectedDate) {
        g_form.showFieldMsg(
            'due_date',
            'Past dates are not allowed. Please select Today or Future.',
            'error'
        );
    }
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Ankur Bawiskar 

 

I have used same script, but it is not working, in both views SOW and Native View

@Sriram Pusuluri 

this UI policy worked for me in both

Even if you give 1 min in past it gives error

AnkurBawiskar_0-1771224728071.pngAnkurBawiskar_1-1771224737800.png

Output

due date not in past 1 min also not working.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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