We've updated the ServiceNow Community Code of Conduct, adding guidelines around AI usage, professionalism, and content violations. Read more

Creat BR for remove member from group if he is not manager of appliction

nadavt182355205
Tera Contributor

Hello every one,

i need to make a BR that is remove members in a group that i created called "IT URS".

It triggers when a field  called 'managed_by' on the cmdb_ci_appl table is change and it is referance to the user table.

What the BR need to do is when the 'managed_by' field is change it takes the previous user and check if he managed another app from the cmdb_ci_appl table, if no he need to remove him from a group called IT URS.

 

thanks for the help

1 ACCEPTED SOLUTION

Bhavya11
Kilo Patron

Hi @nadavt182355205 ,

 

Please write Business rule on cmdb_ci_appl table it will run after update 

filter condition : manged_by changes

Bhavya11_0-1730978139369.png

 

condition : current.managed_by != previous.managed_by 

 

in script:

Bhavya11_1-1730978168981.png

 

(function executeRule(current, previous /*null when async*/ ) {

    var previousUser = previous.managed_by;


    if (previousUser) {
        var appCheck = new GlideRecord('cmdb_ci_appl');
        appCheck.addQuery('managed_by', previousUser);
        appCheck.addQuery('sys_id', '!=', current.sys_id); // Exclude the current record
        appCheck.query();

        if (!appCheck.hasNext()) {
            var group = new GlideRecord('sys_user_grmember');
            group.addQuery('group.name', 'IT URS');
            group.addQuery('user', previousUser);
            group.query();

            if (group.next()) {
                group.deleteRecord(); 
               
            }
        }
    }

})(current, previous);

 

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

Thanks,

BK

If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,
BK

View solution in original post

2 REPLIES 2

Bhavya11
Kilo Patron

Hi @nadavt182355205 ,

 

Please write Business rule on cmdb_ci_appl table it will run after update 

filter condition : manged_by changes

Bhavya11_0-1730978139369.png

 

condition : current.managed_by != previous.managed_by 

 

in script:

Bhavya11_1-1730978168981.png

 

(function executeRule(current, previous /*null when async*/ ) {

    var previousUser = previous.managed_by;


    if (previousUser) {
        var appCheck = new GlideRecord('cmdb_ci_appl');
        appCheck.addQuery('managed_by', previousUser);
        appCheck.addQuery('sys_id', '!=', current.sys_id); // Exclude the current record
        appCheck.query();

        if (!appCheck.hasNext()) {
            var group = new GlideRecord('sys_user_grmember');
            group.addQuery('group.name', 'IT URS');
            group.addQuery('user', previousUser);
            group.query();

            if (group.next()) {
                group.deleteRecord(); 
               
            }
        }
    }

})(current, previous);

 

 

 

Please mark helpful & correct answer if it's really worthy for you.

 

Thanks,

BK

If this information proves useful, kindly mark it as helpful or accepted solution.

Thanks,
BK

SufiyanMurshad
Tera Contributor

Hi @nadavt182355205 ,

you need to add table {cmdb_ci_appl} (or the appropriate table if it's customized).

When : After.

filter condition is select Field as managed_by and trigger when it Changed.

 

BR script:


var previousUser = previous.managed_by;

var appCheck = new GlideRecord('cmdb_ci_appl');
appCheck.addQuery('managed_by', previousUser);
appCheck.query();

 

if(!appCheck.hasNext()) {
var groupGR = new GlideRecord('sys_user_group');
groupGR.addQuery('name', 'IT URS');
groupGR.query();

 

if(groupGR.next()) {
var groupMember = new GlideRecord('sys_user_grmember');
groupMember.addQuery('group', groupGR.sys_id);
groupMember.addQuery('user', previousUser);
groupMember.query();

 

if(groupMember.next()) {
groupMember.deleteRecord();
}
}
}

 

 

 

 

 

 

Please mark helpful & correct answer if it's really worthy for you.