catalog item

Gaurav69
Tera Contributor

I have created a catalog item which has a variable(dropdown choices type) named countries having choices India and USA. I want if the logged in user's country is not india then only one choice (USA) should be visible to him. How to configure it? 

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi Gaurav,

You can use addOption() and removeOption() to show/hide choices. You can check thread for a check.

Rajesh Chopade1
Mega Sage

Hi @Gaurav69 

You need to create onLoad client script

- First check location of user

- Based on location you can use addOption() and removeOption() to show/hide choices. 

 

I hope this helps you to resolve your issue,

Please mark answer correct and helpful...

thank you.

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @Gaurav69,

 

Client Script :

var ga = new GlideAjax('UserObjectUtils');
ga.addParam('sysparm_name', 'userObject');
ga.getXML(HelloWorldParse);
 
function HelloWorldParse(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
 // alert(answer); 
if (answer == 'USA'){
g_form.removeOption('fieldname', 'india');
} else {
g_form.removeOption('fieldname', 'USA');
}
}


Script include name : UserObjectUtils
Client callable : true

var UserObjectUtils = Class.create();
UserObjectUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userObject:function() { 
var user = new GlideRecord("sys_user");
user.addQuery("sys_id", gs.getUserID());
user.setLimit(1)
user.query();
if (user.next()) {
return user.location.country;
}
} ,
   _privateFunction: function() { // this function is not client callable     
    }
 });
 

☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Deepak Shaerma
Kilo Sage

Hi @Gaurav69 

Script Logic

You will write a script that conditionally adjusts the options in the dropdown based on the user’s country. Here’s an example script:

function onLoad() {
    // Function to check the user’s country and adjust dropdown options accordingly
    checkAndAdjustCountryOptions();
}

function checkAndAdjustCountryOptions() {
    // Assuming ‘sys_user’ is the user table and ‘country’ is the relevant field
    // This example also assumes there’s a way to get the current user’s country
    // Replace ‘getCurrentUserCountry’ with your method of obtaining the user’s country
    var userCountry = getCurrentUserCountry();
    
    if (userCountry !== ‘India’) {
        // If the user’s country is not India, remove/hide the ‘India’ option
        removeOption(‘countries’, ‘India’);
    }
}

function removeOption(variable, optionText) {
    // Function to remove an option based on its text
    var options = g_form.getItemScope(‘countries’);
    options.forEach(function(option) {
        if (option.text === optionText) {
            // Replace or remove the option as needed
            // Here we’re hiding it. Feel free to adapt.
            g_form.removeOption(‘countries’, option.value);
        }
    });
}

function getCurrentUserCountry() {
    
    return ‘USA’; // Example static return value for demonstration
}



Note: Please Mark this Helpful and Accepted Solution. If this Helps you to understand. This will help us a lot.
Thanks & Regards 
Deepak Sharma