Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Querying Another table from Client Script

nehapateria
Kilo Expert

Hi Members,

I have a simple query. I am trying to query 'sys_user' table from my Custom form. I am querying 'user_id'   of my current form to 'user_name' of 'sys_user' table. If the record is found then a user must get an alert that the same UserId exists in the system. For that I have written the below Client Script. Issue is :- I am getting a below error message in the 'user_id' field.

Error:-

onChange script error: TypeError: GlideRecord is not a constructor function (){var o=i(m,arguments);return l.apply(n,o)}

Please Suggest.

Thanks,

Neha

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

                              if(isLoading)

                              {

                                                              return;

                              }

                              var gr1 = new GlideRecord('sys_user');

                              gr1.addQuery('user_name', newValue);

                              gr1.query();

                              myFunc();

                             

                              function myFunc()

                              {

                                while(gr1.next())

                                    alert('USER ID already exists');

                              }

    //Type appropriate comment here, and begin script below

   

}

1 ACCEPTED SOLUTION

Hi Neha ,



Try something as below in script include:-



var sysid = this.getParameter('sysparm_sys_id')   ;
var gr1 = new GlideRecord('sys_user');


                              gr1.addQuery('sys_id', sysid )


                              gr1.query();
                         


                                while(gr1.next())


                                    gs.log('user exists');


                                      return true;
                              }




Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


Thanks,
Deepa


View solution in original post

39 REPLIES 39

Deepa & Ashutosh,



Many Thanks for your help;



The issue is Resolved now. Below are the scripts. But if you think that code needs to be improved then please feel free to share your suggestions.



Client Script: -



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


    if (isLoading || newValue == '') {


          return;


    }


                              var sysid = newValue;


                              var ga1 = new GlideAjax('ValidateUser');


                              ga1.addParam('sysparm_name', 'checkUser');


                              ga1.addParam('sysparm_newValue', newValue);


                              ga1.addParam('sysparm_sys_id', sysid);


                              alert('newValue '+newValue +'sysid ' +sysid);


                              ga1.getXML(UserParse);


                         


                              function UserParse(response)


                              {


                                var answer = response.responseXML.documentElement.getAttribute("answer");


                                alert(answer);


                              }




    //Type appropriate comment here, and begin script below



}




Script Include :-



var ValidateUser = Class.create();


ValidateUser.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {


    checkUser: function() {


    var userid = this .getParameter('sysparm_newValue');  


    var sysid = this.getParameter('sysparm_sys_id');


    var gr1 = new GlideRecord('sys_user');


    gr1.addQuery('user_name', userid);


    gr1.query();


     


    while(gr1.next())


    return "UserID already existing";;



    },



    _privateFunction: function() { // this function is not client callable        


    }


});


what does it return becoz still i am getting null value.


Please try my code and check with existing userid and non-existing userid. For existing you will get an alert and for non-existing you will get 'null' value.


No i did same thing what you wrote.. after that only i posted that,,,its returning null now also.


Check if field names are correct