Script to find user is VIP or not in ServiceNow | ServiceNow

How to Determine the VIP Status of Users and Their Managers in ServiceNow



In ServiceNow, there may be scenarios where you need to identify whether a user or their manager has VIP status. This information can be crucial for performing specific operations or implementing custom workflows. In this blog post, we will explore a script that helps in determining the VIP status of users and their managers in ServiceNow.

Problem Statement

Imagine you encountered a situation where you needed to find out whether a user or their manager has VIP status. Based on this information, you have certain tasks or actions to perform within your ServiceNow instance.

The Solution

To solve this problem, we can utilize the powerful GlideRecord object in ServiceNow. GlideRecord allows us to query and manipulate records in the ServiceNow database, making it an ideal tool for retrieving user-related information.

Here is the script that you can use to determine the VIP status of users and their managers:

var gr = new GlideRecord('sys_user');

gr.addQuery('sys_id', 'sys_id'); //put your sys_id here or you can use var userId = gs.getUserID(); to get sys_id of user

gr.query();

if (gr.next()) {

    var man = gr.manager;

    gs.print(man.sys_id);

}

var br = new GlideRecord('sys_user');

br.addQuery('active', 'true');

br.addQuery('sys_id', man.sys_id);

br.query();

if (br.next()) {

    if (br.vip == true) {

        gs.print('yes');

    } else {

        gs.print('no');

    }

}

Explanation:

  • var gr = new GlideRecord('sys_user');: This line creates a new GlideRecord object called gr for the "sys_user" table. GlideRecord allows you to query and manipulate records in the ServiceNow database.

  • gr.addQuery('sys_id', 'sys_id');: This line adds a query condition to gr retrieve the record with the specified sys_id value. The sys_id provided here is "sys_id". Put your sys_id here.

  • gr.query();: This line executes the query defined in the GlideRecord object and retrieves the matching record.

  • if (gr.next()) { ... }: This if statement checks if there is a record found by the query. If a record is found, the code inside the if block will be executed.

  • var man = gr.manager;: This line retrieves the value of the "manager" field from the GlideRecord gr and assigns it to the variable man.

  • gs.print(man.sys_id);: This line prints the sys_id of the manager to the system logs using the gs.print() function.

  • var br = new GlideRecord('sys_user');: This line creates a new GlideRecord object called br for the "sys_user" table.

  • br.addQuery('active', 'true');: This line adds a query condition to br to retrieve active records.

  • br.addQuery('sys_id', man.sys_id);: This line adds another query condition to br to retrieve records with the sys_id matching the value stored in the man variable (the sys_id of the manager from the previous GlideRecord).

  • br.query();: This line executes the query defined in the GlideRecord object br.

  • if (br.next()) { ... }: This if statement checks if there is a record found by the query. If a record is found, the code inside the if block will be executed.

  • if (br.vip == true) { ... } else { ... }: This if-else statement checks the value of the "VIP" field of the GlideRecord br. If the field value is true, it prints 'yes' to the system logs using gs.print(). Otherwise, it prints 'no'.

Usage

You can use this script in various scenarios within ServiceNow, such as:

  • As a Business Rule to automatically perform certain actions based on the VIP status of users and their managers.
  • Within custom workflows or workflows that involve user management.
  • In widgets or UI scripts to display VIP-related information or trigger specific behaviors.

Remember to replace 'sys_id' with the actual sys_id of the user you want to check, or use the dynamic approach mentioned in the script.

Post a Comment

0 Comments