- 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: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 07:03 AM
Hi @lukasz szumilas Thanks a ton. That worked and cleared my fundamentals.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 06:52 AM - edited 02-26-2025 07:03 AM
Hi @Rohit Singh
There's one other way, which is simple when compared to script include.
Create one new reference variable which will refer to Company table e.x: company. Make sure to hide that variable to the end users and it should be dependent on the 1st user variable company field. So that it'll hold the company value of the first user field.
Now in the 2nd user variable (list collector) ref qualifier use the company value from the company question.
Sample: javascript: "active=true^company="+current.variables.company;
Hope this helps.
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-26-2025 07:04 AM
Hi @J Siva
Thanks for the alternative.