The CreatorCon Call for Content is officially open! Get started here.

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

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.

Hi @lukasz szumilas  Thanks a ton. That worked and cleared my fundamentals. 

J Siva
Kilo Patron
Kilo Patron

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&colon; "active=true^company="+current.variables.company;

 

 Hope this helps.

Regards,

Siva 

Hi @J Siva

Thanks for the alternative.