If a user enters a city in a field, populate State and Country fields for that particular city.

Tejas16
Tera Contributor

I  have created a custom table with above three fields(city, state and Country) and I have given a reference to cmn_location table for City. So,When user select a city,based on selection of city its corresponding state and country must be auto-populated using any scripting.

1 ACCEPTED SOLUTION

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Tejas,

                 Refer this client script and script include to fulfill your requirement 

 

https://community.servicenow.com/community?id=community_question&sys_id=08c24761dbd8dbc01dcaf3231f9619f3

 

Kindly Mark correct and helpful if its applicable

View solution in original post

5 REPLIES 5

Chetan Mahajan
Kilo Sage
Kilo Sage

Hi Tejas,

                 Refer this client script and script include to fulfill your requirement 

 

https://community.servicenow.com/community?id=community_question&sys_id=08c24761dbd8dbc01dcaf3231f9619f3

 

Kindly Mark correct and helpful if its applicable

can we used scratchpad variable with Client script instead of using script include?

if yes then How?

g_scratchpad we can use in Onload client script it's best practice, but we need onchnage client script here. So its better to go with GlideAjax

Please check below script working for me

Client Script // onChange State

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


var ga = new GlideAjax("MyFavoritesAjax");
ga.addParam("sysparm_name", "getFavorites");
ga.addParam("sysparm_caller", g_form.getValue('u_city'));
ga.getXML(ajaxResponse);

function ajaxResponse(serverResponse) {
var favorites = serverResponse.responseXML.getElementsByTagName("favorite");

for (var i = 0; i < favorites.length; i++) {
var name = favorites[i].getAttribute("name");
var value = favorites[i].getAttribute("value");

alert ("value" + value);
if (name == 'state') {
g_form.setValue('u_state1', value);
}

if (name == 'country') {
g_form.setValue('u_country1', value);

}
}
}
}

 

Script Include : 

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

getFavorites: function() {

var id = this.getParameter('sysparm_caller'); // to get parameter from client side
gs.info(' chetan callidis' + id);

var loc = new GlideRecord('cmn_location');
loc.addQuery('sys_id', id);
loc.query();
if (loc.next()) {
gs.info("Chetan State : " + loc.state);
this._addFavorite("state", loc.state);
this._addFavorite("country", loc.country);

}
},


_addFavorite: function(name,value) {

var favs = this.newItem("favorite");
favs.setAttribute("name", name);
favs.setAttribute("value", value);
},

 

type: "MyFavoritesAjax"

 

});

 

Result : 

find_real_file.png

 

Kindly mark helpful if works for you