add/remove user to new group "kgb" if user add/remove from the groups like "gfd, fgd,dfg,dfg"

rajputs
Tera Contributor
How can we do this business rules script by flow designer. and how can we do this dynamic way
 
use case: if the user is added from the one of the group like "Application Development,Analytics Settings Managers,App Engine Admins" then that user should be added in the other new group "Hardware". and if we remove user from the group like "Application Development,Analytics Settings Managers,App Engine Admins" then that user should be removed from the new group "Hardware". how can be do this by flow designer in the servicenow.
 
(function executeRule(current, previous /*null when async*/) {
    // Define the groups that will trigger the addition or removal from the Hardware group
    var monitoredGroups = ['Application Development', 'Analytics Settings Managers', 'App Engine Admins', 'App Engine Studio User Limited'];
    var targetGroupName = 'Hardware';
 
    // Get the sys_id of the target group (Hardware)
    var targetGroup = new GlideRecord('sys_user_group');
    targetGroup.addQuery('name', targetGroupName);
    targetGroup.query();
    if (!targetGroup.next()) {
        gs.log('Target group ' + targetGroupName + ' not found.');
        return;
    }
    var targetGroupId = targetGroup.sys_id;
 
    // Function to check if the user is already a member of the target group
    function isUserInGroup(userId, groupId) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group', groupId);
        gr.query();
        return gr.next();
    }
//Function to check if the user is a member of any monitored groups
function isUserInAnyMonitoredGroup(userId, monitoredGroups) {
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group.name', 'IN', monitoredGroups.join(','));
        gr.query();
        return gr.hasNext();
    }
 
    // Function to add user to the target group
    function addUserToGroup(userId, groupId) {
        if (!isUserInGroup(userId, groupId)) {
            var gr = new GlideRecord('sys_user_grmember');
            gr.initialize();
            gr.user = userId;
            gr.group = groupId;
            gr.insert();
            gs.addInfoMessage('User ' + userId + ' added to group ' + targetGroupName + '.');
        }
    }
 
    // Function to remove user from the target group
    function removeUserFromGroup(userId, groupId) {
 
        var gr = new GlideRecord('sys_user_grmember');
        gr.addQuery('user', userId);
        gr.addQuery('group', groupId);
        gr.query();
        while (gr.next()) {
            gr.deleteRecord();
            gs.addInfoMessage('User ' + userId + ' removed from group ' + targetGroupName + '.');
        }
 
    }
 
    // Check if the current group membership change is in the monitored groups
    var group = new GlideRecord('sys_user_group');
    group.get(current.group);
    if (monitoredGroups.indexOf(group.name.toString()) !== -1) {
        if (current.operation() == 'insert') {
            addUserToGroup(current.user, targetGroupId);
        } else if (current.operation() == 'delete') {
if (isUserInAnyMonitoredGroup(current.user, monitoredGroups)) {
            removeUserFromGroup(current.user, targetGroupId);
}
        }
    }
})(current, previous);

 

0 REPLIES 0