Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

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

On using your code its returning nothing.



I did a minor change to see if it works but thats throwing the error.


What type of field is "User role", string or reference? if it's a reference field, what table is it referencing?


Its a reference field. Referencing to sys_group_has_role.


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


}


HI @Chuck Tomasi 

 

Could you please clarify my doubt, if possible.

In the similar requirement, I have created one custom table ABC having one the field role_name (Reference to sys_user_role table) and i have another custom table XYZ in this XYZ table we have few fields called Role(Reference to ABC table), Group_List, 

Here my doubt is : How can we get the list of groups in XYZ table based on selection of Role Field ??
The Role Field is reference to ABC table.