When ServiceNow form loads, the on change script is acting

Community Alums
Not applicable

Hi

 

We have an on change script which acts on date-time variable. It works fine for future date validation but once the form is submitted it takes to our custom ticket conversation widget and shows the error message that date is not valid. Also when view this ritm under 'My Requests' , it shows the error message written in the onchage client script.

Please advise me anyone has faced this issue?

I followed KB article from servicenow(https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1117925) which is for requested_for field and tried 

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

but it didn't work

 

Thanks

Bimsari

4 REPLIES 4

Sid_Takali
Kilo Patron
Kilo Patron

Hi @Community Alums Can you share your Client Script code?

Community Alums
Not applicable

Hi 

 

below is the script, on change script and variable is 

 

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

var today_dateStr = formatDate(new Date(),g_user_date_time_format);
    var five_years_from_today = new Date();

    five_years_from_today.setDate(five_years_from_today.getDate()+1826);
    var five_years_from_todayStr = formatDate(five_years_from_today,g_user_date_time_format);
    var todayNum = getDateFromFormat(today_dateStr,g_user_date_time_format);
    var thirty_daysNum = getDateFromFormat(five_years_from_todayStr,g_user_date_time_format);
    var selected_dateNum = getDateFromFormat(newValue,g_user_date_format);
    if(selected_dateNum > thirty_daysNum) {
        g_form.clearValue('access_start_date');
        g_form.showFieldMsg('access_start_date','Access start date cound not be more than five years from today.','error',true);    
    } else if(selected_dateNum < todayNum ){
        g_form.clearValue('access_start_date');
        g_form.showFieldMsg('access_start_date','Access start date can\'t be a past date','error',true);
    }else {
        g_form.hideFieldMsg('access_start_date',true);
    }



   //Type appropriate comment here, and begin script below
   
}

Ayushi12
Mega Sage

Hi @Community Alums Please share the script also check if there is any conflicting script for the date-time variable

Community Alums
Not applicable

Hi below is the script, I coudlnt find any scripts affecting this variable. It's strange that when I console.log it the new value becomes zero after submission

 

Hi 

 

below is the script, on change script and variable is 

 

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

var today_dateStr = formatDate(new Date(),g_user_date_time_format);
    var five_years_from_today = new Date();

    five_years_from_today.setDate(five_years_from_today.getDate()+1826);
    var five_years_from_todayStr = formatDate(five_years_from_today,g_user_date_time_format);
    var todayNum = getDateFromFormat(today_dateStr,g_user_date_time_format);
    var thirty_daysNum = getDateFromFormat(five_years_from_todayStr,g_user_date_time_format);
    var selected_dateNum = getDateFromFormat(newValue,g_user_date_format);
    if(selected_dateNum > thirty_daysNum) {
        g_form.clearValue('access_start_date');
        g_form.showFieldMsg('access_start_date','Access start date cound not be more than five years from today.','error',true);    
    } else if(selected_dateNum < todayNum ){
        g_form.clearValue('access_start_date');
        g_form.showFieldMsg('access_start_date','Access start date can\'t be a past date','error',true);
    }else {
        g_form.hideFieldMsg('access_start_date',true);
    }



   //Type appropriate comment here, and begin script below
   
}