- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 08:23 AM
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);
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 11:06 AM
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();
}
Vinod Kumar Kachineni
Community Rising Star 2022

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 08:44 AM
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!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 11:30 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 08:48 AM
var role = new GlideRecord('sys_group_has_role');
role.addQuery('role_id', '<ROLE_SYS_ID>');
role.query();
while(role.next())
{
role.deleteRecord();
}
Vinod Kumar Kachineni
Community Rising Star 2022
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-03-2019 10:53 AM
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??