Dynamic reference qualifiers for reference variables

bennyphipps
Giga Expert

Hi,

 

This should be simple enough to answer I hope .

 

I have been playing around with a catalogue item with two variables in our PoC environment.   mirror_user (reference -> sys_user) and shared_group (reference -> sys_user_group).

 

find_real_file.png

 

So for example if John Doe is a member of (GroupA, GroupB, GroupC in sys_usergrmember) I would like the 'shared_group' variable to only show these groups... effectively acting as a dynamic reference qualifier.

 

This is basically to allow our business users to enter a MIRROR user and see what groups they are a member of.

 

I imagine I'll need to query sys_usergrmember to find the groups for John Doe somehow?

 

As a mini workaround I did try having the reference field referring to sys_usergrmember cos you can select what you want based on the filter but the sys_usergrmember refers to sysid..

find_real_file.png

 

So there's a challenge for you SN bods... a nice elegant solution please

4 REPLIES 4

marcguy
ServiceNow Employee
ServiceNow Employee

you will need to call a script include in the 'reference qualifier' field of the group variable.



javascript:backfillAssignmentGroup()



SCRIPT INCLUDE:


function backfillAssignmentGroup() {



  var strQry = 'active=true'; //default filter in case user variable is empty



  var strUser = current.variables.mirror_user; //user chosen in variable


  if (strUser!= '') { //if not empty


  strQry = '^sys_idIN' + _getGroupsIds(strUser); //get the list of groups they are a member of


  }


  return strQry; //return the query


}




function _getGroupsIds(user) {


  var strGroups = '';



  // Get all instances where the user is a change_coordinator for a group.


  var grRole = new GlideRecord('sys_user_grmember');


  grRole.addQuery('user', user);


  grRole.query();



  while (grRole.next()) {


  if (strGroups.length > 0) {


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


  strGroups += ',' + grRole.group;


  } else {


  strGroups = '' + grRole.group;


  }


  }


  return strGroups;


}


Oh ok thanks... that sort of makes sense I suppose...   do I need to make it client callable and what specific name do I need to call teh script include (if that matters).



Quick response



Cheers


Ben


marcguy
ServiceNow Employee
ServiceNow Employee

It shouldn't need to be client callable, no.



I normally call mine the same as the function i.e.backfillAssignmentGroup



and in the ref qual attribute on the variable: javascript:backfillAssignmentGroup();


Ah this works... thanks



SWEET!



Have a nice Bonfire Night