Get group membership in record producer script

danielschmidt
Kilo Expert

Hello,

i have got another problem today. We've crerated a record producer for all members of our company.

We added there 2 variable sets for a specific variable to be shown.

In the catalog script of the record producer did we put a onLoad function to change, what Variable should be displayed on behalf of a group membership. If somebody is a member of the group, he should see the 1st variable, if not, he should see the 2nd.

I got a solution for that by creating a role without any permissions, which is granted by the group. So i could use the "hasRole" function. Our system administrator told me today, that we can't set up groups with roles. So i have to modify the script now, to check if the user is a member of the group.

My script so far:

function onLoad() {

var isMember = g_user.hasRole('companyname_rolename');

if (isMember){

g_form.setDisplay('variable2', false);

g_form.setDisplay('variable1', true);

} else {

g_form.setDisplay('variable2', true);

g_form.setDisplay('variable1', false);

}

}

My problem now is, that i can't use the isMemberof function because it is a server script function. How can i get this script do what i want to?

It would be nice if somebody could explain me that. Thank you!

Best /Daniel

1 ACCEPTED SOLUTION

I think that because you are running your script in the HR Scope, the gs.getUser() method is not working. I have seen similar issues when trying to run gs methods when calling them outside the scope of the include. Since we cannot use that method, I just wrote what it does in that function within the script include. Here are the new details for the script include:


var usrUtilsAjax = Class.create();


usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      /**


        * Checks to see if a user is a member of a group


        *


        * @param           {string}   sysparm_grp   The sys_id or name of the group to check


        * @param           {string}   sysparm_usr   The sys_id of the User to check


        * @return         {boolean}   Returns true if the user is a member of the group,


        *                                               otherwise returns false


        */


      chkIsMember: function() {


              var grpID = this.getParameter('sysparm_grp');


              var usrID = this.getParameter('sysparm_usr');


              var memObj = new GlideRecord('sys_user_grmember');


              var qString = 'group=' + grpID + '^user=' + usrID;


              memObj.addEncodedQuery(qString);


              memObj.query();


              return memObj.hasNext();


      },



      type: 'usrUtilsAjax'


});



Hopefully this will work.


View solution in original post

14 REPLIES 14

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hello Daniel,



You are right. isMemberof will work on the server side. So, in this case, you need to make a GlideAjax call and get the output at the client side.
Reference:


How to Write Smart GlideAjax Quickly


Hello Pradeep,



i think i'll read this article and try to get some good javascript tutorials. I'm good at using Visual Basic, but javascript is a bit harder in my opinion. So i need to practise a bit more and get some good books about it.


Anyways, thanks for your link.


ccajohnson
Kilo Sage

1.   Create a Client Callable Script Include:


      Name: usrUtilsAjax


      Client callable: true


      Accessible from: All application scopes


      Script:


var usrUtilsAjax = Class.create();


usrUtilsAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      /**


        * Checks to see if a user is a member of a group


        *


        * @param           {string}   sysparm_grp   The sys_id or name of the group to check


        * @param           {string}   sysparm_usr   The sys_id of the User to check


        * @return         {boolean}   Returns true if the user is a member of the group,


        *                                               otherwise returns false


        */


      chkIsMember: function() {


              var grpID = this.getParameter('sysparm_grp');


              var usrID = this.getParameter('sysparm_usr');


              var result = gs.getUser.getUserByID(usrID).isMemberOf(grpID);


              return result;


      },



      type: 'usrUtilsAjax'


});



2.   Modify your onLoad Catalog Client Script:


function onLoad() {


              var ga = new GlideAjax('usrUtilsAjax');


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


              ga.addParam('sysparm_usr', g_user.userID);


              ga.addParam('sysparm_grp', 'sys_id_of_group');


              ga.getXML(setVisible);


      }


}



function setVisible(response) {


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


      if (answer == 'true') {


              g_form.setDisplay('variable2', false)


              g_form.setDisplay('variable1', true);


      } else {


              g_form.setDisplay('variable2', true)


              g_form.setDisplay('variable1', false);


      }


}



If you have any questions about how to build this, let us know.


Hello Christopher,



thanks for your great work. I tried to implement this into our dev instance. But apparently it doesn't work well. After i impersonate to a user who is member of the group i get an error called "AbstractAjaxProcessor undefined, maybe missing global qualifier". My javascript knowledge isn't that good to debug what i did wrong. I didn't changed anything of your script include, i understand a bit whats happening there. I changed the variable names (obviously that wasn't the correct ones) and removed a "}" on the onLoad step. But it seems there is still a problem. Could you help me here please?



Best, Daniel