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)
- Enable custom address field (Beta)
- Create a Custom Address field
- 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.
- Go to Setup | Search for User Interface.
- Select User Interface.
- Click on Use custom address fields (Beta) and click on Save.
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.
- 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.
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.
- Configure States, Countries, and Territories
- Setup | Then Search for State and Country/Territory Picklists.
- On the State and Country/Territory Picklists page, click the Configure States, Countries, and Territories.
- On the Configure States, Countries, and Territories page, select Active and Visible.
- Click Save.
For example in my case I only want to provide support for United State then Selected United state only like below screen
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.
- 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
- 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.
- 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.
- You can’t mark a custom address field as required but you can make it required on page layout or by validation rules.
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.