Dynamically Hiding Choice Values in Record Producer Based on User country

sravanku888
Tera Contributor

Hi Everyone,

 

I'm currently working on a record producer and need some assistance with dynamically controlling the visibility of choice values for a specific variable.

Here's the scenario:

I have a variable named "Select Currency" with the following choices:

1.India INR
2.United States Dollar (USD) $
3.Euro (EUR) €
4.British Pound Sterling (GBP) £
5.Japanese Yen (JPY) ¥
6.Australian Dollar (AUD) $
The requirement is to filter these choices based on the logged-in user's country.

For example:

If the user's country is India, only "India INR" should be visible in the "Select Currency" dropdown.
If the user's country is the United States, only "United States Dollar (USD) $" should be visible.
Has anyone implemented something similar before? I'm looking for guidance on the best approach to achieve this. Should I be looking at client scripts, UI policies, or perhaps something else? Any code snippets or pointers would be greatly appreciated!

Thanks in advance for your help!

1 REPLY 1

Abbas_5
Tera Sage
Tera Sage

Hello @sravanku888,

 

To dynamically hide choice values in a ServiceNow record producer based on the user's country, you can use a combination of client scripts and UI policiesSpecifically, you'd use a client script to filter the options available in a choice field based on the user's country, and then use UI policies to handle the visibility of other related fields or sections. 
 
Please refer to the below steps:
1. Identify the User's Country:
  • Accessing User Information:
    You'll need to access the user's country information. This might be stored in the user's profile or a related table. The script will need to be able to retrieve this information. 
     
  • Example:
    If the user's country is stored in the user_country field, you can access it using g_user.get(user_country). 
     
2. Create a Client Script:
  • onChange Function:
    Create a client script that triggers when the user changes the choice field (or when the form loads, if you want to hide values initially).
  • Conditional Logic:
    Within the script, use conditional statements (e.g., ifelse ifelse) to determine which choice values to hide based on the user's country.
  • g_form.removeOption():
    Use the g_form.removeOption() method to remove specific choice values from the dropdown.
    • Example: g_form.removeOption("my_choice_field", "value_to_hide");.
  • Example:
    If the user is in the US, hide certain options, and if they are in another country, hide a different set of options. 
     
3. Create UI Policies (Optional, for related fields/sections):
  • Visibility Control:
    You can use UI policies to dynamically show or hide other fields or sections in the record producer, based on the values selected in the choice field.
  • Condition:
    The UI policy's condition will be based on the value selected in the choice field (or another field related to the choice field).
  • Action:
    The UI policy will either set the visibility of related fields or sections to true (visible) or false (hidden). 
     
4. Integrate with the Record Producer:
  • Connect Fields: Ensure that the choice field and any fields or sections affected by the UI policy are properly linked in the record producer. 
     
Example (Simplified):
Let's say you have a choice field named issue_type with values like "Problem," "Incident," and "Change." You want to hide "Change" for users in certain countries. 
  1. Access Country: Retrieve the user's country (e.g., using g_user.get(country)).
  2. Client Script:
 
    function onChange(control, oldValue, newValue, isLoading) {        if (isLoading || newValue == '') { return; }        if (g_user.get(country) === 'Country A') {            g_form.removeOption('issue_type', 'Change');        } else if (g_user.get(country) === 'Country B') {            g_form.removeOption('issue_type', 'Incident');        }    }
  1. UI Policy (Optional): You could use a UI policy to show/hide a "Change Details" section based on the selected issue_type.
     
Key Considerations:
  • Performance:
    Ensure that your scripts are efficient, especially if you have many choice options.
  • User Experience:
    Provide clear feedback to the user about why certain options are hidden or displayed.
  • Testing:
    Thoroughly test your implementation to ensure it works correctly across different user profiles and locations. 
     
By combining client scripts and UI policies, you can effectively control the visibility of choice values in your record producers based on user-specific information, such as their country.
 
If it is helpful, please mark it as helpful and accept the correct solution by referring to this solution in the future; it will be helpful to them.
 
Thanks & Regards,
Abbas Shaik