The CreatorCon Call for Content is officially open! Get started here.

Advanced reference qualifier on Variable set

chrisp1
Mega Sage

In the Service catalogue I have a variable set containing a number of reference fields. These include department_auth_only and auth_manager_auth_only, i'm trying to restrict the selection options of the authoriser list dependent on the department selected.

I know I need to use advance refernce qualifiers for this and I've tried ammending a script from another post as below, however it is not working for me.

Any advice would be gretly appreciated!

currently department references cmn_department

authoriser references sys_user with reference qualifier javascript:u_getDepartmentUsers()

I have created this Script Include

function u_getDepartmentUsers(){

  var usrsList = '';

  var dept = current.variables.department_auth_only;

  var usr = new GlideRecord('sys_user');

  usr.addQuery('department',dept);

  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 department

  return 'sys_idIN' + usrsList;

}

1 ACCEPTED SOLUTION

OK,


ran this as a background script to see exactly what it was doing


I remember the += is a bit funny at times and the original script is, but was returning an array


So, if you add the toString() in the two lines below, you will get the array as you expect


change the addQuery to use a sys_id of one of your groups




var usrsList = '';


          //var dept = current.variables.department_auth_only;


          var usr = new GlideRecord('sys_user');


          usr.addQuery('department','221f79b7c6112284005d646b76ab978c');


          usr.query();


          while(usr.next()) {


gs.print('user id is : ' + usr.sys_id);


          if (usrsList.length > 0) {


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


          usrsList += (',' + usr.sys_id).toString();


          }


          else {


          usrsList = usr.sys_id.toString();


          }


          }


          // return a list of sus_user id's for the selected department


//           return 'sys_idIN' + usrsList;


gs.print('end user list is : '+ usrsList);



Cheers


View solution in original post

11 REPLIES 11

I created this Script Include



function u_getDepartmentUsers(dept){


          var usrsList = '';


          //var dept = current.variables.department_auth_only;


          var usr = new GlideRecord('sys_user');


          usr.addQuery('department',dept);


          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 department


          return 'sys_idIN' + usrsList;


      }



and via a background script, I called it via these and it works



gs.print(new getDepartmentUsers('IT'));


gs.print(new getDepartmentUsers('221f79b7c6112284005d646b76ab978c'));



Initially it did give me the same error you had, so I renamed it to just getDepartmentUsers and that worked


renamed it back and it still works.


thanks. my script include name did not match the function! so now it is working partially, however it is only returning one seemingly random user from the department instead the whole list.


Can you check that you are using "While"   in your glide record and not "If"


-Anurag

the script is still as originally posted,


while(usr.next()) {


if (usrsList.length > 0) {


OK,


ran this as a background script to see exactly what it was doing


I remember the += is a bit funny at times and the original script is, but was returning an array


So, if you add the toString() in the two lines below, you will get the array as you expect


change the addQuery to use a sys_id of one of your groups




var usrsList = '';


          //var dept = current.variables.department_auth_only;


          var usr = new GlideRecord('sys_user');


          usr.addQuery('department','221f79b7c6112284005d646b76ab978c');


          usr.query();


          while(usr.next()) {


gs.print('user id is : ' + usr.sys_id);


          if (usrsList.length > 0) {


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


          usrsList += (',' + usr.sys_id).toString();


          }


          else {


          usrsList = usr.sys_id.toString();


          }


          }


          // return a list of sus_user id's for the selected department


//           return 'sys_idIN' + usrsList;


gs.print('end user list is : '+ usrsList);



Cheers