Reference Variable Filtered by Group

Laurie Marlowe1
Kilo Sage

Hi,

I have a reference variable called "Project Lead".   I want to only show members of the Physical Security group in this field.   From what I've read it seems a reference qualifier should be used.   After reading some articles I tried entering the following:

Reference qualifier:   javascript:new GetGroupMember().getMember(bb3fc3056f68e10006a8f00dba3ee483)

I'm passing the sys_id of the Physical Security group.

Script Include:

var GetGroupMember= Class.create();

GetGroupMember.prototype = {

getMember : function(bb3fc3056f68e10006a8f00dba3ee483)

{

   

      var user_array = [];

      var getMembers = new GlideRecord('sys_user_grmember');

      getMembers.addQuery('group',bb3fc3056f68e10006a8f00dba3ee483);

      getMembers.query();

      while(getMembers.next())

              {

                user_array.push(getMembers.getValue('users'));

      }

      return 'sys_idIN' + user_array.toString();

}

};

What happens is I get the entire sys_user table unfiltered.   How do I just get the members of the Physical Security group so I can select one of them?

Any help is appreciated, as I am new to scripting.

Thanks,

Laurie

1 ACCEPTED SOLUTION

Please see my other reply as it will correct this situation.


View solution in original post

36 REPLIES 36

Laurie Marlowe1
Kilo Sage

So, one more question....now I want to be able to pass a variable for a group name to the script include so I can use it for any group, not just the one coded in the script. I tried modifying the reference qualifier and script include (numerous times because I don't know how to code this properly).  



Here is the reference qualifier:   javascript:new GetGroupMember().getMember(group_sys_id);



Here is the script:



var WEGetGroupMember= Class.create();




WEGetGroupMember.prototype = {



  getMember : function(group_name)


{


  //var x = 'bb3fc3056f68e10006a8f00dba3ee483'; //Physical Security group sys_id


  var x = this.group_name;


      var user_array = [];


      var getMembers = new GlideRecord('sys_user_grmember');


      getMembers.addQuery('group',x);


      getMembers.query();


      while(getMembers.next())


              {


                user_array.push(getMembers.user + '');


      }


      return 'sys_idIN' + user_array.toString();


}


};



Any help would be greatly appreciated!   I'm going to Knowledge15 and taking as much scripting and debugging as I can.


You are definitely on the right track and very good considering you haven't had any scripting classes!   Honestly it is good that you are pulling up your sleeves with scripting prior to going to training because you will have context going into it versus not 100% following it not having the background.   You will have many Ah Ha moments at K15 as you are learning scripting where things will make better sense.



Again just a few syntax errors, but here is an updated script for your Script Include:


var WEGetGroupMember= Class.create();


WEGetGroupMember.prototype = {


      getMember : function(group_name) {


              var user_array = [];


              var getMembers = new GlideRecord('sys_user_grmember');


              getMembers.addQuery('group', group_name);


              getMembers.query();


              while(getMembers.next()) {


                      user_array.push(getMembers.user + '');


              }


              return 'sys_idIN' + user_array.toString();


      }


};



You had previously had this.group_name and I removed the "this".   "This" is only needed where you have one script include function leveraging variables from another.   So since this is all in one function you don't need it.   I also removed the declaration of the 'x' variable because you really don't need it.



Now in your reference qualifier you need to pass it.   With your very first post you were heading there, but forgot the quotes around the sys_id string.   But here we will just grab the assignment group from the current record:


javascript:new GetGroupMember().getMember(current.assignment_group);


It works!   I have to fields on my catalog form; each one is looking for members of different groups (one is assigning who the Analyst is, and the other assigning who the Installer is).   So I just corrected the syntax on the variables for each reference qualifier to pass the sys_id of the different groups.   THIS IS SO COOL!!!   Thank you, Michael, for all your help!


Awesome great to hear this is working for you.   Very happy to help.   At K15 there will be scripting classes the weekend before the conference starts if interested and there should be labs on scripting as well.   Have a good one.


I'm already signed up for the pre-conference scripting class!   Hoping there is room in the scripting 201 & 301 classes, too.   Cheers!