How to remove a user's role when the user is no longer a group manager

ss123
Tera Contributor

Hi everyone!

I would like to ask your assistance, I have this requirement wherein when a user is placed as a Group Manager it will automatically grant an "approver_user" role. However, I would also like to revert once the user is no longer a Group Manager.

 

Currently I am using this BR script to assign an "approver_user" role to a user placed as Group Manager. I would like also like to modify this script to remove the "approver_user" role once the user is no longer a Group Manager.

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

    // Add your code here
    var gr = new GlideRecord("sys_user_has_role");
    gr.addQuery("user", current.manager);
    gr.addQuery("role.name", "approver_user");
    gr.query();
    if (!gr.hasNext()) {
        gr.initialize();
        gr.user = current.manager;
        gr.setDisplayValue("role", "approver_user");
        gr.insert();
    }

})(current, previous);

 

Thank you in advance!! 🙂

1 ACCEPTED SOLUTION

MrMuhammad
Giga Sage

Hey,

There are multiple ways to achieve this.

1. You can achieve this using flow designer without writing the script. [Highly recommended]

Documentation: https://docs.servicenow.com/bundle/quebec-servicenow-platform/page/administer/flow-designer/concept/...

2. Create an After Update Business rule on the Group table.

When to Run: Manager [changes]

Advance:

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

	// Add your code here
	var gr = new GlideRecord("sys_user_has_role");
	gr.addQuery("user", previous.manager);
	gr.addQuery("role.name", "approver_user");
         gr.setLimit(1);
	gr.query();
	if (gr.next()) {
		gr.deleteRecord();
	}

})(current, previous);

Hope that helps!

Regards,

Muhammad 

 

 

 

Regards,
Muhammad

View solution in original post

6 REPLIES 6

@Sabrina Salazar 

Hope you are doing good.

Did my reply answer your question?

If my response helped please close the thread by marking appropriate response as correct so that it benefits future readers.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

ss123
Tera Contributor

Hi Muhammad,

 

Thank you for your help on this. It worked as intended! 🙂 

 

Regards,

Sab