How to remove role from group in script

madamski
Kilo Contributor

I have built a custom business rule which gets triggered when the field for Type is changed for a group. When a specific type is added, it will give that group a specific role and when the type is removed, it will remove that role.

I've got it working for adding the role when the type is added but can't figure out how to target the role for removal, any ideas? Here is my incomplete code so far...

 

(function executeRule(current, previous /*null when async*/) {
  var config         = new xMattersConfig();
  var log            = new xMattersLogger(config.DEBUGGING, 'xMatters Type & Role Sync');
  var xm_type_sys_id = '<TYPE_SYS_ID>';
  var action         = null;
  var sys_id         = current.sys_id;

  if (current.type.includes(xm_type_sys_id)) {
		// The xM type was just given and previously did not have it...
		action = 'add';
		log.debug('*********************************** SYS ID: ' + sys_id);
	  var role = new GlideRecord('sys_group_has_role');
			  role.initialize();

			  role.group     = sys_id;
			  role.role      = '<ROLE_SYS_ID>';
			  role.inherits  = true;

			  role.insert();
  }
  else if (!current.type.includes(xm_type_sys_id)) {
		// The xM type was just removed but did previously have it...
		action = 'remove';

		var role = new GlideRecord('sys_group_has_role');
				role.addQuery('role_id', )
		log.debug('*********************************** SYS ID: ' + sys_id);
  }

  log.debug('******************** ACTION: ' + action);
})(current, previous);
1 ACCEPTED SOLUTION

Sorry missed a query

 

//Try this please

 

var role = new GlideRecord('sys_group_has_role');
role.addQuery('group', current.sys_id);
role.addQuery('role', '<ROLE_SYS_ID>');
role.query();
while(role.next())
{


role.deleteRecord();
}

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

View solution in original post

7 REPLIES 7

Sorry missed a query

 

//Try this please

 

var role = new GlideRecord('sys_group_has_role');
role.addQuery('group', current.sys_id);
role.addQuery('role', '<ROLE_SYS_ID>');
role.query();
while(role.next())
{


role.deleteRecord();
}

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022

Thanks! Where can I find documentation to know what kind of fields/attributes to target in a given table? The ServiceNow system is really confusing to me. For instance, I had no way of knowing the role/group fields to add queries for, so how would I find that stuff in the future?

Please signup for a personal development instance.

In this instance you are on the group screen. 

the URL specifies the table (sys_user_group) of the record.

Once you know the table name go to the list of tables and look for the table columns.

https://your_instance.service-now.com/nav_to.do?uri=%2Fsys_user_group.do%3Fsys_id%3D0a52d3dcd7011200f2d224837e6103f2%26sysparm_record_target%3Dsys_user_group%26sysparm_record_row%3D1%26sysparm_record_rows%3D34%26sysparm_record_list%3DORDERBYDESCsys_updated_on

 

find_real_file.png

The roles in the group table is 'sys_group_has_role'

Look at the columns in the table to see the complete list of columns.

 

Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Vinod Kumar Kachineni
Community Rising Star 2022