Show question choice based on who is open HR Case

rafas_2703
Tera Guru

Hi there guys!

 

I have a Record producer that contains the following variables:

Requested for, Requested by and Choose an option.

 

Requested for and requested by are automatically populated with the user opening the case.

The variable Choose an option is a select box type with the options : spouse, child and member.

 

I want the option member to be visible, only if the person on Requested by is the manager of the requested for.

 

What is the best approach of doing so?

 

Thank you!

1 ACCEPTED SOLUTION

JamesEcoStratus
Mega Guru

If I understand your question correctly, you can try the following approach.

 

To make the "member" option in the "Choose an option" select box visible only if the person on "Requested by" is the manager of the "Requested for," you can use client-side scripting in a ServiceNow Record Producer. Here's a step-by-step guide:

 

Edit the Record Producer:

 

Go to "Service Catalog" > "Catalog Definitions."

Open the Record Producer you want to modify.

Modify the "Choose an option" Variable:

 

Edit the "Choose an option" variable in the Record Producer.

In the variable configuration, set the "Visible" field to "false" initially to hide it by default.

 

Add a Client Script:

 

Go to "System Definition" > "Client Scripts."

Create a new client script.

Configure the client script as follows:

Table: The table associated with your Record Producer (e.g., the Incident table if it's an Incident Record Producer).

Type: OnLoad

Applies to: The specific Record Producer you're working with.

Write the Client Script:

 

In the client script, you'll need to check if the person on "Requested by" is the manager of the "Requested for" user.

If they are the manager, set the "Choose an option" variable to be visible.

 

Here's a sample client script to achieve this:

 

function onLoad() {

    var requestedBy = g_form.getValue('requested_by');

    var requestedFor = g_form.getValue('requested_for');

    var chooseOption = g_form.getControl('choose_option');

 

    // Check if the "Requested by" user is the manager of the "Requested for" user

    if (isManagerOf(requestedBy, requestedFor)) {

        // If yes, make the "Choose an option" variable visible

        chooseOption.style.display = ''; // Display the select box

        g_form.setMandatory('choose_option', true); // Make it mandatory if needed

    } else {

        // If not, hide the "Choose an option" variable

        chooseOption.style.display = 'none'; // Hide the select box

        g_form.setValue('choose_option', ''); // Clear any selected value

        g_form.setMandatory('choose_option', false); // Make it non-mandatory

    }

}

 

// Function to check if a user is the manager of another user

function isManagerOf(managerSysID, userSysID) {

    // Implement your logic to check if managerSysID is the manager of userSysID

    // You can use GlideRecord queries to fetch user roles, relationships, etc.

    // Return true if managerSysID is the manager of userSysID, otherwise return false.

    // Example:

    /*

    var managerGr = new GlideRecord('sys_user');

    managerGr.get(managerSysID);

    var isManager = managerGr.manager == userSysID;

    return isManager;

    */

}

 

Replace the comments in the code with your actual logic to determine if the "Requested by" user is the manager of the "Requested for" user. You may need to query user records or use any relationships you have in your instance to determine the manager-subordinate relationship.

 

This script will dynamically show or hide the "Choose an option" variable based on the manager-subordinate relationship between the two users.

 

Good Luck,

 

James

View solution in original post

2 REPLIES 2

Rohit01998
Tera Guru

Hello@rafas_2703 ,

you need to use onChange + GlideAjax in catalog client script for this and in script include you can write functionality if the person on Requested by is the manager of the requested for.

 

Give 👍if this helps you...

Thanks,

JamesEcoStratus
Mega Guru

If I understand your question correctly, you can try the following approach.

 

To make the "member" option in the "Choose an option" select box visible only if the person on "Requested by" is the manager of the "Requested for," you can use client-side scripting in a ServiceNow Record Producer. Here's a step-by-step guide:

 

Edit the Record Producer:

 

Go to "Service Catalog" > "Catalog Definitions."

Open the Record Producer you want to modify.

Modify the "Choose an option" Variable:

 

Edit the "Choose an option" variable in the Record Producer.

In the variable configuration, set the "Visible" field to "false" initially to hide it by default.

 

Add a Client Script:

 

Go to "System Definition" > "Client Scripts."

Create a new client script.

Configure the client script as follows:

Table: The table associated with your Record Producer (e.g., the Incident table if it's an Incident Record Producer).

Type: OnLoad

Applies to: The specific Record Producer you're working with.

Write the Client Script:

 

In the client script, you'll need to check if the person on "Requested by" is the manager of the "Requested for" user.

If they are the manager, set the "Choose an option" variable to be visible.

 

Here's a sample client script to achieve this:

 

function onLoad() {

    var requestedBy = g_form.getValue('requested_by');

    var requestedFor = g_form.getValue('requested_for');

    var chooseOption = g_form.getControl('choose_option');

 

    // Check if the "Requested by" user is the manager of the "Requested for" user

    if (isManagerOf(requestedBy, requestedFor)) {

        // If yes, make the "Choose an option" variable visible

        chooseOption.style.display = ''; // Display the select box

        g_form.setMandatory('choose_option', true); // Make it mandatory if needed

    } else {

        // If not, hide the "Choose an option" variable

        chooseOption.style.display = 'none'; // Hide the select box

        g_form.setValue('choose_option', ''); // Clear any selected value

        g_form.setMandatory('choose_option', false); // Make it non-mandatory

    }

}

 

// Function to check if a user is the manager of another user

function isManagerOf(managerSysID, userSysID) {

    // Implement your logic to check if managerSysID is the manager of userSysID

    // You can use GlideRecord queries to fetch user roles, relationships, etc.

    // Return true if managerSysID is the manager of userSysID, otherwise return false.

    // Example:

    /*

    var managerGr = new GlideRecord('sys_user');

    managerGr.get(managerSysID);

    var isManager = managerGr.manager == userSysID;

    return isManager;

    */

}

 

Replace the comments in the code with your actual logic to determine if the "Requested by" user is the manager of the "Requested for" user. You may need to query user records or use any relationships you have in your instance to determine the manager-subordinate relationship.

 

This script will dynamically show or hide the "Choose an option" variable based on the manager-subordinate relationship between the two users.

 

Good Luck,

 

James