Auto-manage group members by department

MujeebQ
Tera Contributor

Hey everyone

We need to create a group that automatically includes users from specific departments (from departments table or department field in sys_user). We share the tenant with another part of the organization and we want a role that applies only to our employees. What is the best way to set this up so members are added and removed automatically?

1 ACCEPTED SOLUTION

mujeebqasimi
Tera Contributor

Found a solution for this. I created a scheduled job that runs daily to add all users from the specified departments to the group, and another job that removes inactive users or those who have moved out of the department.

Using a consistent prefix for all departments under the same organizational unit makes this much easier to manage.

View solution in original post

3 REPLIES 3

Rafael Batistot
Kilo Patron

Hi @MujeebQ 

 

This solution might help you 

 

https://www.servicenow.com/community/developer-forum/can-you-create-a-dynamic-group/td-p/1808324

If you found this response helpful, please mark it as Helpful. If it fully answered your question, consider marking it as Correct. Doing so helps other users find accurate and useful information more easily.

mujeebqasimi
Tera Contributor

Found a solution for this. I created a scheduled job that runs daily to add all users from the specified departments to the group, and another job that removes inactive users or those who have moved out of the department.

Using a consistent prefix for all departments under the same organizational unit makes this much easier to manage.

nityabans27
Giga Sage

Hi @MujeebQ ,

Best approach: use a Dynamic Group with a scripted membership condition.

Steps:

  1. Go to User Administration → Groups → New → Dynamic Group.

  2. In the Dynamic Group Script, add logic like:

    // Include users from specific departments
    (function() {
        var depts = ['IT', 'Finance']; // replace with your department names
        var userDept = current.department.getDisplayValue();
        return depts.indexOf(userDept) > -1;
    })();
  3. Save and assign the required role(s) to this group.

  4. The platform will automatically add/remove members as users’ department fields change — no manual sync needed.

✅ Why this way:

  • Avoids cloning user records or manual maintenance.

  • Keeps role assignment scoped to your org segment.

  • Automatically stays updated as departments or users change.