- 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-28-2017 06:01 AM
I do think that you can accomplish this if you really want to, but this is too complicated to be reasonable in my opinion. This would be very script heavy, which presents a maintenance and testing hazard.
1st situation: Alva Pennington (employee of Department 2), needs to see a read-only reference field.
Question: Is this form only supposed to be used by employees of Department 1? Or, are you saying that Alva should only have herself as a choice in this field, because she is not the supervisor?
2nd situation: Allyson Gillispie (supervisor of Department 2) can select herself, any of her employees, or users with the department_101 role.
Answer: The script would return a query string with Allyson's user sys_id, plus sys_ids for each user where she is their manager, or users that have the role "department_101". This would be heavily scripted. How are you planning on noting that Allyson is a supervisor? Is there a role that supervisors get? Is it just the fact that they have at least 1 employee reporting to them?
3rd situation: Angelo Ferentz (supervisor of department 3) is acting as a delegate (?) of Adela Cervantz (employee in department 1). Angelo should be able to select any employee of Department 3, and Department 1.
Answer: Because Angelo is a supervisor (again - how is that decided?), you can say that he should be able to view any employee in that same department. Because Angelo is a delegate of Adela, and Angelo is a supervisor, you can include Adela's department as well. Where is it noted that Angelo is a delegate for Adela?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-28-2017 09:18 AM
Thanks Patrick for your answer.
With your expertise on my back i think its to hard for me, to get a solution done. Thanks for that.
For the questions:
1. The Form is one form for all users. So there is no form only for supervisors or anything else. Everybody has only his own name as a choice if the person isn't a supervisor
2. The supervisors are getting an own role (department_101, 102, etc). So i know, that the person in this roles is a supervisors for persons with the user specified field with the number of the end of the role.
3. The 3rd situation is only a great wish. That is to hard to explain now, i think.
As said, thanks for your great help. Have a good one.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-05-2017 11:37 AM
Dear Patrick,
i've found a solution for my problem. i posted it here: Re: GlideRecord Query returns nothing
Could you, if you have some time, if this is a good thing or the things you would change. Thanks! /daniel
