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

How to display country code as country name?

Buddy 1
Tera Contributor

I have a country field in 'ABC' case form, Based on account field in that form , the country field is displaying the value for ex: DE , but i have to achieve this as Germany , i.e., when account is selected , the country field should display as Germany instead of DE. 

Basically the country field is dot-walked from form layout,,--> account.country

When checking on configure dictionary , the table name is core_company
When i click on show country field: These are the below info 

Table: sn_customerservice_case

Field :account

Type: reference

Reference: customer_account

Reference Qual :new global.GenericUtils().getContactAccounts(current.contact.user_name );

Max Length : 32

 

 

How to achieve this with client script , i.e.., make the country field display country name instead of country code?

 

Thanks in advance!

10 REPLIES 10

Hi @Buddy 1 

 

How is your Account and Country related. Can you show the record for sample.

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Buddy1_0-1669267381267.pngBuddy1_1-1669267560132.png

The second image is a record from customer_account table

AnubhavRitolia
Mega Sage

Hi @Buddy 1 

 

As your Country field is string and store 'DE', so it is showing as 'DE' only in your Case form.

 

To get the Country name, you need to query 'core_country' table.

 

Please find the code below with your custom field on case to show Country Name:

 

OnChange Client Script:

Field - Account

 

 

 

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

var ga = new GlideAjax('AccountContryName');
ga.addParam('sysparm_name', 'getCountryName');
ga.addParam('sysparm_account', g_form.getValue('account'));
ga.getXML(AccountCountryFunc);
 }
function AccountCountryFunc(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  g_form.setValue('u_country_name',answer);
   }
   

 

 

 

Client Callable Script Include:

 

 

 

var AccountContryName = Class.create();
AccountContryName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getCountryName : function() {
		var accountID = this.getParameter('sysparm_account');
                var acc = new GlideRecord('customer_account');
                acc.get(accountID);
                var country_code = acc.country;

               var ctry = new GlideRecord('core_country');
               ctry.addQuery('iso3166_2', country_code);
               ctry.query();

             if(ctry.next())
             {
              return ctry.name;
             }
              return country_code;

	},

    type: 'AccountContryName'
});

 

 

 

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hi @AnubhavRitolia ,

I have created a custom country field , but the changes are not reflecting in the field. I am not getting where am i missing something.
Can you help me 

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

var ga = new GlideAjax('AccountContryName');  --> added my script include name
ga.addParam('sysparm_name', 'getCountryName'); --> same function name
ga.addParam('sysparm_account', g_form.getValue('account'));
ga.getXML(AccountCountryFunc);
 }
function AccountCountryFunc(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  g_form.setValue('u_country_name',answer);  ---> created a custom field with same name
getCountryName : function() {
		var accountID = this.getParameter('sysparm_account');
                var acc = new GlideRecord('customer_account');
                acc.get(accountID);
                var country_code = acc.country;

               var ctry = new GlideRecord('core_country');
               ctry.addQuery('iso3166_2', country_code);
               ctry.query();

             if(ctry.next())
             {
              return ctry.name;
             }
              return country_code;

	},    ----> added this above function in my already existing script include

Hi @Buddy 1 

 

Code look good to me expect that closing bracket '}' in Client Script but I guess you must have already done.

 

Lets debug the code with few alert() and gs.info() as below:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (newValue === '') {
      return;
   }
alert(g_form.getValue('account'));  // check that it display Account Sys_id in pop up.

var ga = new GlideAjax('AccountContryName');
ga.addParam('sysparm_name', 'getCountryName');
ga.addParam('sysparm_account', g_form.getValue('account'));
ga.getXML(AccountCountryFunc);
 }
function AccountCountryFunc(response) {
alert("response = " +response);  //check what value comes in response
  var answer = response.responseXML.documentElement.getAttribute("answer");
alert("Answer= " +answer); // check what value comes in answer
  g_form.setValue('u_country_name',answer);
   }

 

getCountryName : function() {

gs.info("Account Country SI : getCountryName "); // check on Logs if script include is called
		var accountID = this.getParameter('sysparm_account');
                var acc = new GlideRecord('customer_account');
                acc.get(accountID);
gs.info("Account Country Country : " +acc.country); // check on Logs what value show for Account.country field
                var country_code = acc.country;

               var ctry = new GlideRecord('core_country');
               ctry.addQuery('iso3166_2', country_code);
               ctry.query();

             if(ctry.next())
             {
gs.info("Account Country Country Name: " +ctry.name); // check on Logs what value show for Country.Name and if it is going inside this IF  field
              return ctry.name;
             }
           //   return country_code;
	},

 

For Script include, you can filter in System Logs with Message starts with Account Country.

 

Please let me know which alert() and logs worked properly and what values you are getting.

 

 

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023