Is it possible to display only one field option to users in the US location.

Ria
Tera Contributor

Is it possible to display only one field option to users in the US location, while making all other options visible to users in other locations?
For example, only the wireless option should be visible to the US location users.

 

 

15 REPLIES 15

Dr Atul G- LNG
Tera Patron
Tera Patron

Hi @Ria 

 

Yes, you need to hide the choice values based on the user's location. Write a Catalog Client Script.

Try something like this:

 

https://www.servicenow.com/community/developer-forum/simple-client-script-to-hide-choice-value-based...

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

var name = g_form.getValue('name');
if(name=='payments'||name=='invoice'||name=='statement'){
g_form.clearOptions('status');
g_form.removeOption('status', '3rd Party');
}
if(name=='card){
g_form.clearOptions('status');
g_form.addOption('status', '3rd Party');
}

}

 

*************************************************************************************************************
If my response proves useful, please indicate its helpfulness by selecting " Accept as Solution" and " Helpful." This action benefits both the community and me.

Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/atul_grover_lng [ Connect for 1-1 Session]

****************************************************************************************************************

Ankur Bawiskar
Tera Patron
Tera Patron

@Ria 

you can use onLoad client script + GlideAjax

Script Include: It should be client callable

var UserLocationChecker = Class.create();
UserLocationChecker.prototype = Object.extendsObject(AbstractAjaxProcessor, {

    // This function checks if the user's location is 'US'
    checkUserLocation: function() {
        var userGR = new GlideRecord('sys_user');
        if (userGR.get(gs.getUserID())) {
            return userGR.location.name.toString() == 'US';
        }
        return false;
    },

    type: 'UserLocationChecker'
});

AnkurBawiskar_0-1743158549924.png

onLoad client script:

function onLoad() {
    var ga = new GlideAjax('UserLocationChecker');
    ga.addParam('sysparm_name', 'checkUserLocation');
    ga.getXMLAnswer(function(response) {
        // if US then remove wired option
        if (response.toString() == 'true')
            g_form.removeOption('type', 'wired'); // give the correct field name or variable name and the choice value
    });
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Robert H
Mega Sage

Hello @Ria ,

You can also achieve this with minimal scripting.

Step 1: Have a variable for the User, Location, and the Type, like this:

RobertH_1-1743159695048.png

 

Step 2: Configure the Location variable to get auto populated based on the User location:

RobertH_2-1743159754801.png

Step 3: Create a UI Policy with the Condition "Location = US" and the following script:

RobertH_0-1743160305824.png

 

If true:

function onCondition() {
	g_form.removeOption('type', 'wired');
}

If false:

function onCondition() {
	g_form.addOption('type', 'wired', 'wired');
}

If you don't actually require the User and Location variables to be visible on your form you can enable the "Hidden" checkbox on them.

Regards,

Robert

 

Ria
Tera Contributor

Hi @Robert H 

 

We already have a 'Site' field available on the catalog item. I have created a UI policy with above-mentioned script and condition as 'Site' is US. It's not working as expected and getting the below error when a UI policy is created.

RiyaJain_0-1743163787165.png

 

 

Regards,

Riya Jain