set field value Based on logged in user country

Anubhav Srivas1
Tera Expert

Hello,

i have a lookup variable which has country data (like: australia, england, china, etc),

i want to set these lookup values based on the logged in user country location.

 

example: if requested for country = (India IN), then the variable (what country is this requested for?) should be automatically selected as = India. I have attached a similar example. The variable : (what country is this requested for? ) has value which is coming from a lookup and it is currently on the custom table.

2 REPLIES 2

Deepak Shaerma
Kilo Sage

Hi @Anubhav Srivas1 

 

1. Open the Service Catalog Item or Record Producer for which you want to set the country value.
2. Navigate to Catalog Client Scripts related list and create a new Catalog Client Script.
3. Choose the type OnLoad since you want to set the value when the form is loaded.
4. In the Script field, enter the JavaScript code to set the country value:

 

 

function onLoad() {
    var countryCode; // Variable to hold the country code
    var user = g_user; // Get the current logged-in user object

    // Assuming ‘location’ is a field on the User [sys_user] record that references the Location [cmn_location] table
    // where the country is stored
    var userLocationGR = new GlideRecord(‘cmn_location’);
    if (userLocationGR.get(user.location)) {
        countryCode = userLocationGR.getValue(‘country’); // Get the country from the user’s location

        // Assuming your lookup variable’s name is ‘country_lookup’
        var countryLookupVariable = g_form.getReference(‘country_lookup’, function(countryLookupVar) {
            // Set the value for the lookup variable based on the country code
            g_form.setValue(‘country_lookup’, countryCode, countryLookupVar.display_value);
        });
    }
}

 

 

// Note: Replace ‘country_lookup’ with the actual name of your variable

 Please Mark this as Helpful/ Solved if this solves your query

Thanks & Regards
Deepak Sharma

Service_RNow
Mega Sage

Hi @Anubhav Srivas1 

you can populate the default value in variables

location reference variable

javascript: gs.getUser().getLocation();
country string variable

javascript: gs.getUser().getRecord().getValue('country');

Please mark reply as Helpful/Correct, if applicable. Thanks!