SugarCRM SupportProduct GuidesSugar DeveloperSugar Developer Guide 11.1CookbookConverting Address' Country Field to a Dropdown
This release is only available for SugarCloud customers.

Converting Address' Country Field to a Dropdown

Overview

Address fields in Sugar® are normally text fields, which allow users to enter in the appropriate information (e.g. street, city, and country) for the record. However, with multiple users working in Sugar, it is possible for data (e.g. country) to be entered in a variety of different ways (e.g. USA, U.S.A, and United States) when creating or editing the record. This can cause some issues when creating a report grouped by the Billing Country field, for example, as records with the same country will be grouped separately based on the different ways the country was entered.  

This article will cover how to change the address' Billing Country field to a dropdown list which will allow users to select a single value (e.g. USA) and maintain consistency in data throughout the system.    

Note: This article pertains to Sugar versions 6.x and 7.x.

Use Case

In this example, we will convert the Billing Country field in the Accounts module to a dropdown-type field to allow values to be selected from a dropdown list.

Prerequisites

A part of making this change involves mapping the "countries_dom" dropdown list to your existing Billing Country field's values. This dropdown list can be accessed and modified via Admin > Dropdown Editor. For more information on editing dropdown lists, please refer to the Developer Tools documentation. It is very important that the existing values in the Billing Country field exactly match the Item Name values in the "countries_dom" dropdown list in order for the values to convert properly. If the existing value (e.g. United States) does not match one of the country options (e.g. USA) in the dropdown list, then the new dropdown version of the Billing Country field will most likely default to a blank value for that record record.

To avoid such issues, please review and update all of your existing Billing Country text field values prior to making this change. For more information on updating many records at once via import, please refer to the Updating Records Via Import article. 

Steps to Complete

Converting Field to Dropdown

Use the following steps to change the Billing Country field to a dropdown list:

  1. First, we will associate the Billing Country field in the Accounts module with the "countries_dom" dropdown. Locate the following directory in your Sugar file system: ./custom/Extension/modules/Accounts/Ext/Vardefs/
  2. Locate a file called sugarfield_billing_address_country.php which controls the billing_address_country field and add the following lines to the file. This will set the field type to 'enum' and define the dropdown (countries_dom) to use for the field. 
    Note: If this file or location does not exist, then you will need to first create this path and file.
    Your file should look like this:
  3. <?php
    
    $dictionary['Account']['fields']['billing_address_country']['type']='enum';
    $dictionary['Account']['fields']['billing_address_country']['options']='countries_dom';
  4. Now, since the address block is handled uniquely in Sugar, we will also need to modify the template file in order to display the field as a dropdown anywhere the layout is being used in backward compatibility mode. To do this in an upgrade safe way, you will need to copy the file located in ./include/SugarFields/Fields/Address/en_us.EditView.tpl and place it in the following directory: ./custom/include/SugarFields/Fields/Address/
    If you are using multiple languages in Sugar, you will need to make this change for each language-type in your system. For our example, we will be focusing on the "en_us" language.
    Note: You will most likely need to create this directory as it likely does not already exist.
  5. Once the en_us.EditView.tpl file is in the custom directory, locate the line for the input html element for the country field. The line of code should look similar to this:
    <input type="text" name="{{$country}}" id="{{$country}}" size="{{$displayParams.size|default:30}}" {{if !empty($vardef.len)}}maxlength='{{$vardef.len}}'{{/if}} value='{$fields.{{$country}}.value}' tabindex="{{$tabindex}}">
  6. Directly between the list shown above and the <td> appearing before it, add the following lines to display the field as a dropdown:
    {if (!isset($config.enable_autocomplete) || $config.enable_autocomplete==false) && isset($fields.{{$country}}.options)}
    <select name="{{$country}}" id="{{$country}}" title=''>
    {if isset($fields.{{$country}}.value) && $fields.{{$country}}.value != ''}
    {html_options options=$fields.{{$country}}.options selected=$fields.{{$country}}.value}
    {else}
    {html_options options=$fields.{{$country}}.options selected=$fields.{{$country}}.default}
    {/if}
    </select>
    {else}
    <input type="text" name="{{$country}}" id="{{$country}}" size="{{$displayParams.size|default:30}}" {{if !empty($vardef.len)}}maxlength='{{$vardef.len}}'{{/if}} value='{$fields.{{$country}}.value}' tabindex="{{$tabindex}}">
    {/if}
  7. Save the changes to the file.

Once the necessary change has been made, please navigate to Admin > Repair and perform a Quick Repair and Rebuild in order for the change to take effect. Your Billing Country field will now display as a dropdown field in the Accounts module.

Please note that only the Account module's Billing Country field will be converted to a dropdown field once this change is applied. If you wish to have the Shipping Country field converted to a dropdown as well, please follow the steps above, but be sure to make the change specific to the "shipping_address_country" field. In addition, you can make this change for other modules (e.g. Contacts and Leads) as well by specifying the appropriate module name (e.g. Contacts) and field name (e.g. Primary Address Country) when going through the steps.

Updating the Field Type for List View Filter

Once the appropriate changes have been made per the section above, use the following steps to get the list view filter to recognize the change:

  1. Navigate to Admin > Studio > Accounts > Layouts > Search.
  2. Drag the Billing Country field from the Default column to the Hidden column and click "Save & Deploy".
    ChangingBillingCountry Dropdown Studio Search
  3. Now drag the Billing Country field back to the Default column and click "Save & Deploy" again.

This will retrieve the Billing Country field in its new state and add it to the account's list view filter.

Application

Once the Billing Country field is converted successfully to a dropdown, all records that had an existing value in the field should have the matching country automatically selected. Going forward, users will simply need to select the appropriate country when entering address information in Sugar. 

Please note that administrators can add additional country values as necessary to the "countries_dom" dropdown via Admin > Dropdown Editor. For more information on editing dropdown lists, please refer to the Developer Tools documentation.  

The Billing Country field will now appear as follows:

For Accounts record view:
BillingCountry_Dropdown_7 

For Accounts list view filter:
ChangingBillingCountry Dropdown ListViewFilter

 

Last modified: 2021-05-19 19:16:09