- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2016 07:37 PM
Hello all, there is a requirement that restricts the users based on the company selected on the Incident form. I have created a script include to do this bit :
var getCurrentCompanyUsers = Class.create();
getCurrentCompanyUsers.prototype = {
initialize: function() {
var userslist = '';
var comp = current.company;
var usr = new GlideRecord('sys_user');
usr.addQuery('company',comp.sys_id);
usr.query();
while(usr.next()) {
if(userslist.length > 0) {
userslist += (',' +usr.sys_id);
}
else {
userslist = usr.sys_id;
}
}
return 'sys_idIN' +userslist;
},
type: 'getCurrentCompanyUsers'
};
Still, it is showing the list of all the users in the system and not in the selected company. Is there something I am missing in the code ?
(I can use the dependent functionality but using the advanced ref qualifier so that the filter could be modified at any time)
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2016 05:53 PM
Unfortunately Reference Qualifiers don't show the query in the breadcrumb.
I posted some code up on how to do it via UI Macro and Field decorators a little while ago.
See my comment on the thread below:
Reference Lookup Initial Filter
I will try adapt it for you below:
Use a UI Macro
http://wiki.servicenow.com/index.php?title=UI_Macros#gsc.tab=0
Then, use the ref_contributions attribute to call it on the field.
Dictionary Attributes - ServiceNow Wiki
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<j:set var="jvar_n" value="my_company_users_${ref}"/>
<g:reference_decoration id="${jvar_n}" field="${ref}"
onclick="filterUserByCompany(); "
title="${gs.getMessage('My Companies Users')}" image="images/icons/tasks.gifx"/>
<script>
function filterUserByCompany() {
var thefield = 'caller_id'; //Identify the reference field this should be attached to.
var thetable = 'incident'; //This gets whatever table you are on.
var lookupfield = 'lookup.'+ thetable + '.' + thefield; //Creates the lookup or reference field ID.
var thetarget = thetable + '.' + thefield;
var url = "&amp;sysparm_query=company=" + g_form.getValue('company');
var refurl = reflistOpenUrl(thetarget, thetarget, thefield, 'sys_user', 'null', 'false', '');
var refurlquery = refurl + url;
popupOpenStandard(refurlquery, 'lookup');
}
</script>
</j:jelly>
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2016 09:15 PM
Yes, the only way to achieve this requirement is through a UI macro, which will show an extra button on the right of the reference qualifier button.
If you name your UI Macro my_company_users, then your called ID field must have ref_contributions=my_company_users
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2016 09:34 PM
This is the dictionary attribute of 'caller_id' field
This is how the Incident form looks like after selecting a company. No new buttons next to 'caller_id'/Contact field
This is the UI macro - my_company_users that I am using.
Am I missing something here Paul ?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2016 09:45 PM
I think line 3 has to align with the name of the UI Macro:
<j:set var="jvar_n" value="my_company_users_${ref}"/>
Unfortunately I don't have my instance with the code for this anymore so I am just guessing at this stage 😕
"/>
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2016 09:55 PM
Nope, still the same , btw do I need to add any images or something ? or is this browser dependent, I am using chrome now...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-18-2016 10:18 PM
I was able to get this working in my developer instance.
UI Macro my_company_users
<?xml version="1.0" encoding="utf-8" ?>
<j:jelly trim="false" xmlns:j="jelly:core" xmlns:g="glide" xmlns:j2="null" xmlns:g2="null">
<j:set var="jvar_n" value="my_company_users_${ref}"/>
<g:reference_decoration id="${jvar_n}" field="${ref}"
onclick="filterUserByCompany(); "
title="${gs.getMessage('My Companies Users')}" image="images/icons/tasks.gifx"/>
<script>
function filterUserByCompany() {
var thefield = 'caller_id'; //Identify the reference field this should be attached to.
var thetable = 'sys_user'; //This gets whatever table you are on.
var lookupfield = 'lookup.'+ thetable + '.' + thefield; //Creates the lookup or reference field ID.
var thetarget = thetable + '.' + thefield;
var url = "&amp;sysparm_query=company=" + g_form.getValue('company');
var refurl = reflistOpenUrl(thetarget, thetarget, thefield, 'sys_user', 'null', 'false', '');
var refurlquery = refurl + url;
popupOpenStandard(refurlquery, 'lookup');
}
</script>
</j:jelly>
Dictionary Entry caller_id
Solution:
CLicking the exclamation button shows the popup.
P.S.
The company field must be exposed on the form.
ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022