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

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Is this form for users to fill out for themselves or can they inout information about other users?   If their profile, gs.getUserId() will provide the sys_id of the logged in user that you can leverage to get their user record.



These users would not be logged into their own account. This UI Page is being used for a check-in form housed on a single machine. People would walk up and sign in by typing in their information, so I could not use gs.getUserId().


Deepak Ingale1
Mega Sage

It's Now or Never - A hitchhikers guide to ServiceNow and it's universe: Part 2: Making UI Page "Ser...



I found this article helpful when I was building some test pages using Angular JS


Probably it will also help you.


For record to appear, you will have to user ng-model directive.



The ng-model directive binds the value of HTML controls (input, select, textarea) to application data. The ng-model directive can also: Provide type validation for application data (number, email, required). Provide status for application data (invalid, dirty, touched, error). Provide CSS classes for HTML elements


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.