Advanced reference qualifier not working as expected

ramak
Giga Expert

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)

1 ACCEPTED SOLUTION

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;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

View solution in original post

33 REPLIES 33

Thanks mate, it is working.



But is there any way to update the filter to re-assign this incident to other company users, but by default the selected company users gets displayed ?



find_real_file.png


Reference qualifier can control only the list of records that can be selected on a reference field.   If you want to populate the field with some value, use a onchange client script.


You could add logic to the function so if the company field is empty, show all users.


In fact, this is much simpler and more efficient:



var IncidentContext = Class.create();


IncidentContext.getCurrentCompanyUsers = function() {


  var comp = current.company;


  if (comp.nil()) {


  return '';


  }


  return 'company=' + current.getValue('company');


}



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

Thanks Paul, but is there any way to update the filter like this below: Which actually shows the name of the company that is selected first and then when the 'affected user' field is selected, the below window pops up with the company name in the filter ? (reason being the business needs the ability to change the company   field from the filter to another company & select that company's users)



find_real_file.png


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;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