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

Allen Andreas
Administrator
Administrator

Hi,

Basically, if the choice is to remove, then you'd query sys_group_has_role for that specific group sys_id and then locate the role (through type? they'd still select type to remove right?)...and do role.deleteRecord(); that's it.

So if you need to build out a javascript switch or something, you can, but they'd need to supply the role/type to remove...

Please mark reply as Helpful/Correct, thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Hello,

I said the exact same thing before the other user, but goodness...sorry I didn't write the code specifically for you, haha...figured you could at least do that part...I include everything else.

Take care!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

vkachineni
Kilo Sage
Kilo Sage

var role = new GlideRecord('sys_group_has_role');
role.addQuery('role_id', '<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

This didn't work. Wouldn't I also need to target the role of that specific group? Wouldn't that need to be included in the query??