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

Hello Chris,



could you help me here please?


Are you in the Global Scope? This sounds like an error when the Script Include is not in the global scope.


Oh, thats really bad by me. You're right. It was in the hr scope. Ok. I've changed the scope of the script include (created a new one and deleted the old one). But now i get a javascript error in my browser console.



There i get an unhandled exception in GlideAjax:


amb.MessageClient [INFO] >>> connection exists, request satisfied


js_includes_sp.jsx:26732:1


Unhandled exception in GlideAjax.


js_includes_sp.jsx:66006:1


showBrowserErrors/$window.console.error</<


https://servername.host.com/scripts/js_includes_sp.jsx:66006:1


logError


https://servername.host.com/scripts/js_includes_sp.jsx:60280:1


initialize/this.errorCallbackFn


https://servername.host.com/scripts/js_includes_sp.jsx:60293:1


execute/<


https://servername.host.com/scripts/js_includes_sp.jsx:60369:1


processQueue


https://servername.host.com/scripts/js_includes_sp.jsx:14007:18


scheduleProcessQueue/<


https://servername.host.com/scripts/js_includes_sp.jsx:14022:23


$eval


https://servername.host.com/scripts/js_includes_sp.jsx:14575:8


$digest


https://servername.host.com/scripts/js_includes_sp.jsx:14483:1


$apply


https://servername.host.com/scripts/js_includes_sp.jsx:14602:1


done


https://servername.host.com/scripts/js_includes_sp.jsx:11225:26


completeRequest


https://servername.host.com/scripts/js_includes_sp.jsx:11342:1


requestLoaded



I'm feeling so dumb idk -.-


If your form is in the HR Scope, then you need to call the Script Include from the Global Scope. Change the line of your Catalog Client script from:


              var ga = new GlideAjax('usrUtilsAjax');



To be:


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


Ok, thanks. There is no error now.


To let me learn about that: If I try to get functions from another scope, i have to get the hole reference of it into the line. If i don't do that, the script will try to find it in the actual scope of the application i called it from. So, for further questions, it's essential to give the information, in which scope something is saved in. Thanks for that.



My Record producer catalog script code is:


function onLoad() {  


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


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


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


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


              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);


}


}  


the bad news is, that there is always variable2 displayed. no matter of the group. the sysID is the correct one.