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

Hopefully there is not any other element (UI Policy, Client Script) that is setting the display for this variable. I was able to replicate it on my development instance (but only in the global scope).



You may want to add some alerts into your Catalog Client Script to see what is going on:


function onLoad() {


      var ga = new GlideAjax('global.usrUtilsAjax');


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


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


alert('User ID: ' + g_user.userID);


      ga.addParam('sysparm_grp', '52189daa58664f0049acfc064ad840d2');      


      ga.getXML(setVisible);      


}



function setVisible(response) {


alert('setVisible running');


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


alert('answer= ' + 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);


      }


}



Hopefully you should get an answer back. If not, check the Script include to make sure that it is configured properly:


-     Name: usrUtilsAjax


-     Client callable: true


-     Application: Global


-     Accessible from: All application scopes



I was scratching my head one time because I forgot to check the Client callable checkbox and was getting no results.


Hi Christopher,



thanks for your help (again and again, you're great).


I've set up the alerts into the code. The first alert is returning the sysID of the logged in user. The 2nd alert box is showing up (setVisible running).


But the 3rd one is returning just "answer = null". So i checked the sysID to the user i was impersonating to. It is the correct one. Its not the userID we have on our sys_user list. But getUserByID needs the sysID, am i correct at this point? So there is a problem by getting the correct answer from the script? The User is definitly member of the group i am looking for. And the sysID of the group is definitly the correct one, i did check that twice times.



And yes, i checked the tick on client callable twice times before creating the script include and again now. The name of the script include is "usrUtilsAjax", it is in global scope and accessible from all application scopes.


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.


Yes, it works


I mean, i'm happy with it to understand a bit of the first code you wrote (it took me 1 day, to just read and understand). Could you explain what you're doing there?


I can expect what you're doing at those two lines:


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


              memObj.addEncodedQuery(qString);  


At the first one, you're defining the table you want to run the query on. The second is the filter you build up with an and condition, correct?


after that you're using this string to add it to the actual query and run it after that with this two lines:


              memObj.query();  


              return memObj.hasNext();


But, i can't get the last one. Why do you have to use the last one? Didn't you get an element after running the query?



I'm so glad that you helped me that much. I'm feeling set back to my first days on vba. Thanks for your help.


Good to know it is working. Be sure to mark it as correct so that others with a similar issue can find the answer quickly.



Let me walk through each line of code in the function of the script include:


              var grpID = this.getParameter('sysparm_grp'); //This is the parameter passed from the Client Script


              var usrID = this.getParameter('sysparm_usr'); //This is the parameter passed from the Client Script


              var memObj = new GlideRecord('sys_user_grmember'); //initializes a GlideRecord object on the Group Membership [sys_user_grmember] table


              var qString = 'group=' + grpID + '^user=' + usrID; //Builds a query string to find the group and user in the Group Membership table.


              memObj.addEncodedQuery(qString); //Adds the query string using the addEncodedQuery() method


              memObj.query(); //Runs the query


              return memObj.hasNext(); //uses the hasNext() method to indicate if the query has any found records.



Some helpful documents to refer to:


GlideRecord - Global


GlideRecord - Scoped


AJAX