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

checking group membership via Client script

bennyphipps
Giga Expert

Hi,

I have a Catalog Item where users can select a group and a list of users.   What I want is to look at that group members and set up an alert (or confirm) that says whether any of the users selected are already in that group

Or I guess I could do this maybe via altering the list collector filter?

My attempt at a client script that makes a GlideAjax call is:

function onSubmit() {

var group = variables.group; // group from reference variable on sys_user_group table

var users = variables.requested_for; //comma seperated list of users from list collector.

     

var ga = new GlideAjax('GroupMember');

ga.addParam('sysparm_name','groupMember');

ga.addParam('sysparm_group',group);

ga.getXML(GroupMemberParse);

function GroupMemberParse(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    alert(answer);

}

   

}

... and the script include is:

var GroupMember = Class.create();

GroupMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    groupMember: function() {

         

            var group = this.getParameter('sysparm_group');

            //return "Hello " + this.getParameter('sysparm_user_name') + "!";

            return _getusers(group);

    },

    _getusers: function(user) { // this function is not client callable        

  var strUsers = '';

  var grRole = new GlideRecord('sys_user_grmember');

  grRole.addQuery('user', user);

  grRole.query();

  while (grRole.next()) {

  if (strUsers.length > 0) {

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

  strGroups += ',' + grRole.user;

  } else {

  strGroups = '' + grRole.user;

  }

  }

  return strUsers;

           

    }

});

6 REPLIES 6

Brad Tilton
ServiceNow Employee
ServiceNow Employee

You're passing a group from your client script to the script include and then within your script include into your private _getusers function. It looks like that function is then using the group you passed as a user in the query.


Hi,



I took out the private bit as it seemed to not be working... is this a bad idea as I don't think the class GroupMember.



find_real_file.png



This above code gives me group members (now I just hae to work out another bit (it's all a learning exercise as well you see )



My client script has the output as well which is nice...


find_real_file.png


I tried adding in a retrun false statement if the result of the confirm was false but it didn't work as I'd expected....   I basically DONT want it to submit if the user clicks Cancel on eth confirm message...?



Any ideas?


Hi Ben,



Try this



var x =confirm("your message");


if(x='false')


{


return false;


}


-Anurag

Where abouts do I place thsi though...? Inside the GroupMemberParse function or outside...?   Cos I tired this:


find_real_file.png


and it still submitted the request   I think the return false; statement needs to be outside of teh function... but how do I get access to teh var referenced in the function?