How to use the various operators for returning a query in a java script.

Rohit  Singh
Mega Sage

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! 

1 ACCEPTED SOLUTION

lukasz szumilas
Tera Guru
Hey Rohit!

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'
};

 

 

 

Then, in your list collector’s Advanced Reference Qualifier, call it like this:
 
javascript&colon; new FilterUsersByCompany().getUsersByCompany(current.variables.<your_first_variable_name>);

 

As for syntax and operators when returning a query:
  • = (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.

View solution in original post

5 REPLIES 5

PavanK960672992
Mega Patron

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);

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar