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,294 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-29-2017 03:10 AM
Hi Naman,
Yes, you can use the "^" symbol as an AND operator. So it would look something like this:
javascript: 'company='+current.variables.cr_rec_pro_company.toString()+'^department='+current.variables.cr_rec_pro_department.toString();
Kind regards
Lasse
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-07-2017 02:06 AM
I have created a variable which is of type Lookup Select Box. Here advanced reference qualifier doesnt seem to work as expected.
Whats the solution for this?
Case 1 : javascript:'name=a-ip' is displaying the contents as expected.
Case 2: javascript:'name='+current.variables.publicIpAddressName.toString() is showing nothing,when a-ip is entered in public ip name variable.
Thanks in advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-09-2017 09:03 AM
Hi Naman,
The reference qualifier is not dynamically being updated. If you want to do this, then you need to add client script to update the lookup box based on changes to the variable "publicIPAddressName. This can for instance be done using GlideAjax. There is a relatively good example here:
I hope this helps.
Lasse
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 09:40 PM
Hi Lesse,
Can you throw some light on how did you created that filter using javascript.
if you have any material or online link to learn hot to build the condition would be of greate help.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-29-2017 11:27 PM
Hi Naresh,
You can open any list in ServiceNow and then use the filter to build your query. Once you are satisfied with the result you can right click the breadcrumb and select "Copy query". Then you get the filter string and you then just have to replace the parts that you want to be variables using javascript.
I hope this helps.
/Lasse