Validation script for a variable which should validate if typed value is available in sys_user table

Pranita2
Kilo Guru

Hello Team,

 

I am trying to validate the variable "requested_for" i.e. when any end user starts typing the user name, if the user is not available there should be a pop-up alert which displays certain message. I have created the script as OnChange as catalog client script. But below script is not available:

 



var referenceField = 'requested_for';


var enteredNameBuffer = '';


g_form.getControl(referenceField).observe('keydown', function (evt) {

var charCode = evt.which || evt.keyCode;


var typedChar = String.fromCharCode(charCode);


if (/^[a-zA-Z0-9_]+$/.test(typedChar)) {

enteredNameBuffer += typedChar;


if (enteredNameBuffer) {

var userGr = new GlideRecord('sys_user');
userGr.addQuery('name', 'STARTSWITH', enteredNameBuffer);
userGr.query();

 

if (!userGr.hasNext()) {

alert('The entered Name does not match any user in the sys_user table. Please enter a valid Name.');

enteredNameBuffer = '';

g_form.setValue(referenceField, '');
}
}
});
}

 

Please help me identify what is the issue with the above script.

 

Any quick help is very much appreciated.

 

Thanks,

Pranita Bahuguni

 

6 REPLIES 6

Premankit
Giga Expert

Just a curious question, why are you not using Reference field than to be going through client script?

 

Reference field allows you to show all available user, you have independence to show how you want drop down to appear. And if reference is not found you can quickly return result. You can reference 

 

Post adding a reference field you can use following 
" if (!g_form.getValue('requested_for')) { g_form.showErrorBox('User not found'); } "

 

 

Please go through following document, as it has interesting ways to do what you are trying to achieve. 

https://docs.servicenow.com/bundle/vancouver-platform-administration/page/administer/field-administr... 

Premankit
Giga Expert

I am assuming that Requested for is a reference field. If that is the case you can use out of box functionality of Reference field to show the records as in they type. 

 

Please refer below link to review few of methods 

https://docs.servicenow.com/bundle/vancouver-platform-administration/page/administer/field-administr... 

 

Also in place of using glide query use getValue for reference field. you can do something simple as 

 

Check ##if (!(g_form.getValue('requested_for')) and use  g_form.showErrorBox() method. 

 

 

Aniket Chavan
Tera Sage
Tera Sage

Hello @Pranita2 ,

Please give a try to the script below and see how it works for you.

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

  var referenceField = 'requested_for';
  var enteredNameBuffer = g_form.getValue(referenceField);

  var userGr = new GlideRecord('sys_user');
  userGr.addQuery('name', 'STARTSWITH', enteredNameBuffer);
  userGr.query();

  if (!userGr.hasNext()) {
    alert('The entered Name does not match any user in the sys_user table. Please enter a valid Name.');
    g_form.setValue(referenceField, '');
  }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks,

Aniket

Hi @Aniket Chavan ,

 

Thankyou for your quick response but my scenario is when the user starts typing in a variable field which is reference, user should start validating it on User table.

 

And that's the reason i am using keydown and getControl in the script.

 

Can you please let me know how this can be achieved?

 

Thanks,

Pranita Bahuguni