Custom Address Field In Salesforce

Until Summer’22, We dont have way to create composite Address field in Salesforce for same we have to create multiple fields for address-related information (Country, State, City, Street, PostalCode). In this post we will discuss how to enable the custom address field in Salesforce for your org with the step by step process.

What is Compound Fields in Salesforce

Compound fields group together multiple elements of primitive data types to represent complex data types, such as a location or an address. Standard addresses built into standard objects in Salesforce are structured compound data type, as well as individual address elements. If you enabled Custom Address Fields (beta) you can also add custom fields that mimic the standard address field behavior.

Deliver Custom Address Field in Salesforce

With Custom Address Field (Beta), you can add and retrieve address data via custom Address compound fields on Salesforce standard and custom objects. We have to perform below steps to enable Custom Address fields (Beta)

  1. Enable custom address field (Beta)
  2. Create a Custom Address field
  3. Implement State and Country Picklist (Optional if you want to use default picklist)

Step 1. Enable custom address fields

Follow below steps to enable the custom address field in Salesforce.

  1. Go to Setup | Search for User Interface.
  2. Select User Interface.
  3. Click on Use custom address fields (Beta) and click on Save.
Enable custom address fields In Salesforce.

NOTE: Custom address fields can’t be disabled again.

Step 2. Create a Custom Address field

Now it time to create custom address field. You can create custom address field on standard or custom object. You need to follow below step

  • Click on Setup | Select object Manager
  • Select Object
  • Click on Field & Relation then select Data Type as Address.
Create a Custom Address field
  • Then add field Name and API name.
  • Last step add the field on page layout.

Once field is created, By default, all countries, territories, and their states will visible to end user on custom address field like below screen.

Custom Address Field

If State and Country/Territory Picklists aren’t enabled in your org, the State and Country components of the standard Address fields remain free text fields. Enabling the picklists for standard fields isn’t required to use Custom Address Fields (beta).

Step 3. Implement State and Country Picklist ( Optional )

Custom address fields use the State and Country picklists. By default, all countries, territories, and their states and provinces are visible to users. To specify the available picklist values in Salesforce, configure State and Country/Territory Picklists.

  1. Configure States, Countries, and Territories
    1. Setup | Then Search for State and Country/Territory Picklists.
    2. On the State and Country/Territory Picklists page, click the Configure States, Countries, and Territories.
    3. On the Configure States, Countries, and Territories page, select Active and Visible.
    4. Click Save.

For example in my case I only want to provide support for United State then Selected United state only like below screen

Implement State and Country Picklist

Let see the result of above configuration on Standard and custom address fields.

DML for Custom Address field

If you want to use Custom Address field in Apex then you need to use like below. You need to access all compound field with FieldAPI Name + StateCode__s

Account a = new Account();
a.Name='ApexHours';
a.Custom_Address_Field__c__StateCode__s='CA';
a.Custom_Address_Field__c__CountryCode__s='US';
a.Custom_Address_Field__c__Street__s='1234 Dublin Blvd';
a.Custom_Address_Field__c__PostalCode__s='12345';
a.Custom_Address_Field__c__City__s='Dublin';
a.Custom_Address_Field__c__Latitude__s=80.34;
a.Custom_Address_Field__c__Longitude__s=80.35;
a.Custom_Address_Field__c__GeocodeAccuracy__s='Address';
insert a;

Limitation of Custom Address Field

Before you enable custom address fields, We would like to suggest you to review the Custom Address Fields (Beta) Requirements and Limitations

  1. State and Country/Territory Picklists : Custom address fields use picklists for the State and Country address fields. If State and Country/Territory Picklists aren’t enabled, those picklists are enabled for custom address fields with Custom Address Fields (beta). By default, all countries, territories, and their states and provinces are visible to users
  2. Package Deployment : If a package contains a custom field with the Address field type, package deployment requires that Custom Address Fields (beta) is enabled in the target org.
  3. Custom Address Fields and Org Limits : each custom address field counts as nine custom fields: one each for street, city, postal code, country code, state code, geocode accuracy level, longitude, and latitude, plus one for internal use.
  4. You can’t mark a custom address field as required but you can make it required on page layout or by validation rules.

Summary

This Custom Address Field feature will help us avoid creating multiple fields for address-related information (Country, State, City, Street, PostalCode). This will available in your sandbox soon.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 461

One comment

  1. I added Address type of field in a related list on Account object. However, when I retrieve this Account page layout in VS Code, the related list in the Account Page layout’s metadata XML does not show the breakdown of the Address field into city, street etc. Any idea?

Leave a Reply

Your email address will not be published. Required fields are marked *