Sub topic 17
Changing a Field Type Safely
This chapter assumes you know how to open a field in Object Manager, and that you have read the chapters on text, numeric, picklist and formula fields. It covers what happens to existing data when you change a field's type, which changes Salesforce allows, and how to plan a change that cannot be undone.
Why the type is hard to change
A field's type decides how Salesforce stores each value in the database. Changing the type means converting every existing value to a new storage format. Some conversions are lossless. Others cut, round or blank out values. Salesforce also checks every other component that refers to the field, so a type change can fail even when the data itself would convert cleanly.
Salesforce does not let you pick any target type. From the field's Edit page you see only the types that the current type may convert to. Everything else is blocked.
Conversions that are usually safe
These changes widen what the field can hold, or reinterpret values that are already text.
- Text to Text Area, or Text to Long Text Area, keeps the data. Longer limits cannot truncate anything.
- Email, Phone or URL to Text keeps the data, because these types are text with extra validation.
- Number to Currency or Percent keeps the digits. Check how the value is displayed afterwards, because a Percent field shows 0.5 as 0.5%, and users may read the same digits differently.
- Number to Text keeps the digits as a string, but you can no longer sort or total them as numbers.
- Checkbox to Text produces the strings "true" and "false" or the equivalent. Avoid this unless you have to.
A change that is safe for the data can still be unsafe for the people using it. Reports that sort or filter a field behave differently once it becomes text, and a sort on text puts "10" before "9".
Conversions that can lose data
Reducing length or precision
Changing a Number field's scale from 2 to 0 does not rewrite the stored values. Existing values keep their stored decimals, but the field then displays them rounded. Reducing the length of a Text field is blocked in most cases if existing values are longer. Even so, never rely on that check as your only protection. Query for the longest value first.
Text to Number, Date or Picklist
Converting text to a stricter type works only for values that match the new format. A value that does not parse, such as "N/A" in a field becoming a Number, is cleared. The same happens when a text value does not match any date format. Salesforce does not report which records lost data.
When converting Text to Picklist, Salesforce offers to build the picklist from the distinct values it finds. Values that appear in the data but are not added to the list may be kept on the record as inactive values. Review this list before you confirm.
Picklist to multi-select, and back
Changing a single-select picklist to a multi-select picklist keeps each value. Going the other way keeps only the first value for any record that has several selections, so the other selections are lost. Multi-select values are stored as one string with semicolons between the values, which is why they behave like text when converted.
Picklist to Text
Each value becomes its label as text. Dependent picklist settings and record type value assignments for that field no longer apply.
Dependencies that block a change
Salesforce refuses a type change when another component depends on the field in a way the new type would break. Common blockers are:
- A formula field that references the field. The formula may not compile under the new type, for example when a text comparison now meets a Number.
- A roll-up summary field that totals the field. Roll-up summaries only work on certain types, so changing a Currency field to Text breaks the roll-up.
- A field used as a controlling field in a dependent picklist.
- Workflow rules, flows, validation rules, Apex classes and reports. Some of these block the change and some fail later at run time.
The field's Where is this used? button in Object Manager lists many of these references. It does not cover everything, so search your code base and flows too.
A formula field has an extra constraint: you can change its return type only by rewriting the formula. A change of return type can break any formula that uses it.
Types that cannot be changed
Some conversions are not offered at all. In general, you cannot convert:
- Auto Number fields to or from most other types.
- A formula field to a stored type, or a stored type to a formula field. Create a new field instead.
- Master-detail to or from many other types. A master-detail relationship can be changed to a lookup only if the parent has no roll-up summary fields, and a lookup can be changed to master-detail only if every child record has a parent value.
- Encrypted text to other types, and the reverse, in most editions.
- Geolocation fields. Their component parts cannot be edited independently.
In these cases the pattern is always the same: create a new field with the right type, migrate the data, and retire the old field.
A safe change procedure
- Use Where is this used? and a metadata search to list every dependency.
- Export the record Ids and field values, so you can restore them if the conversion clears data.
- Check the data against the target type. For a Text to Number change, query for values that are not numeric.
- Make the change in a sandbox and run reports, flows and tests that touch the field.
- Schedule the change in production for a quiet period, because the conversion can lock records during processing on large objects.
- After the change, compare the exported values with the new values.
The following Apex finds Text values that will not convert to a number. Run it in an Anonymous Apex window before changing a custom field named Legacy_Code__c on Account.
List<Account> problems = new List<Account>();
for (Account a : [
SELECT Id, Legacy_Code__c
FROM Account
WHERE Legacy_Code__c != null
LIMIT 50000
]) {
if (!a.Legacy_Code__c.isNumeric()) {
problems.add(a);
}
}
System.debug(problems.size() + ' values will not convert');The isNumeric method accepts only digits. A value such as "12.5" fails the check even though it would convert, so treat the result as a list to review and not an exact count.
The parallel field approach
When the direct conversion is blocked or risky, build the new field next to the old one.
- Create the new field with the target type and a temporary name.
- Copy the data with a data load, a flow or a batch Apex job, and clean up values that will not fit.
- Move references over: formulas, reports, page layouts, flows and code.
- Hide the old field with field-level security, and keep it for a few weeks.
- Delete the old field. Deleted custom fields stay in the recycle area for 15 days and can be restored in that window.
This approach is slower, but it gives you a rollback path at each step.
Common mistakes
- Converting without an export, then discovering cleared values weeks later.
- Trusting the dependency list as complete. Hard-coded API names in Apex strings and external integrations do not show up.
- Forgetting that integrations may send values in the old format, such as text into a field that is now a Date.
- Changing a picklist to text and leaving users with free typing where a controlled list used to exist.
Exercise
A Text field called EmployeeCount_c holds values such as "120", "45" and "n/a". The team wants it to be a Number field so that a roll-up can total it. Which approach is safest, and why?
Show answer
Use a parallel field. Create a Number field, then clean the data so that "n/a" is replaced with a blank or a real number, and load the numbers into the new field. A direct conversion would clear "n/a" without reporting it, and any formula or report referring to the field might break. The parallel field also leaves the original text in place for rollback. Before the cutover, find non-numeric values with a query like the Apex check in this chapter.
Exercise
You change a multi-select picklist to a single-select picklist. A record had the values "Golf;Tennis". What is stored afterwards, and how could you have prevented the loss?
Show answer
Only the first value, Golf, is kept. You could have prevented the loss by exporting the field before the change, or by creating a new single-select field and a separate related object or field for the extra values.