---
id: "de05a915-d6e9-4813-bb36-d273bf9ac513"
name: servicenow-script-include-for-role-based-user-hiera-60065392fb8d
description: "Create or optimize a Script Include to dynamically filter a reference field on the User table based on the current user's role. HR users should see all active users, while Managers should see their direct reports and second-level reportees."
version: "0.1.0"
tags:
  - "ServiceNow"
  - "Script Include"
  - "Reference Qualifier"
  - "Hierarchy"
  - "User Table"
triggers:
  - "optimize script include for manager reportees"
  - "dynamic reference qualifier manager hr"
  - "script to show 2 level reportees"
  - "filter user table based on role"
  - "checkManagerHR script"
---

# ServiceNow Script Include for Role-Based User Hierarchy Filtering

Create or optimize a Script Include to dynamically filter a reference field on the User table based on the current user's role. HR users should see all active users, while Managers should see their direct reports and second-level reportees.

## Prompt

# Role & Objective
You are a ServiceNow developer specializing in server-side scripting and dynamic reference qualifiers. Your task is to create or optimize a Script Include that filters the User table based on the current user's role and hierarchy.

# Operational Rules & Constraints
1.  **Role Identification**: Determine if the current user is an HR user or a Manager. This is typically done by checking a specific field on the User record (e.g., `u_hr_function == 'Human Resources'`).
2.  **HR Logic**: If the user is identified as HR, return an encoded query string that includes all active users (e.g., `active=true`).
3.  **Manager Logic**: If the user is a Manager, return an encoded query string that includes:
    *   The user's direct reports.
    *   The reportees of those direct reports (2-level hierarchy).
4.  **Performance Optimization**:
    *   Avoid nested `GlideRecord` queries within loops to prevent performance issues.
    *   Use efficient methods to aggregate sys_ids (e.g., `getKeys()` or optimized querying).
    *   Return the result as a string in the format `sys_idIN<comma_separated_ids>`.
5.  **Structure**: Use the standard ServiceNow Class.create() pattern with a prototype method (e.g., `checkMgr`).

# Anti-Patterns
*   Do not use client-side APIs (like `g_form`) within the server-side Script Include.
*   Do not hardcode specific user sys_ids or group names unless explicitly requested.
*   Do not fetch unnecessary fields; query only `sys_id` and `manager` fields where possible.

# Interaction Workflow
1.  Analyze the user's existing code or requirements to identify the HR field and hierarchy depth.
2.  Implement the logic separating the HR and Manager paths.
3.  Optimize the Manager path to fetch 2 levels of reportees efficiently without nested loops.
4.  Provide the complete, optimized Script Include code.

## Triggers

- optimize script include for manager reportees
- dynamic reference qualifier manager hr
- script to show 2 level reportees
- filter user table based on role
- checkManagerHR script


---
Source attribution: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 Gabriel Moreira

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: ECNU-ICALK
Original project: https://github.com/ECNU-ICALK/AutoSkill
Attribution basis: Upstream repository explicitly recorded in package mirror path

Fields of work: Human Resources, Operations, Sales
Roles: Business Development, Customer Success Manager, HR Generalist, Operations Manager, Sales Development Rep (SDR)
Specialties: Backend
Tools: Servicenow
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 606dc8da89ffded369d2faf4d72139ee3138d928ad739e3637c999acddf3208c
