The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Clear invalid entry on a reference field: Reset filter on Reference field

lomouhamadou
Kilo Guru

Dear,

I need to emptied the value of the reference field when user put something wrong like "ggggggg".

What happened is the default filter will use "name starts with <user input>".

This can be change in the sys_properties but rules make that I cannot change global settings.

I tried a client script on chance on the Service Field and it is not working because I cannont get the wrong value that user put.

When user put something wrong, the field goes red, usually end user get the fact that it is wrong. Then they click on the lookup but no lists shown because the filter kept what they wrote wrong.

Thanks in advance.

find_real_file.png

Regs,

Lô Mouhamadou

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hello Mouhamadou,



This is actually a difficult thing to achieve. The newValue as passed into a client script for a reference field will be either the string of the sys_id of the referenced record, or it will be a blank string. The only way to tell whether the user has blanked out the value or entered an invalid value would be to get the actual display control and check. An additional problem is that the onChange script will be called twice when the value is invalid.



These are not insurmountable problems You could use some DOM scripting to work around this, but you must be careful to test thoroughly. Since you will need to do direct DOM manipulation, you should be aware that a customization like this can stop working when the ServiceNow UI is updated. It will be up to you to maintain the functionality if ServiceNow changes the way forms are rendered.



That said, here is an example client script which implements some of the functionality.



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


      if (isLoading) {


              return;


      }


      //The new value could be blank because the actual value is blank, or because the user entered an invalid value


      if (newValue == '') {


              var id = control.id;


              id = id.split('.').join('\\.');


              id = '#sys_display\\.' + id;



              var elem = ($j(id));


              var val = elem.val();



              //don't run if it's not invalid


              if (!elem.hasClass("ref_invalid"))


                      return;



              //it's a legit empty value (should be caught by above check, but just to be safe)


              if (!val)


                      return;



              //we might be in the second call


              if (window.invalidReferenceValue == val)


                      return;



              //store the value so we can compare on the second time through


              window.invalidReferenceValue = val;


              setTimeout(function () {


                      g_form.setValue("cmdb_ci", "");


                      g_form.showFieldMsg("cmdb_ci", "The Configuration Item you entered does not exist. Please use the magnifying glass to choose from a list of acceptable values", "error");


              });


      }


}



Again, maintaining a script like this will be your responsibility, since it steps well outside the bounds of the standard g_form API.



Thanks


Cory


View solution in original post

5 REPLIES 5

coryseering
ServiceNow Employee
ServiceNow Employee

Hello Mouhamadou,



This is actually a difficult thing to achieve. The newValue as passed into a client script for a reference field will be either the string of the sys_id of the referenced record, or it will be a blank string. The only way to tell whether the user has blanked out the value or entered an invalid value would be to get the actual display control and check. An additional problem is that the onChange script will be called twice when the value is invalid.



These are not insurmountable problems You could use some DOM scripting to work around this, but you must be careful to test thoroughly. Since you will need to do direct DOM manipulation, you should be aware that a customization like this can stop working when the ServiceNow UI is updated. It will be up to you to maintain the functionality if ServiceNow changes the way forms are rendered.



That said, here is an example client script which implements some of the functionality.



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


      if (isLoading) {


              return;


      }


      //The new value could be blank because the actual value is blank, or because the user entered an invalid value


      if (newValue == '') {


              var id = control.id;


              id = id.split('.').join('\\.');


              id = '#sys_display\\.' + id;



              var elem = ($j(id));


              var val = elem.val();



              //don't run if it's not invalid


              if (!elem.hasClass("ref_invalid"))


                      return;



              //it's a legit empty value (should be caught by above check, but just to be safe)


              if (!val)


                      return;



              //we might be in the second call


              if (window.invalidReferenceValue == val)


                      return;



              //store the value so we can compare on the second time through


              window.invalidReferenceValue = val;


              setTimeout(function () {


                      g_form.setValue("cmdb_ci", "");


                      g_form.showFieldMsg("cmdb_ci", "The Configuration Item you entered does not exist. Please use the magnifying glass to choose from a list of acceptable values", "error");


              });


      }


}



Again, maintaining a script like this will be your responsibility, since it steps well outside the bounds of the standard g_form API.



Thanks


Cory


lomouhamadou
Kilo Guru

thanks a lot Cory. I do appreciate.



It is working with a normal client script.



Now I want to do the same with a Record producer client script and it is not working as from line 14.


I guess maybe a syntax is not accepted from record producer : catalog client script.



Do you have any idea on how to solve the issue, please?



  1. //don't run if it's not invalid  
  2.               if (!elem.hasClass("ref_invalid"))  
  3.                       return;  
  4.  
  5.               //it's a legit empty value (should be caught by above check, but just to be safe)  
  6.               if (!val)  
  7.                       return;  
  8.  
  9.               //we might be in the second call  
  10.               if (window.invalidReferenceValue == val)  
  11.                       return;  
  12.  
  13.               //store the value so we can compare on the second time through  
  14.               window.invalidReferenceValue = val;  
  15.               setTimeout(function () {  
  16.                       g_form.setValue("cmdb_ci", "");  
  17.                       g_form.showFieldMsg("cmdb_ci", "The Configuration Item you entered does not exist. Please use the magnifying glass to choose from a list of acceptable values", "error");  
  18.               });  
  19.       }  

On the record producer it is displayed on the cos portal and not in serviceNow. FYI


  1. var elem = ($j(id));   //return [object Object] in both case. so not the issue
  2.               var val = elem.val(); // on service now form return "the characters that users has entered" but on record producer it is undefined