Based on state country name should be autopopulated
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2022 06:38 AM
Hi,
I have a field "state" choice field contains multiple choice.
Now country field which is a string field.
Based on state field selected I want country to be autopopulated.
the logic what I want to implement is :
the choices which are there in state are fixed choices.
So I am dividing them like if choices are karnataka, texas
var state = g_form.getValue('u_state');
if(state == "karnataka"){
g_form.setValue('country') == India;}
else if(state == "Texas"){
g_form.setValue('country') == USA;}
but it works only on client side, I want to implement the same on server side as well.
how to achieve

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2022 08:54 AM
Hi,
Why do you want to do this with server side code.?
You can write before br like
if(current.state == 'Karnataka')
{
current.country = 'India';
else if
else if
else if
Like this.
But why do you want to write server side code.?
Regards
Regards,
Musab

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2022 05:50 PM
Hi Pravallika,
There's actually a Locations table that has Country and State/Providence fields. It would be better to enter State and Country information into this table and query on this table.
Then, create a Script Include. Since I'm only going to call this from server side, I've unchecked "Client callable". I've named the Script Include as "LocationUtil".
var LocationUtil = Class.create();
LocationUtil.prototype = {
initialize: function() {},
getCountryFromState: function(state) {
var grLocation = new GlideRecord('cmn_location');
if (grLocation.get('state', state)) {
return grLocation.country;
}
},
type: 'LocationUtil'
};
Then, this script include can be called from server script as follows.
var state = 'Texas';
var country = new LocationUtil().getCountryFromState(state);
gs.info(country);
Execution result of above script.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2022 06:21 PM
Following is a script include to call from client side.
Script Include. This time, check "Client callable" because it will be called from client script.
I've named this script "LocationUtilClient".
var LocationUtilClient = Class.create();
LocationUtilClient.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCountryFromState: function() {
var locSysId = this.getParameter('sysparm_loc_id');
var grLocation = new GlideRecord('cmn_location');
if (grLocation.get('state', locSysId)) {
return grLocation.country;
}
},
type: 'LocationUtilClient'
});
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ajax = new GlideAjax('LocationUtilClient');
ajax.addParam('sysparm_name', 'getCountryFromState');
ajax.addParam('sysparm_loc_id', newValue);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('country', answer);
}
});
}
In sample execution, I've set field State to be of type "Lookup Select Box" to table "cmn_location"
Execution result:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2022 06:32 PM
Furthermore, it's not even necessary to have a Client Script to fill in the country field.
I'll revert back to using the first Script Include named "LocationUtil" that run on server-side.
Be sure to set the onChange() client script to inactive because it is not needed.
I'll define field "country" to be of type "Reference" to table "cmn_location" and set Reference qualifier as follows.
javascript: new LocationUtil().getCountryFromState(current.variables.state);
This will fill Country Field.