The CreatorCon Call for Content is officially open! Get started here.

Based on state country name should be autopopulated

Pravallika9
Tera Expert

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

6 REPLIES 6

Musab Rasheed
Tera Sage

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

Please hit like and mark my response as correct if that helps
Regards,
Musab

Hitoshi Ozawa
Giga Sage
Giga Sage

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.
find_real_file.png

find_real_file.png

 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".
find_real_file.png

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.

find_real_file.png

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".

find_real_file.png

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"

find_real_file.png

Execution result:

find_real_file.png

Hitoshi Ozawa
Giga Sage
Giga Sage

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.