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

How to restrict past date selection in due date field in problem task

Gopal14
Tera Contributor

Hi Team,

 

I want to restrict past date selection in due date field in problem task and Change task tables.

 

I have tried directly in UI Policy, it is working but in form it is showing error, if selected date has passed past date.

 

Apart from that how can i do this

16 REPLIES 16

@Gopal14 

why you don't want to clear?

I recommend clear since validation fails.

This ensures user doesn't save with wrong data.

💡 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  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Deepak Shaerma
Mega Sage

Hi @Gopal14 

Try this onchange client script:

Type: onChange
Table: Problem Task [problem_task]
Field Name: Due date [due_date]

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }

    // 1. Get the current date (Browser time) and zero out the time component
    var today = new Date();
    today.setHours(0, 0, 0, 0);

    // 2. Convert the ServiceNow date format to a JS Date object
    // g_user_date_format is a system variable that ensures it works with your user's format preference
    var inputDateNum = getDateFromFormat(newValue, g_user_date_format);
    var inputDate = new Date(inputDateNum);
    inputDate.setHours(0, 0, 0, 0);

    // 3. Compare: If Input is strictly less than Today, it is in the past.
    // (If it is Equal to Today, this condition is false, so it allows it.)
    if (inputDate < today) {
        g_form.addErrorMessage('Due date cannot be in the past. Please select Today or a future date.');
        g_form.clearValue('due_date'); // Optional: Clear the invalid entry
    }
}

If you don't want to maintain a script, you can use a UI Policy.

Table
: Problem Task [problem_task]
Short Description: Validate Due Date not in Past

Conditions:

  • Due date | before | Today
    Run scripts
    : Checked.
    Execute if true:

function onCondition() {
    g_form.addErrorMessage('Past dates are not allowed.');
    g_form.clearValue('due_date');
}



 

 

Happy to help! ‌‌
To help others in the community find this solution, kindly mark this response as the Correct Answer ‌‌ and Helpful‌‌.
Warm Regards,
Deepak Sharma
Community Rising Star 2025