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

poyntzj
Kilo Sage

instead of your Script Include having


var dept = current.variables.department_auth_only;


what happens if you pass the dept through via the refrence


javascript:u_getDepartmentUsers(current.variables.department_auth_only);



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;


}



or, you could add gs.log('getDepartmentUsers : dept ' & dept); after the var dept line and see what value shows in the logs.


you can then validate it is firstly there, and then if you are getting a name or a sys_id and if that is what the query is after



function u_getDepartmentUsers(dept){


  var usrsList = '';


  var dept = current.variables.department_auth_only;


  gs.log('getDepartmentUsers : dept : ' & dept);


  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;


}



Cheers


thanks for the suggestions, i'm still getting the full list of sys_users returned, however i am getting the below warning log which is presumably the cause, just not sure how to resolve..



org.mozilla.javascript.EcmaError: "u_getDepartmentUsers" is not defined.


    Caused by error in <refname> at line 1



==>     1: u_getDepartmentUsers();


in your reference qualifier, try this



javascript:new u_getDepartmentUsers()


thanks, no difference unfortunately