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

Thank you for the clarification. Try this for your script include instead. It returns a comma separated list of group names. This code is untested.



var returnRole = Class.create();


returnRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getRole: function(){


 


  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'


});


Hi Chuck,



Its not working .



I tried to push the values into the array in another way:



arr.push(grGroup.getValue('group'));



But thats again throwing the same org.mozilla.NativeArray error.



Thanks.


Hi Shipra,



Debugging scripts via the community is always a good challenge. Can you provide some details of what it is doing (or not doing)? You've got some debug statements in there (alert, gs.log()), etc. How far is it getting?


Hi chuck,



The array is returning nothing.



Below is the error screenshot:



error.PNG



The log displays sys-id of the role selected properly and every log at each level is executed.



Kindly suggest.


That's very odd because I'm not returning an array. I'm returning a comma separate string of names. Let me do a little checking...