get group based on role from sys_group_has_role table

shipra
Giga Contributor

Hi All,

i have a client script which passes role to script include in order to get all the group names of the selected role from sys_group_has_role. In sys_group_has_role, the role field is again reference to another table i.e., sys_user_group.

Kindly suggest.

Thanks!!

1 ACCEPTED SOLUTION

Thank you. I think I understand the issue now. Per your original requirements, you wanted to pass it a role and get back a list of groups that use that role. You are passing it the sys_id of the sys_group_has_role record, not the role itself.



Change the dictionary entry on that field to reference Role (sys_user_role) and use the following scripts and you will have better luck.



/******* script include *********/


var returnRole = Class.create();


returnRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getRole: function(){


  gs.log('>>>DEBUG: returnRole() started...');


  var list = [];


  var rol = this.getParameter('sysparm_role');


  gs.log(rol,'shipra0');


  var grGroup = new GlideRecord('sys_group_has_role');


  grGroup.addQuery('role', rol);


  grGroup.query();


  while (grGroup.next()) {


  list.push(grGroup.group.getDisplayValue());


  }


  return list.join(',');


  },



  type: 'returnRole'


});



/******** client script *********/


function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  if (isLoading || newValue === '') {


  return;


  }


  var rol=g_form.getValue('u_user_role');


  alert('Calling getRole... ' + rol);


  var ga = new GlideAjax('returnRole');


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


  ga.addParam('sysparm_role',rol);


  ga.getXML(AsyncCall);


}


function AsyncCall(response) {


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


  alert(answer);


}


View solution in original post

14 REPLIES 14

Chuck Tomasi
Tera Patron

Can you share what code you have at the moment? This is completely do-able.



Just so I'm clear on the requirements - you are passing a role and want to get back a list of groups with that role, correct? Are you looking for sys_ids, display values (names), or both?


Hi Chuck,



Below is the client script:



function onChange(control, oldValue, newValue, isLoading, isTemplate) {


  alert('Hi inside setrole1');


  if (isLoading || newValue === '') {


  return;


  }


  var rol=g_form.getValue('u_user_role');


  alert('Hi inside setrole'+rol);


  var ga = new GlideAjax('returnRole');


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


  ga.addParam('sysparm_role',rol);


  ga.getXML(AsyncCall);


}


function AsyncCall(response) {


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


  alert(answer);


}



And this is the script include:



var returnRole = Class.create();


returnRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getRole: function(){


  var rol=this.getParameter('sysparm_role');


  gs.log(rol,'shipra0');


  var grGroup=new GlideRecord('sys_group_has_role');


  grGroup.addQuery('role', rol);


  grGroup.query();


  var grGroupName=new GlideRecord('sys_user_group');


  gs.log(grGroup,'shipra1');


  var arr='';


  gs.log('1','shipra2');


  while(grGroup.next()){


  grGroupName.addQuery('sys_id', grGroup.sys_id);


  grGroupName.query();


  return true;


  }


  return false;


  },



  type: 'returnRole'


});



Thanks.


Hi Shipra,



What is the intent of the script include? It looks like you are about to build an array, then you turn around and return a true/false. What is the client script expecting?



Reference:


Dot-Walking - Servicenow Wiki


Hi Chuck,



You are correct i did built an array to pass all the group names related to the role selected.



But the array was generating error: org.mozilla.javascript.NativeArrat@1b065



I want to get save all the group names which has the selected role in an array and pass it to the client script in order to add users to those groups.




Kindly let me any appropriate approach to achieve this.



Thanks!!