How to restrict a datefield time to not to select future date in record producer

Srinivas K
Tera Contributor

Hi All,

I am having a datetime field in servicenow with the name "issue_first_occur". I tried with below script, but it is not working. Please help me with the script that when someone selects the date in future, the script shouldnot accept it

I created an on change catalog client script

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    //current date
 
    var currentDateObj = new Date();
    var currentDateStr = formatDate(currentDateObj, g_user_date_format);
    var currentDateNum = getDateFromFormat(currentDateStr, g_user_date_format);
    var selfirstdate= g_form.getValue('issue_first_occur_date');
    alert("firstdate is " +selfirstdate); //Getting proper date from the field
    var startDateNum = getDateFromFormat(selfirstdate, g_user_date_format);
    alert("startdatenum " +startDateNum); //This is coming as 0
    if (startDateNum > currentDateNum) {

        alert("Date is in the future");
        g_form.clearValue("issue_first_occur_date");
        return false;

    }

}
Please help me in resolving this
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Srinivas K 

no script required. you can handle it using UI policy

No Code date validations thru (Catalog) UI Policies

Field type is date or date/time?

If it's date/time then use g_user_date_time_format instead of g_user_date_format

 

 

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

View solution in original post

2 REPLIES 2

Ankur Bawiskar
Tera Patron
Tera Patron

@Srinivas K 

no script required. you can handle it using UI policy

No Code date validations thru (Catalog) UI Policies

Field type is date or date/time?

If it's date/time then use g_user_date_time_format instead of g_user_date_format

 

 

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

Thankyou @Ankur Bawiskar  it worked well after I give g_user_date_time_format as it is date/time field type.