Changing Field Type in Salesforce

You can change the Field Type of custom fields that you create in Salesforce. Although it is a good practice to have well defined fields and field types during the design of your application, there is a good chance that you might have to change an existing custom field’s type well after the application has been in production. 

In Lightning Experience, the type of a custom field can be changed from Setup - Object Manager - Fields & Relationships - select the field and click Edit - click Change Field Type button. This article provides few key things to consider while changing custom field types.

First off: What cannot be done?

  • If you encrypt a custom field using Shield Platform Encryption, you can’t change the field type.
  • This should be obvious, but formula fields cannot be converted to any other data type, and also any other field type cannot be converted into a formula field.
  • After you have created a roll-up summary field on an object, you cannot convert the object's master-detail relationship into a lookup relationship. In other words, you can convert a master-detail relationship field created on a child/detail object into a lookup relationship, as long as there are no roll-up summary fields on the master object summarizing this child/detail object.
  • You cannot convert a lookup relationship to a master detail relationship if there are any existing records on the object that have a null value set for the lookup relationship. In other words, you can convert a lookup relationship to a master-detail relationship only if the lookup field contains a value in all the records.

What happens to the data?

There is a risk of losing existing data if you change the type of a field. Conversion to Text from certain field types might retain the data but lose the formatting in the UI. For example a Number field displaying the data in the UI as 100,000 (formatted with comma separating the thousands) when converted to Text type would just show the data as 100000. In cases where the type change would result in data loss, Salesforce displays a warning message similar to the below.

Below table lists some points to note during various type conversion scenarios.

Field Type
Changing to Type
Behavior
Number
Currency
Email
Percent
Phone
URL
Text
 - Data is Retained
 - Any specific formatting of the data in the UI is lost.
Auto Number
Picklist
Text
Data is Retained
Text
Auto Number
Data is Retained
Picklist
Multi-select Picklist
- List of Picklist values (metadata) are retained. Admin can add/modify the picklist values.
- Data in the field is retained and the value remains selected. Users can select more values.
- Any inactive picklist values are lost. (Both metadata and field data)
In addition, any compatible field type changes like converting from Number to Currency (and vice-versa) would retain the existing data provided the field lengths are identical.

Additional Restrictions and Considerations

You cannot change the type of a custom field which is referenced elsewhere by other items in Setup. For example, below error is shown when trying to change the type of a field being referenced by Validation Rules and Lookup Filters.


Below error is shown when changing the type of a field referenced in Flows or Process Builder. Note that Processes are also referred to as 'Flow' in the error message so be sure to check your Processes as well as Flows for the field reference.


These errors are displayed immediately upon selecting the field type. There are some other restrictions that get fired at the end of the field type modification process, and Salesforce shows error at the last step. For example, below error is shown when trying to change the type of a field being referenced in a Field Update.


Below error is shown if the field is being referenced in an Apex class or trigger.


Also, you can’t change the data type of any custom field that is mapped for Lead Conversion. The field type update process will fail at the last step. However, as of Winter' 19, the error message does not clearly tell why the field type update has failed when the field is being mapped for lead conversion. The reason is shown blank as below.


If you change the data type of a custom field that is set as an External ID, choosing a data type other than text, number, or email causes the field to no longer act as an External ID. This is because External ID fields must be text, number or email fields.

Invalid Conversions

Certain invalid type conversions like converting from Phone to Email might not throw any error during the conversion process, and also the data is not lost. However Salesforce throws validation error the next time the record is saved. For example, a Phone number field has the data +12125553490 and you changed the data type of this field to Email. The existing data is not lost, however when the record is subsequently edited, it throws an error as below while saving the record.

Changing custom Picklist field to Checkbox:

For a Picklist field, if you select Checkbox as the new data type, you can choose which picklist values to map to checked boxes and unchecked boxes. For example, you can convert a picklist field 'Broker Type' into a checkbox that displays whether the Broker is licensed or not based on the existing data in the picklist field. The wizard displays options for Picklist Value Conversion as below.


Changing Rich Text Area to Long Text Area:

You can only convert rich text area fields into long text area fields. Any images are deleted the next time the long text area field is saved. As long text areas do not support HTML markup, you can either choose to remove the HTML markup and keep the content, or keep the markup as plain text.


0 comments:

Post a Comment