Changing a Text Field to a Rich Text Field

Author: Bruce Tollefson Published: May 23, 2022; Modified: May 27, 2022
computer_mouse

You created a text field based on business requirements, everything gets implemented to production and now requirements changed where you need to change the field to a rich text field. To add to that you have added apex references to the field. If you check the UI go to the field you want to change to a rich text –> Edit –> Change Field Type and you don’t see that as an option:

Change Field Data Type Screen

Next you may be thinking it is not possible, then everything that you need to do in order to change the Text field to a Rich Text field comes to mind. This may include:

  1. Creating an outage window
  2. Getting all of the values for that field
  3. Commenting out all the Apex code
  4. Deleting all of the Flow Versions (Even non-active) that reference the field
  5. Commenting out all Formula References use ‘/* */’
  6. Deleting the field
  7. Creating the field with the new data type
  8. Uploading all of the data that was in the field
  9. Uncommenting all of the Apex Code with references
  10. Deploying the flows back into the production org
  11. Hoping nothing get missed / messed up

If you have a data and recovery tool you could use that to make things a little easier but still not much. A lot of changes for one field switch. There is an alternative method as long as the only reference is through Apex. Unfortunately Flows and Formula References will error (discussed below).

Text Field to Rich Text Alternative Method

If you tried to retrieve the metadata for the text field (Test_Change_to_Rich_Text__c in this example):

<?xml version="1.0" encoding="UTF-8"?> <CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> <fullName>Test_Change_to_Rich_Text__c</fullName> <externalId>false</externalId> <label>Test Change to Rich Text</label> <length>255</length> <required>false</required> <trackTrending>false</trackTrending> <type>Text</type> <unique>false</unique> </CustomField>

and change it to Rich Text metadata:

<?xml version="1.0" encoding="UTF-8"?> <CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> <fullName>Test_Change_to_Rich_Text__c</fullName> <externalId>false</externalId> <label>Test Change to Rich Text</label> <length>32768</length><!-- Length must be greater than 255 --> <trackTrending>false</trackTrending> <type>HTML</type> <visibleLines>10</visibleLines><!-- must be 10 lines or greater --> </CustomField>

Then deploy you will get the following error in Setup –> Deployment Status error: Conversion to Html only supported from LongTextArea

Text To Rich Text Deployment Failure

This at least indicates you can convert a Long Text to a Rich Text. Next you are thinking let’s change the field from Text to Long Text to Rich Text, so you try that via the UI and receive a validation error:

Text To Long Text Validation Error

Even though this fails in the UI if you try this even a metadata deployment it will work:

<?xml version="1.0" encoding="UTF-8"?> <CustomField xmlns="http://soap.sforce.com/2006/04/metadata"> <fullName>Test_Change_to_Rich_Text__c</fullName> <externalId>false</externalId> <label>Test Change to Rich Text</label> <length>32768</length> <trackTrending>false</trackTrending> <type>LongTextArea</type> <visibleLines>10</visibleLines> </CustomField>

Then you can deploy the Rich Text Metadata again. Now you have a Rich Text field that was a Text field!

Flow and Formula Reference Error

If you are trying the above alternative method and you receive the following error: Cannot change type due to existing data . This occurs when the field you are trying to change is referenced from a formula or a flow (even inactive versions). To get the above to work you would need to comment out the formula references which can be done with ‘/* <field reference> */’. Unfortunately, there is no quick solution with flows. All versions would need to be deleted.

Leave a Reply

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