- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 07:50 AM - edited 04-15-2025 08:22 AM
Hello everyone!
Hope you are all doing well. I have a question regaring the field "Type" from the table sys_user_group. Basically we have a business rule running after a deletion. I'll leave the code here (EDIT):
(function executeRule(current, previous /*null when async*/ ) {
// Get the group name from the deleted group-role record
var groupName = current.group.name.toString();
var fulfillerType = false;
var approverType = false;
// Collect all remaining roles for the group
var roleGR = new GlideRecord('sys_group_has_role');
roleGR.addQuery('group', current.group.toString());
roleGR.query();
var remainingRoleIds = [];
while (roleGR.next()) {
remainingRoleIds.push(roleGR.role.name.toString()); // Use sys_id for proper license_role lookup
}
gs.addInfoMessage(remainingRoleIds.join(',').toString());
// Now check license_role table for any roles that are of type 'fulfiller' or 'approver'
if (remainingRoleIds.length > 0) {
var licenseGR = new GlideRecord('license_role');
//licenseGR.addQuery('name', 'IN', remainingRoleIds.join(',').toString());
licenseGR.addEncodedQuery('license_role_typeSTARTSWITHFulfiller^ORlicense_role_typeSTARTSWITHApprover^nameIN' + remainingRoleIds.join(',').toString());
licenseGR.query();
if (licenseGR.hasNext()) {
gs.addInfoMessage('Group "' + groupName + '" still has fulfiller or approver roles assigned.');
while (licenseGR.next()) {
var roleType = licenseGR.license_role_type.toString();
var roleName = licenseGR.name.toString();
gs.addInfoMessage(roleName + ': ' + roleType);
if (roleType == 'fulfiller') {
gs.addInfoMessage('Role: ' + roleName + ' - Fulfiller');
fulfillerType = true;
} else if (roleType == 'approver') {
gs.addInfoMessage('Role: ' + roleName + ' - Approver');
approverType = true;
}
}
} else {
gs.info('Group "' + groupName + '" no longer has any fulfiller or approver roles.');
}
}
var groupGR = new GlideRecord('sys_user_group');
if (fulfillerType && approverType) {
if (groupGR.get(current.group.toString())) {
groupTypes = ['9c774e5dfb7ce61468bcfcbaaeefdc6a', '96c782ddfb7ce61468bcfcbaaeefdcd5']; // Sys_id Fulfiller Type & Approver Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('UPDATED BOTH');
}
} else if (fulfillerType && !approverType) {
if (groupGR.get(current.group.toString())) {
groupTypes = ['9c774e5dfb7ce61468bcfcbaaeefdc6a']; // Sys_id Fulfiller Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('UPDATED FULFILLER');
}
} else if (approverType && !fulfillerType) {
if (groupGR.get(current.group.toString())) {
groupTypes = ['96c782ddfb7ce61468bcfcbaaeefdcd5']; // Sys_id Approver Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('UPDATED APPROVER');
}
} else {
groupTypes = ['']; // Sys_id Approver Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('REMOVED ALL ROLES');
}
})(current, previous);
Everything on this Business Rule runs well, except the else. When we don't find any roles that are from the type "Approver" or "Fulfiller" we basically want to empty the field "Type" on sys_user_group. On any of the conditions it fills the field with the right type, if there's one of each or more left it gets both types on the field, if it finds atleast one from both it also fills the field well. Problem is if it doesn't find any types on the form or if there are no roles associated at all. It enters our else, shows the info message we have for debug but doesn't clear the value. How can we make this work?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 08:51 AM
try this
(function executeRule(current, previous /*null when async*/ ) {
// Get the group name from the deleted group-role record
var groupName = current.group.name.toString();
var fulfillerType = false;
var approverType = false;
// Collect all remaining roles for the group
var roleGR = new GlideRecord('sys_group_has_role');
roleGR.addQuery('group', current.group.toString());
roleGR.query();
var remainingRoleIds = [];
while (roleGR.next()) {
remainingRoleIds.push(roleGR.role.name.toString()); // Use sys_id for proper license_role lookup
}
gs.addInfoMessage(remainingRoleIds.join(',').toString());
// Now check license_role table for any roles that are of type 'fulfiller' or 'approver'
if (remainingRoleIds.length > 0) {
var licenseGR = new GlideRecord('license_role');
licenseGR.addEncodedQuery('license_role_typeSTARTSWITHFulfiller^ORlicense_role_typeSTARTSWITHApprover^nameIN' + remainingRoleIds.join(',').toString());
licenseGR.query();
if (licenseGR.hasNext()) {
gs.addInfoMessage('Group "' + groupName + '" still has fulfiller or approver roles assigned.');
while (licenseGR.next()) {
var roleType = licenseGR.license_role_type.toString();
var roleName = licenseGR.name.toString();
gs.addInfoMessage(roleName + ': ' + roleType);
if (roleType == 'fulfiller') {
gs.addInfoMessage('Role: ' + roleName + ' - Fulfiller');
fulfillerType = true;
} else if (roleType == 'approver') {
gs.addInfoMessage('Role: ' + roleName + ' - Approver');
approverType = true;
}
}
} else {
gs.info('Group "' + groupName + '" no longer has any fulfiller or approver roles.');
}
}
var groupGR = new GlideRecord('sys_user_group');
if (groupGR.get(current.group.toString())) {
if (fulfillerType && approverType) {
groupGR.type = '9c774e5dfb7ce61468bcfcbaaeefdc6a,96c782ddfb7ce61468bcfcbaaeefdcd5'; // Sys_id Fulfiller Type & Approver Type
groupGR.update();
gs.addInfoMessage('UPDATED BOTH');
} else if (fulfillerType && !approverType) {
groupGR.type = '9c774e5dfb7ce61468bcfcbaaeefdc6a'; // Sys_id Fulfiller Type
groupGR.update();
gs.addInfoMessage('UPDATED FULFILLER');
} else if (approverType && !fulfillerType) {
groupGR.type = '96c782ddfb7ce61468bcfcbaaeefdcd5'; // Sys_id Approver Type
groupGR.update();
gs.addInfoMessage('UPDATED APPROVER');
} else {
groupGR.type = ''; // Clear the type field
groupGR.update();
gs.addInfoMessage('REMOVED ALL ROLES');
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 08:00 AM
try this
Changes done
- Query the sys_user_group record: Ensure the groupGR object is queried for the current group before updating the type field.
- Clear the type field: Set groupGR.type to an empty string to clear the field when no roles of type "Approver" or "Fulfiller" are found.
(function executeRule(current, previous /*null when async*/) {
// Get the group name from the deleted group-role record
var groupName = current.group.name.toString();
var fulfillerType = false;
var approverType = false;
// Collect all remaining roles for the group
var roleGR = new GlideRecord('sys_group_has_role');
roleGR.addQuery('group', current.group.toString());
roleGR.query();
var remainingRoleIds = [];
while (roleGR.next()) {
remainingRoleIds.push(roleGR.role.name.toString()); // Use sys_id for proper license_role lookup
}
gs.addInfoMessage(remainingRoleIds.join(',').toString());
// Now check license_role table for any roles that are of type 'fulfiller' or 'approver'
if (remainingRoleIds.length > 0) {
var licenseGR = new GlideRecord('license_role');
licenseGR.addEncodedQuery('license_role_typeSTARTSWITHFulfiller^ORlicense_role_typeSTARTSWITHApprover^nameIN' + remainingRoleIds.join(',').toString());
licenseGR.query();
if (licenseGR.hasNext()) {
gs.addInfoMessage('Group "' + groupName + '" still has fulfiller or approver roles assigned.');
while (licenseGR.next()) {
var roleType = licenseGR.license_role_type.toString();
var roleName = licenseGR.name.toString();
gs.addInfoMessage(roleName + ': ' + roleType);
if (roleType == 'fulfiller') {
gs.addInfoMessage('Role: ' + roleName + ' - Fulfiller');
fulfillerType = true;
} else if (roleType == 'approver') {
gs.addInfoMessage('Role: ' + roleName + ' - Approver');
approverType = true;
}
}
} else {
gs.info('Group "' + groupName + '" no longer has any fulfiller or approver roles.');
}
}
// Update the group type field
var groupGR = new GlideRecord('sys_user_group');
if (groupGR.get(current.group.toString())) {
if (fulfillerType && roleGR.getRowCount() <= 1 || approverType && roleGR.getRowCount() <= 1 || !approverType && !fulfillerType) {
groupGR.type = ''; // Clear the type field
groupGR.update();
gs.addInfoMessage('REMOVED ALL ROLES');
}
}
})(current, previous);
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 08:19 AM - edited 04-15-2025 08:20 AM
Hi @Ankur Bawiskar , I was actually mistaken when sending this snipped. This is from another business rule, the one we have working is this one:
(function executeRule(current, previous /*null when async*/ ) {
// Get the group name from the deleted group-role record
var groupName = current.group.name.toString();
var fulfillerType = false;
var approverType = false;
// Collect all remaining roles for the group
var roleGR = new GlideRecord('sys_group_has_role');
roleGR.addQuery('group', current.group.toString());
roleGR.query();
var remainingRoleIds = [];
while (roleGR.next()) {
remainingRoleIds.push(roleGR.role.name.toString()); // Use sys_id for proper license_role lookup
}
gs.addInfoMessage(remainingRoleIds.join(',').toString());
// Now check license_role table for any roles that are of type 'fulfiller' or 'approver'
if (remainingRoleIds.length > 0) {
var licenseGR = new GlideRecord('license_role');
//licenseGR.addQuery('name', 'IN', remainingRoleIds.join(',').toString());
licenseGR.addEncodedQuery('license_role_typeSTARTSWITHFulfiller^ORlicense_role_typeSTARTSWITHApprover^nameIN' + remainingRoleIds.join(',').toString());
licenseGR.query();
if (licenseGR.hasNext()) {
gs.addInfoMessage('Group "' + groupName + '" still has fulfiller or approver roles assigned.');
while (licenseGR.next()) {
var roleType = licenseGR.license_role_type.toString();
var roleName = licenseGR.name.toString();
gs.addInfoMessage(roleName + ': ' + roleType);
if (roleType == 'fulfiller') {
gs.addInfoMessage('Role: ' + roleName + ' - Fulfiller');
fulfillerType = true;
} else if (roleType == 'approver') {
gs.addInfoMessage('Role: ' + roleName + ' - Approver');
approverType = true;
}
}
} else {
gs.info('Group "' + groupName + '" no longer has any fulfiller or approver roles.');
}
}
var groupGR = new GlideRecord('sys_user_group');
if (fulfillerType && approverType) {
if (groupGR.get(current.group.toString())) {
groupTypes = ['9c774e5dfb7ce61468bcfcbaaeefdc6a', '96c782ddfb7ce61468bcfcbaaeefdcd5']; // Sys_id Fulfiller Type & Approver Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('UPDATED BOTH');
}
} else if (fulfillerType && !approverType) {
if (groupGR.get(current.group.toString())) {
groupTypes = ['9c774e5dfb7ce61468bcfcbaaeefdc6a']; // Sys_id Fulfiller Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('UPDATED FULFILLER');
}
} else if (approverType && !fulfillerType) {
if (groupGR.get(current.group.toString())) {
groupTypes = ['96c782ddfb7ce61468bcfcbaaeefdcd5']; // Sys_id Approver Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('UPDATED APPROVER');
}
} else {
groupTypes = ['']; // Sys_id Approver Type
groupGR.type = groupTypes.join(',');
groupGR.update();
gs.addInfoMessage('REMOVED ALL ROLES');
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 08:36 AM
so is this script working as expected?
If yes then close the thread by marking your own response as correct.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2025 08:39 AM
No @Ankur Bawiskar. Everything here runs correct until it enters the last else. It shows the InfoMessage present inside, but it doesn't clear the field value as it's supposed