Search sys_user based on form input

leenathan24
Kilo Contributor

I have created a form for users to type in information about themselves. Much of this information will be in the sys_user table, and so should be able to be checked for validity. Using AngularJS from a UI Page, I am wanting to query the sys_user table to see if the information given by the user actually matches something in the table. I have a basic idea of what I should be doing, but things just aren't going well. Any ideas on the best way to traverse the sys_user table?

/*note that 'id' is an arbitrary tag for a field of a user*/

function searchforID(id) {
var user = new GlideRecord('sys_user');
var inc = new GlideRecord('incident');
user.query();
for each (var _user = new GlideRecord('sys_user') in /*sys_user table*/) {

        if (_user.id == id) {

                  inc.description = id;

        } else inc.description = 'no record';

}
}

1 ACCEPTED SOLUTION

LaurentChicoine
Tera Guru

I find it hard to understand you're for loop. Looking at it I can't quite understand what you are trying to do.



From my understanding of your question "Any ideas on the best way to traverse the sys_user table?", you are looking to match a value with any matching value no matter the field or user record.



Here is a code proposition you may adapt based on your needs:



function searchForInput(input){ //I replaced your id with input to be less confusing


        var user = new GlideRecord("sys_user");


        var inc = new GlideRecord("incident");


       


        inc.initialize(); //I'm gessing you want to create a new incident so we have to initialize it


       


        user.query(); //Query the whole user table


        while(user.next()){ //Iterate over each user


                  for (e in user){ //Starts a loop for every fields in the user record


                            if(user[e] == input){


                                      inc.description = input; //Consider using += to avoid overwriting a previous match or breaking out of loops after a match


                            }


                  }


        }


        if(inc.description == ""){ //If there was no match the description will be an empty string


                  inc.description = "no record";


        }


        inc.insert(); //Insert the created record


}



I'm not sure if that meets your needs, if it doesn't please reply to me with a more precise description of what you are trying to achieve.


View solution in original post

7 REPLIES 7

Thank you for your input! This makes a lot of sense. Right now, however, I'm having trouble actually getting this function to execute. I'm using AngularJS with this form in a separate included file. I know the file is being included and loaded correctly, because I have a variable in the controller that is being called in the HTML correctly. The problem is that the function doesn't seem to be called. I edited the function you provided slightly:



function searchForIdentifier(id) {


        var user = new GlideRecord('sys_user');


        var inc = new GlideRecord('incident');



        inc.initialize();


        inc.description = '';


        inc.short_description = '';



        //user.addNotNullQuery('u_uin');


        user.query();



        var keepSearching = true;


        while (keepSearching && user.next()) {


                  for (var e in user) {


                            if (user[e] == id) {


                                      keepSearching = false;


                                      inc.description += input;


                                      inc.short_description += input;


                            }


                  }


        }



        if (inc.description == '') {


                  inc.description += 'no record';


                  inc.short_description += 'no record';


        }



        inc.insert();


        console.log(inc.number + ' created');


        gs.alert(inc.number + ' created');


}



This function is being called here (or at least it's supposed to be):



<div id="submit-button" name="submit-button">


        <button type="submit" name="submit" value="submit"


                  ng-submit="searchForIdentifier(uin)">Sign In</button>


</div>



I have tried having `searchForIdentifier()` included as a function within the Angular controller, as well as simply including it in the script file, but outside the controller. Both seem to have identical behavior, that is, nothing. Clearly I don't know how to utilize this function in my ServiceNow UI Page.



EDIT: I now realize that I should have been using the `ng-submit` directive in the form tag instead of the submit button tag. So I changed that, but still, there is a total lack of any indication that the function is being executed.


I'm not quite familar with implementing AngularJS inside a UI Page. However, I'm more familar with AngularJS in general and AngularJS inside the new ServicePortal in Helsinki.



Inside a service portal widget, your function searchForIdentifier(id) would need to have both a definition ($scope.searchForIdentifier = function(id){...}) in the Client Script (or controller in pure AngularJS) which would be passing the input to a Server Script running the GlideRecord query.



However, in a UI Page, I'm not quite sure how you would accomplish that using angular.


Depending on your ACL, the GlideRecord query could be run on the client side if your user have access to the required field in the sys_user table (which would not really make sens as you are trying to validate information and they would have access to that information if they were advanced developers).



So the other way is a GlideAjax that you would need to call (see GlideAjax doc in the wiki).



As I said, I'm not used to AngularJS in UI Page, but if you want to leave it aside, I can lead you better with a solution leaving AngularJS aside.


I'm going to keep playing around with this, and see if I can get the function call to work using the function from within the controller. If I find out something that works, I'll let you know for your own future reference, and if I get stuck then I may come back to you for help in implementing this without using AngularJS.



Thank you for all the help!