How to: create an advanced reference qualifier for a Record Producer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-29-2014 07:27 PM
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.
- Service Catalog > Record Producer
- New
- 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
- On the Variables related list tab, click New
- Type: Reference
- Select the Question tab and enter
Question: Select a company.
Name: u_company - Select theType Specifications tab and enter
Reference: Company [core_company] - 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.
- On the Variables related list tab, click New
- Type: Reference
- Select the Question tab and enter
Question: Select a user.
Name: u_user - Select theType Specifications tab and enter
Reference: User [sys_user]
Use reference qualifier: Advanced
Reference qual: javascript: u_getCurrentCompanyUsers() - 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.
- Service Definition > Script Includes
- New
- 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.
- Labels:
-
Service Catalog
- 47,301 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-13-2018 03:03 PM
I found example[1] helpful. Please note script include will return something like
"sys_idIN ,20e828764f10220056d327201310c714,20e8e8f64f10220056d327201310c77c"
for the function call.
[1]https://www.servicenowguru.com/scripting/script-includes-scripting/advanced-reference-qualifier-script-include/