- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 09:28 AM
I have a variable in a variable set that shows all users by default and they do not want to change that, but on an order guide and the catalog items under that order guide, they want that user field to only show active users.
Script Include
var FilterUserResultstoActive = Class.create();
FilterUserResultstoActive.prototype = {
initialize: function() {},
activeUsers: function() {
var userCheck = new GlideRecord("sys_user");
userCheck.addEncodedQuery('active=true');
userCheck.query();
},
type: 'FilterUserResultstoActive'
};
And then onload client script:
function onLoad() {
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('FilterUserResultstoActive');
ga.addParam('sysparm_name', 'FilterUserResultstoActive');
ga.getXML(FilterUserResultstoActive);
function FilterUserResultstoActive(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
}
}
But the filter isn't working. Any ideas on what I am missing?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 09:50 AM - edited 12-22-2023 10:02 AM
Also, you can create a Catalog Client Script for variable set - you can call your filter logics there, but still...you need your query on the variable itself - refQual - or find a way to pass it (using session or similar approach).
In my opinion this is not optional way to do it - reusing unusable set just for the idea of reusing and causing lots of work and possible undesired results afterwards.
On second thought you can use sys_class_name to decide if you should show all users or only active ones - you just add in the variable as reference qualifier your script include but a bit changed - check this for reference what i mean - https://servicenowguru.com/scripting/script-includes-scripting/advanced-reference-qualifier-script-i...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 09:37 AM - edited 12-22-2023 09:47 AM
@JuliaHowells : Could you brief on the below:
1. Are you trying to repurpose the same variable set that shows all the users by default? If yes, this cannot be done via client scripts and should be done with reference qualifiers of that variable and it's a shared variable set it will impact all the other catalogs.
Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 09:42 AM
First of all, the approach to filter data in a Reference Variable is incorrect. You will not be able to achieve this with Client Script.
You need to use a reference qualifier within that variable. Now because you are saying, you are not supposed to impact that variable for all catalog items, I would suggest you create a new variable only as you will definitely require to update reference qualifier of that variable.
From the reference qualifier, you are supposed to call the Script Include & your script include should return filtered users sys-id which is comma separated to the reference qualifier.
E.g. You want your script include to return something as follows -
return 'first-user-sys-id,second-user-sys-id,third-user-sys-id';
I hope I have set you on the right path. If it helps, please mark the answers as ACCEPTED / HELPFUL.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 09:43 AM
First part is your SI:
var FilterUserResultstoActive = Class.create();
FilterUserResultstoActive.prototype = {
initialize: function() {},
activeUsers: function() {
var query = "sys_idIN";
var count = 0;
var userCheck = new GlideRecord("sys_user");
// userCheck.addEncodedQuery('active=true');
// better use this one as it's faster
// even better - use parameter to pass to activeUsers() method which you can use as your query - parm = "active=true", then activeUsers(parm); and then
// use it in userCheck.addEncodedQuery(parm) - it's much more easier for you afterwards and more functional I think
userCheck.addActiveQuery();
userCheck.query();
while(userCheck.next()){
if(count == 0){
query += userCheck.getUniqueValue();
} else {
query += "," + userCheck.getUniqueValue();
}
count++;
}
return query;
},
type: 'FilterUserResultstoActive'
};
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2023 09:50 AM - edited 12-22-2023 10:02 AM
Also, you can create a Catalog Client Script for variable set - you can call your filter logics there, but still...you need your query on the variable itself - refQual - or find a way to pass it (using session or similar approach).
In my opinion this is not optional way to do it - reusing unusable set just for the idea of reusing and causing lots of work and possible undesired results afterwards.
On second thought you can use sys_class_name to decide if you should show all users or only active ones - you just add in the variable as reference qualifier your script include but a bit changed - check this for reference what i mean - https://servicenowguru.com/scripting/script-includes-scripting/advanced-reference-qualifier-script-i...