- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 12:48 PM
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.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:24 PM
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
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:17 PM
Please use below code
- 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 += ',' + usr.sys_id;
- }
- return 'sys_idIN' + usrsList;
- }
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:22 PM
Thank you so much Sachin. Why do i have to remove the else thing there? Could you explain me that a little bit please?
dhravesh Thanks for your question: i don't use the simple qualifier with filters because i only want to provide this reference field to user groups which has the same number as the number specified in the user field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:18 PM
Provide with the screenshot of reference variable, why not you are using simple qualifier and setting the filters.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-27-2017 01:20 PM
A script include in reference qualifier should return an array.
Try this:
function ScriptInclude() {
var usrArray = [];
var time_agent = '101';
var usr = new GlideRecord('sys_user');
usr.addQuery('u_test',time_agent);
usr.query();
while(usr.next()) {
usrArray.push(usr.getUniqueValue());
}
return usrArray;
}
Also, notice I used usr.getUniqueValue() instead of usr.sys_id, which points to the record, so only last sys_id might be used depending on the operation that is made to it. In your case it was already converted to a string because it was concat to ','.