Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

set field based on logged in used 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.

3 REPLIES 3

yad_achyut
Giga Guru

@Anubhav Srivas1  : Write a onload client script to set the value 

can you please help me with the script

@Anubhav Srivas1 : you can do it in two ways
1. using the onLoad Client script or you can change your variable type from lookup to "reference" and set the values as :
Reference Table : 

cmn_location

 

 

Variable attributes
 

 

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

 

Screenshot 2024-06-04 at 10.55.15 AM.png
2. You can write an onload client Script:

 

function onLoad() {
    var countryCode; // country code
    var user = g_user; // current logged-in user 

     var userLocationGR = new GlideRecord(‘cmn_location’); //Location [cmn_location] table
    if (userLocationGR.get(user.location)) {
        countryCode = userLocationGR.getValue(‘country’); // Get the country from the user’s location

        // lookup variable’s name is ‘country_lookup’ change it according to your variable
        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);
        });
    }
}

 


Thanks 
Achyut