How to: create an advanced reference qualifier for a Record Producer

Danny Mortense1
ServiceNow Employee
ServiceNow Employee

You can use a reference qualifier to restrict the data that is selectable for a reference field. Setting up a simple reference qualifier is quite straight forward, especially if you simply need to filter the results of the referenced field by a static value. However, creating an advanced reference qualifier for a record producer proved to be a lot trickier than I first imagined.

 

With the help of my legendary colleague and good friend Arlen Vartazarian (Kudos) I have created my own example based upon the example Constraining the Assignment Group Field found in the wiki. The script include below can be written in a number of ways, but this is just one that works for me.

 

Firstly we need to create our Record Producer with two variables, u_company and u_user.

  1. Service Catalog > Record Producer
  2. New
  3. Name: Users with company example
    Table name: User [sys_user]

Now create the first variable, a reference type field referring to the core_company table

  1. On the Variables related list tab, click New
  2. Type: Reference
  3. Select the Question tab and enter
    Question: Select a company.
    Name: u_company
  4. Select theType Specifications tab and enter
    Reference: Company [core_company]
  5. Click Submit

Now create the second variable, a reference type field referring to the sys_user table. This time we'll put on an advanced reference qualifier.

  1. On the Variables related list tab, click New
  2. Type: Reference
  3. Select the Question tab and enter
    Question: Select a user.
    Name: u_user
  4. Select theType Specifications tab and enter
    Reference: User [sys_user]
    Use reference qualifier: Advanced
    Reference qual: javascript: u_getCurrentCompanyUsers()
  5. Click Submit

That has created the basic requirements for our example Record Producer with two variables requesting the user to select a company and a user. However, we are not finished yet. We now need to create a Script Include containing the function we are calling in our Record Producer. This function will return a list of users for the company selected from your first Record Producer variable.

  1. Service Definition > Script Includes
  2. New
  3. Name: u_getCurrentCompanyUsers
    Script:

function u_getCurrentCompanyUsers(){

  var usrsList = ' ';

  var cmpny = current.variables.u_company;

  var usr = new GlideRecord('sys_user');

  usr.addQuery('company',cmpny);

  usr.query();

  while(usr.next()) {

  if (usrsList.length > 0) {

  //build a comma separated string of groups if there is more than one

  usrsList += (',' + usr.sys_id);

  }

  else {

  usrsList = usr.sys_id;

  }

  }

  // return a list of sus_user id's for the selected company

  return 'sys_idIN' + usrsList;

}

  4. Click Submit

 

Done!! Now go back to you Record Producer and Try It.

 

NOTE: I am by no means a scripting guru, so please feel free to contribute improvements or your own examples. This is simply me sharing knowledge acquired during my journey of learning the power of the ServiceNow platform.

15 REPLIES 15

mev
Tera Contributor

Hi Danny,



Thanks for posting this.   It worked like a charm!   Took me 5 minutes to put in place for my catalog item.  



Regards,


Patrick


rlatorre
Kilo Sage

Try Create a dynamic filter option



These can be available in filters or as reference qualifiers and are shared. Works for both reference fields and variables.


Nice idea. I'll have to try that one out. I discovered that you can build your own Dynamic Filters not that long ago.



Thanks to all who have posted helpful additions to this info. It's always good to plant the seed an watch it grow.


Yeah, I would consider using Dynamic Ref Qualifiers as a Best Practice for my Development Team.



ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

sifynm
Tera Expert

Dear Mr. Danny Mortensen,



                    I have done as mentioned in the documentation, but would like to know how to use it or test it? Kindly requesting someone to help me here with an example. To be prepared I have read this documentation here http://wiki.servicenow.com/index.php?title=Reference_Qualifiers#gsc.tab=0 .