Is there a way to hide/show a variable based on the current user's group?

jonb
Tera Contributor

I am needing to hide a variable (that is part of a variable set), unless the current user is a member of a certain group... is this possible?

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Jon,



I finally figured this out, as it was bugging me. One of the things that helps when troubleshooting is using 'Scripts->Background' (you need to have elevated security). I ran a bunch of script tests in Background and realized you can't just pass a string to 'isMemberOf()', it needs to be an object.



As an example, this does not work:



var user = 'fd5c7b034f4c020093bd7d218110c7ee';


if (user.isMemberOf('Service Desk')) {


gs.print(true);


} else {


gs.print(false);


}



It always evaluates to false.



This does work, though:



var ourUser = gs.getUser().getUserByID('fd5c7b034f4c020093bd7d218110c7ee');


if (ourUser.isMemberOf('Service Desk')) {


gs.print(true);


} else {


gs.print(false);


}



This evaluates to true.



So, I modified the script include:



isGrpMember : function() {


  var userID = this.getParameter('sysparm_userID');


  var groupName = this.getParameter('sysparm_groupName');


  var thisUser = gs.getUser().getUserByID(userID);


  //gs.log("TM ===> " + thisUser + " | " + groupName, "Q: isGrpMember Ajax");


  if (thisUser.isMemberOf(groupName)) {


  return true;


  } else {


  return false;


  }



  },



Client script:



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('MyAjaxUtils');


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


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


  ga.addParam('sysparm_groupName', 'Service Desk');


  ga.getXML(hideField);


   


  // Callback function to process the response returned from the server


  function hideField(response) {


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


  alert(answer);


  if (answer == 'false') {


  g_form.setDisplay('field4', false);


  }



  }



}



This worked for me on a test catalogue item with some fields. Hopefully this works for you as well. I added the 'sysparm_groupName' to make it a little more re-usable; instead of specifying the group in the script include, you can reuse the function in multiple client scripts with different groups passed to the script.



I found this article which had a similar example which someone noted did not work:



ismemberOf question



Anyways, hope that helps!



Cheers,



Tim


View solution in original post

11 REPLIES 11

Community Alums
Not applicable

Jon,



I finally figured this out, as it was bugging me. One of the things that helps when troubleshooting is using 'Scripts->Background' (you need to have elevated security). I ran a bunch of script tests in Background and realized you can't just pass a string to 'isMemberOf()', it needs to be an object.



As an example, this does not work:



var user = 'fd5c7b034f4c020093bd7d218110c7ee';


if (user.isMemberOf('Service Desk')) {


gs.print(true);


} else {


gs.print(false);


}



It always evaluates to false.



This does work, though:



var ourUser = gs.getUser().getUserByID('fd5c7b034f4c020093bd7d218110c7ee');


if (ourUser.isMemberOf('Service Desk')) {


gs.print(true);


} else {


gs.print(false);


}



This evaluates to true.



So, I modified the script include:



isGrpMember : function() {


  var userID = this.getParameter('sysparm_userID');


  var groupName = this.getParameter('sysparm_groupName');


  var thisUser = gs.getUser().getUserByID(userID);


  //gs.log("TM ===> " + thisUser + " | " + groupName, "Q: isGrpMember Ajax");


  if (thisUser.isMemberOf(groupName)) {


  return true;


  } else {


  return false;


  }



  },



Client script:



function onLoad() {


  //Type appropriate comment here, and begin script below


  var ga = new GlideAjax('MyAjaxUtils');


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


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


  ga.addParam('sysparm_groupName', 'Service Desk');


  ga.getXML(hideField);


   


  // Callback function to process the response returned from the server


  function hideField(response) {


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


  alert(answer);


  if (answer == 'false') {


  g_form.setDisplay('field4', false);


  }



  }



}



This worked for me on a test catalogue item with some fields. Hopefully this works for you as well. I added the 'sysparm_groupName' to make it a little more re-usable; instead of specifying the group in the script include, you can reuse the function in multiple client scripts with different groups passed to the script.



I found this article which had a similar example which someone noted did not work:



ismemberOf question



Anyways, hope that helps!



Cheers,



Tim


This worked perfectly!!! Thank you!!!