Check if a user is already in a similar group

Hendrik6
Kilo Guru

Hello

 

I am developing a scoped application that is used to manage the work plans of employees organized in work teams. Each work team has multiple users assigned to it. However, a single user can be only in zero to one work teams. This condition should be checked at all times. Currently, I use groups [sys_user_group] to represent a work team. Every work team inherits its roles from the parent group "Work Team" which contains the role "Work Team Member".

 

My current approach:

I created a business rule on the Group Role table [sys_user_grmemember] to check before an insert of a new record if any of the groups assigned to the user in the new record contains the role "Work Team Member". Therefore, I use two GlideRecord queries and two while loops to iterate through the records. 

 

Problems with the current approach:

The business rules succeeds when I try to create a record directly on the table sys_user_grmember. But, when I try to add a group over the related list in a user record, the debugger does not even notices that the business rule is triggered. This behavior is very odd.

 

Alternative approaches:

  1. Instead of checking for the user's roles and their roles. I could check if any of the groups assigned to the user has already the parent "Work Team".
  2. I could create a new type "Work Team" and check if any of the groups assigned to the user has already the type "Work Team". This approach would not differ much from the first alternative.
  3. Perhaps instead of a business rule I could try using a scripted ACL.
  4. If a user is already is already in a work team group they will have to have the role "Work Team Member". Therefore, I could check by gs.hasRole("admin") if the user has this role.

 

Questions:

  1. I am wondering why my current approach with a business rules works on the sys_user_grmember table but not when I try to assign a group to a user via the user's related list. How can I fix that?
  2. Are there any other best practices how to ensure that a user is only in one type of a group?

 

Kind regards,

 

Hendrik

10 REPLIES 10

Hello Saurabh

 

Thank you for your advise on how to simplify the query, that is very useful. 

 

However, I am experiencing an odd error: 

When I enter a new record in the table sys_user_grmember directly the business rule correctly shows the error message and prevents the insert. However, when I am in a group and want to add the user via the related list and the slushbucket the business rules also prevents the insert, but it does not show a message. Instead I am seeing the following message:

Hendrik6_0-1674809657272.png

 

Do you know how I could display my error message as well?

 

Kind regards,

 

Hendrik

Hi,
Which version of servicenow you are using?

 

 


Thanks and Regards,

Saurabh Gupta

Hi,
Just try changing the order of BR. (try making it -100 or 10000)

 

 

 

 


Thanks and Regards,

Saurabh Gupta

Hello

 

We are on Tokyo.

Hi,
In Tokyo onwards this role management is happening through events.

 

You can try changing the order of BR as per below.

 

SaurabhGupta_0-1674823498788.png

 

 

 

  


Thanks and Regards,

Saurabh Gupta