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.

Script Include returns only one result in variable

danielschmidt
Kilo Expert

Hello everyone,

i'm trying to figure out how client scripts and script includes are working together with rocord producer or variable sets.

In my developer instance i added a custom field to sys_user table called "test". I added the number 101 to some users in that table and 102 to others. Now i want to get the users with 101 in a reference variable i created for a record producer.

The Script Include i am using for that is the following:

function ScriptInclude() {

var usrsList = '';

var time_agent = '101';

var usr = new GlideRecord('sys_user');

usr.addQuery('u_test',time_agent);

usr.query();

while(usr.next()) {

if (usrsList.length > 0) {

usrsList += (',' + usr.sys_id);

}

else {

usrsList = usr.sys_id;

}

}

return 'sys_idIN' + usrsList;

}

After that i put in the advanced reference qualifier into my variable: "javascript:ScriptInclude();"

Now i have a problem. The Script include works, i think. But my reference variable only shows up one result. What did i do wrong here? Can somebody help?

If you need further information, please feel free to ask.

Thanks in advance.

1 ACCEPTED SOLUTION

Patrick Schult2
Giga Guru

It is not a good practice to manually build the list of users to be shown in the query, if you can avoid it. Because you can avoid it here, you should. The best practice is to return a query string to the caller that will let the database do the heavy lifting, instead of the JS engine, which is slower. If users with "test=101" is all you want, then you don't even need the script include, just specify exactly that as the reference qualifier and call it good.



But for argument's sake, let's say you want to do this the hard way and use the script include to manually construct the list of users.



function ScriptInclude() {  


//var usrsList = ''; // This is the problem - this is a string. At line 9, you use it like an Array. This should be an Array.


var usrsList = [];


var time_agent = '101';


var usr = new GlideRecord('sys_user');  


usr.addQuery('u_test',time_agent);  


usr.query();  


while(usr.next()) {


usrsList.push(usr.getValue("sys_id")); // Push is an Array method, you don't need to worry about commas and where you are in the array, just push all of it.


//if (usrsList.length > 0) {  


//usrsList += (',' + usr.sys_id);  


//}  


//else {  


//usrsList = usr.sys_id;   // This REALLY IMPORTANT. As you have written it here, this is declaring usrsList to be equal to the sys_id of the GlideRecord object.


// Every time you do next() on the GlideRecord, that means your sys_id is going to change, thus changing the usrsList value along with it. ALWAYS use getValue('sys_id')


//}  


}  


return 'sys_idIN' + usrsList.toString();     // Added toString here to force the Array to a string


}  


View solution in original post

12 REPLIES 12

Richard101
Mega Expert

Daniel,



Are you getting a list of a the same ID listed over and over or just one sys_id?



Either way, Instead of using usr.sys_id (which is a GlideElement) use usr.sys_id.toString(). Seems to work better. I find that when I do it the way you have it seems to all refer to the same row of the GildeRecord.



function ScriptInclude() {  


        var usrsList = [];  


        var time_agent = '101';  


        var usr = new GlideRecord('sys_user');  


        usr.addQuery('u_test',time_agent);  


        usr.query();



        while(usr.next()) {  


                  usrsList.push(usr.sys_id.toString());


        }



        return 'sys_idIN' + usrsList.join(',');  


}  




I modified lines 2 and 9 above to get rid of the IF statement, since I think it's easier to read


Patrick Schult2
Giga Guru

It is not a good practice to manually build the list of users to be shown in the query, if you can avoid it. Because you can avoid it here, you should. The best practice is to return a query string to the caller that will let the database do the heavy lifting, instead of the JS engine, which is slower. If users with "test=101" is all you want, then you don't even need the script include, just specify exactly that as the reference qualifier and call it good.



But for argument's sake, let's say you want to do this the hard way and use the script include to manually construct the list of users.



function ScriptInclude() {  


//var usrsList = ''; // This is the problem - this is a string. At line 9, you use it like an Array. This should be an Array.


var usrsList = [];


var time_agent = '101';


var usr = new GlideRecord('sys_user');  


usr.addQuery('u_test',time_agent);  


usr.query();  


while(usr.next()) {


usrsList.push(usr.getValue("sys_id")); // Push is an Array method, you don't need to worry about commas and where you are in the array, just push all of it.


//if (usrsList.length > 0) {  


//usrsList += (',' + usr.sys_id);  


//}  


//else {  


//usrsList = usr.sys_id;   // This REALLY IMPORTANT. As you have written it here, this is declaring usrsList to be equal to the sys_id of the GlideRecord object.


// Every time you do next() on the GlideRecord, that means your sys_id is going to change, thus changing the usrsList value along with it. ALWAYS use getValue('sys_id')


//}  


}  


return 'sys_idIN' + usrsList.toString();     // Added toString here to force the Array to a string


}  


Hello Patrick,



thanks for the explanation. To be clear here: I don't filter always for 101. The goal is to get the number i want to filter for from a user role the user could have or not. that brings me to the next problem, how can i get the roles of the user and check if he has one out of 30 - 40. I'm turning around in a circle. I want to show only a list if the user has a specific role and if thats true, he only has to see a specific group of users. The specific group of users is shown by the number i specified in sys_user. And the roles does contain theese numbers like "role_101", "role_102", etc.


I think I get what you are trying to do but can you write out a full example and what the script should do in that situation? There are APIs to do role checks pretty easily.


Hello Patrick,



thanks for your help. I hope the Off-Topic is allowed, So let me try to build up an example:



Lets suppose i have a record producer which is generating incidents on my incidents table.


I have there a field (generated as reference variable) where the user can choose, for whom the incident is taken for. Lets suppose the user who is creating these incidents is the supervisor or something. These supervisors does have a specific role, each department has these role. Do Department 1, Department 2 and Department 3. there are these roles/groups (the role is granted by a group) department_101, department_102 and department_103. But it's possible that the count of departments go up or down from time to time.


So, lets put some names into the department: department 1 has abel tutor as supervisor, abraham lincoln, Adela Cervantsz and Aileen Mottern as employees in this department. In the 2nd department ist Allyson Gillispie the supervisor with employees Alva Pennigton, Alyssa Biasotti and Amelia Caputo. And the 3rd department does contain these employees: Amos Linnan, Andrew Jackson and Angelique Schermerhorn with supervisor Angelo Ferentz.


After this, on the service portal there are several options: The first one, Alva Pennington wants to create a incident task. So she loads the page (she isn't a member of department_101 role) and only sees a readonly reference field.


The 2nd option ist hat allyson gillispie is loading the form, he's a member of department_101 role/group. So he can edit the reference field and choose out of himself or one of his employees in department 1 Alva Pennigton, Alyssa Biasotti and Amelia Caputo. He can't see the others employees.


The 3rd option is angelo ferentz. He's working as a representation for adela cervantsz from department 1. So he has the roles department_101 and deparmtent_103. So if he loads the form, he can chhose out of the employees from department 1 and 3 and the supervisor of them.



The employees has a number in a specific field on sys_user, like i said, for example 101 for department 1, 102 for department 2 and 103 for department 3.



So, i want to show a dynamic list in reference of the roles a user has.



Did you got all the information? Or did i miss something? Ist this even possible to build?



Thanks for your help.