Add/ Remove a specific role from user profile when user is Added/removed from a particular field.

Puneet4418
Tera Contributor

Hello Developers,

 

Can someone help me with below requirement.

There is a field "Owner" in a custom application form. whenever any user added to that field, a specific role should be automatically added to the user's profile and vice versa when removed from the field. How can we achieve this?

1 ACCEPTED SOLUTION

@Puneet4418 In your business rule, can you update as follows.

 

if(current.u_owner!=previous.u_owner){
    var userRole = new GlideAggregate('sys_user_has_role');
    userRole.addQuery('user',current.u_owner);//replace with your 
    userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
    userRole.addAggregate('COUNT');
    userRole.query();
    if(userRole.next()){
    var roleCount=0;
    roleCount= userRole.getAggregate('COUNT');
    gs.info(roleCount);
        if(roleCount==0){//Only add role if the user doesn't have it
            var glideRole = new GlideRecord('sys_user_has_role');
            glideRole.initialize();
            glideRole.user=current.u_owner;
            glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
            glideRole.state='active';
            glideRole.insert();

            if(previous.u_owner!=''){//Code to remove the role from previous owner.
                var removeRole = new GlideRecord('sys_user_has_role');
                removeRole.addQuery('user',previous.u_owner);
                removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
               gs.info('Previous owner '+previous.u_owner);
                if(removeRole.next()){
                 gs.info('Deleting the record '+ removeRole.getValue('sys_id'));
                    removeRole.deleteRecord();
                }
            }

        }
    }
}

 

Can you check if both the info logs are printed and what values do they print.

 

View solution in original post

10 REPLIES 10

Sandeep Rajput
Tera Patron
Tera Patron

@Puneet4418 Create an insert/update business rule on your custom table with condition owner changes. Inside the business rule script, add the following code.

 

if(current.u_owner!=previous.u_owner){
    var userRole = new GlideAggregate('sys_user_has_role');
    userRole.addQuery('user',current.u_owner);//replace with your 
    userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
    userRole.addAggregate('COUNT');
    userRole.query();
    if(userRole.next()){
    var roleCount=0;
    roleCount= userRole.getAggregate('COUNT');
    gs.print(roleCount);
        if(roleCount==0){//Only add role if the user doesn't have it
            var glideRole = new GlideRecord('sys_user_has_role');
            glideRole.initialize();
            glideRole.user=current.u_owner;
            glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
            glideRole.state='active';
            glideRole.insert();

            if(previous.u_owner!=''){//Code to remove the role from previous owner.
                var removeRole = new GlideRecord('sys_user_has_role');
                removeRole.addQuery('user',previous.u_owner);
                removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
                if(removeRole.next()){
                    removeRole.deleteRecord();
                }
            }

        }
    }
}

Hope this helps.

@Sandeep Rajput Thanks for your help.

The role addition part is working fine. However, when I am removing user from the owner field, it is not removing that particular role from user's profile.

@Puneet4418 In your business rule, can you update as follows.

 

if(current.u_owner!=previous.u_owner){
    var userRole = new GlideAggregate('sys_user_has_role');
    userRole.addQuery('user',current.u_owner);//replace with your 
    userRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Add your role sys_id here
    userRole.addAggregate('COUNT');
    userRole.query();
    if(userRole.next()){
    var roleCount=0;
    roleCount= userRole.getAggregate('COUNT');
    gs.info(roleCount);
        if(roleCount==0){//Only add role if the user doesn't have it
            var glideRole = new GlideRecord('sys_user_has_role');
            glideRole.initialize();
            glideRole.user=current.u_owner;
            glideRole.role='b2d8f7130a0a0baa5bf52498ecaadeb4'; //replace with your role sys_id
            glideRole.state='active';
            glideRole.insert();

            if(previous.u_owner!=''){//Code to remove the role from previous owner.
                var removeRole = new GlideRecord('sys_user_has_role');
                removeRole.addQuery('user',previous.u_owner);
                removeRole.addQuery('role','b2d8f7130a0a0baa5bf52498ecaadeb4');//Replace with you role sys_id
               gs.info('Previous owner '+previous.u_owner);
                if(removeRole.next()){
                 gs.info('Deleting the record '+ removeRole.getValue('sys_id'));
                    removeRole.deleteRecord();
                }
            }

        }
    }
}

 

Can you check if both the info logs are printed and what values do they print.

 

@Sandeep Rajput tried but no info logs are printed.