- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 06:39 AM
I have my two variables in a Service Catalog. One is Reference type (sys_user) and second list collector (sys_user).
When I select a user in 1st variable then in 2nd variable I want to see only those users who belongs to the same company as selected in 1st Variable. I know that we can use Advanced Reference qualifier and create a script include.
Can anyone share the code that we can use in script include.
Also I want to understand the syntax when we are returning the result in a function. Like use of various operators so that I can use it in future.
Thanks in Advance!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 06:47 AM - edited 02-26-2025 06:53 AM
For your ServiceNow query, you can create a Script Include to filter the second variable (list collector) based on the company of the user selected in the first variable (reference field). Here’s a quick example:
Script Include:
var FilterUsersByCompany = Class.create();
FilterUsersByCompany.prototype = {
initialize: function() {},
getUsersByCompany: function(selectedUser) {
var companyID = '';
var userGR = new GlideRecord('sys_user');
userGR.get(selectedUser); // Get the selected user's record
companyID = userGR.company; // Fetch their company
// Return query for users in the same company
return 'company=' + companyID;
},
type: 'FilterUsersByCompany'
};
javascript: new FilterUsersByCompany().getUsersByCompany(current.variables.<your_first_variable_name>);
- = (equals): Matches an exact value, like company=abc123.
- ^: Combines conditions (AND), e.g., company=abc123^active=true.
- ^OR: Adds OR logic, e.g., company=abc123^ORcompany=xyz789.
- !=: Not equal, like company!=abc123.
- STARTSWITH, ENDSWITH, CONTAINS: For partial matches, e.g., nameCONTAINSrohit.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 06:55 AM
Hi @Rohit Singh ,
Create a Script Include:
Make sure the Client Callable checkbox is checked.
use below code for the Script Include:
var FilterUsersByCompany = Class.create();
FilterUsersByCompany.prototype = {
initialize: function() {
},
getUsersByCompany: function(companySysId) {
var users = [];
var userGR = new GlideRecord('sys_user');
userGR.addQuery('company', companySysId);
userGR.query();
while (userGR.next()) {
users.push(userGR.getUniqueValue());
}
return users.join(',');
},
type: 'FilterUsersByCompany'
};
Use the Script Include in the Advanced Reference Qualifier of your second variable:
Go to your Service Catalog item and open the second variable (List Collector).
In the Advanced Reference Qualifier section, select Dynamic and then choose Advanced.
Use the following code in the Reference qualifier script:
var companySysId = current.variables.first_variable_name; // Replace 'first_variable_name' with the actual name of your first variable
var filter = new FilterUsersByCompany();
answer = 'sys_idIN' + filter.getUsersByCompany(companySysId);
ServiceNow Community MVP 2024.
Thanks,
Pavankumar