drop down option need to hide

poco
Tera Contributor

I have one variable  type of event (drop down)

Board meting

remote 

telecom 

offsite 

if want visible "offsite" option only requested for is  VIP users otherwise that option need to hide.

1 ACCEPTED SOLUTION

Moin Kazi
Kilo Sage
Kilo Sage

Hi @poco ,

 

Please write an onChange client script for the Requested For field.

 

function executeClientScript(control, oldValue, newValue, isLoading) {

    // Use g_form.getReference to get the user record
    g_form.getReference('requested_for', function(user) {
        // Check if the user is VIP
        if (user.vip != 'true') { // Assuming 'vip' is a true/false field
            // Hide "Offsite" option from "Type of Event" variable
            g_form.removeOption('type_of_event', 'offsite');
        } else {
            // Ensure "Offsite" option is visible for VIP users
            g_form.addOption('type_of_event', 'offsite', 'offsite');
        }
    });
}

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

 

View solution in original post

3 REPLIES 3

ELMAHDI
Tera Expert

Hello @poco 

To show the "Offsite" option only for VIP users, you can use a Catalog Client Script with a simple script to dynamically adjust the options based on the requester's VIP status.

Here's how you can set it up:

  1. Create a Catalog Client Script on the catalog item where the "Type of Event" variable is used.

  2. Configure the Client Script to check if the requester is a VIP and adjust the options accordingly.

Here's the script to achieve this:

 

 
 

 

// Catalog Client Script - Type: OnLoad
function onLoad() {
    // Replace 'type_of_event' with the actual name or ID of your variable
    var eventType = g_form.getControl('type_of_event'); 

    // Retrieve the current user object to check the VIP status
    var isVip = g_user.hasRole('vip'); // Ensure you have a 'vip' role or adjust as needed

    if (!isVip) {
        // Remove the "Offsite" option for non-VIP users
        for (var i = eventType.options.length - 1; i >= 0; i--) {
            if (eventType.options[i].value == 'offsite') {
                eventType.remove(i);
            }
        }
    }
}

 

 
  • Check VIP status: The script uses g_user.hasRole('vip') to check if the current user has the VIP role. Adjust this line if you have a different way of identifying VIP users.
  • Remove "Offsite" option: If the user isn’t VIP, the script finds and removes the "Offsite" option from the dropdown.

This should make the "Offsite" option visible only to VIP users. Test the script to ensure it behaves as expected for both VIP and non-VIP users.

 



If you find this post helpful, don't forget to click on the Helpful Button 😊
thank you!



ELMAHDI
Tera Expert

@poco ,

Check this : 
https://www.servicenow.com/community/developer-forum/how-to-hide-dropdown-options-depending-on-condi...

If you find this comment helpful, don't forget to click on the Helpful Button 😊

thank you 

Moin Kazi
Kilo Sage
Kilo Sage

Hi @poco ,

 

Please write an onChange client script for the Requested For field.

 

function executeClientScript(control, oldValue, newValue, isLoading) {

    // Use g_form.getReference to get the user record
    g_form.getReference('requested_for', function(user) {
        // Check if the user is VIP
        if (user.vip != 'true') { // Assuming 'vip' is a true/false field
            // Hide "Offsite" option from "Type of Event" variable
            g_form.removeOption('type_of_event', 'offsite');
        } else {
            // Ensure "Offsite" option is visible for VIP users
            g_form.addOption('type_of_event', 'offsite', 'offsite');
        }
    });
}

 

 

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If you found my response **helpful**, I’d appreciate it if you could take a moment to select **"Accept as Solution"** and **"Helpful"** Your support not only benefits me but also enriches the community.

 

Thank you!
Moin Kazi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~