Sub topic 3
Text fields: from 255 characters to rich text
This chapter assumes you can create a custom field in Object Manager and that you know what an API name is. It covers the four text field types and how to choose among them. Salesforce stores all of them as strings, but they differ in length, formatting, and how you can filter, sort, and index them.
The four types at a glance
| Type | Maximum length | Lines | Formatting | Typical use |
|---|---|---|---|---|
| Text | 255 | Single | Plain | Names, codes, short labels |
| Text Area | 255 | Multiple | Plain | Short multi-line values such as an address |
| Text Area (Long) | 131,072 | Multiple | Plain | Notes, descriptions, pasted logs |
| Text Area (Rich) | 131,072, including HTML markup | Multiple | Bold, lists, links, images | Formatted instructions, customer-facing content |
The length you set is a ceiling you choose when you create the field. For long and rich text, the default is 32,768 characters.
Text
Text is the default choice for short values. You set a length from 1 to 255.
Text is the only plain-text type that supports the attributes tied to matching records:
- Unique: Salesforce rejects a second record with the same value. You choose whether
ABCandabccount as duplicates. - External ID: the field can identify records during upserts and data loads.
- Required and default value, as with most types.
Text values can be used in WHERE, ORDER BY, and GROUP BY clauses. If you expect to query by the value often, Text is usually the right type.
Text Area
Text Area is a 255-character field that shows as a multi-line box. Use it when a value is short but has line breaks, such as a mailing address.
It cannot be unique or an external ID. The extra lines are line-feed characters inside the string, so a value with three lines uses up part of the 255 characters.
Text Area (Long)
Long Text Area holds up to 131,072 characters. You set two values:
- Length: the maximum number of characters stored.
- Visible lines: how many lines the edit box shows before it scrolls. This setting changes the layout only, not the storage.
Long text trades capability for size. Expect these restrictions:
- You cannot use it in a SOQL
WHEREclause,ORDER BY, orGROUP BY. - It cannot be unique, an external ID, or indexed for filtering.
- It has limits in reports and formulas.
- Salesforce limits the number of long and rich text fields per object, and the total character count across them.
Because you cannot filter on it, you cannot write a query such as "find records whose Notes contain this phrase". Use SOSL or a search for that instead. If you need to filter on part of the text, store that part in a separate Text field.
Text Area (Rich)
Rich Text Area adds an editor with bold, italics, lists, links, and uploaded images. The stored value is HTML, so the length limit counts the markup as well as the visible text. A short formatted paragraph can use more characters than you expect.
The same query restrictions as Long Text Area apply. Two more points matter in practice:
- Salesforce sanitizes the HTML and keeps only a set of allowed tags.
- Code that reads the field gets HTML. If you want to show it in a place that does not render HTML, convert it first.
Case c = [SELECT Id, Description FROM Case LIMIT 1];
String plain = c.Description == null ? '' : c.Description.stripHtmlTags();
System.debug(plain);Case.Description is a plain Long Text Area, so stripHtmlTags() changes nothing there. On a rich text field of your own, it returns the text without tags.
Use Rich Text Area only when users need formatting. Rich text pasted from a word processor or web page often brings in unwanted styles, and it makes reports and exports harder to read.
Choosing among the four
Two further questions override this path:
- Will you filter, sort, or match on it? If yes, stay with Text, even when you must shorten the data or split it into several fields.
- Must it be unique or an external ID? Only Text supports both.
Text fields in metadata and Apex
In metadata, the field type name does not always match the label you see in the UI.
| UI label | Metadata `type` |
|---|---|
| Text | `Text` |
| Text Area | `TextArea` |
| Text Area (Long) | `LongTextArea` |
| Text Area (Rich) | `Html` |
A Long Text Area field looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Internal_Notes__c</fullName>
<label>Internal Notes</label>
<type>LongTextArea</type>
<length>32768</length>
<visibleLines>6</visibleLines>
</CustomField>A Text field uses <length> with a value up to 255 and has no visibleLines element.
In Apex, all four types are String values. Describe calls tell them apart:
Schema.DescribeFieldResult name = Account.Name.getDescribe();
System.debug(name.getType()); // STRING
System.debug(name.getLength()); // 255
Schema.DescribeFieldResult descr = Account.Description.getDescribe();
System.debug(descr.getType()); // TEXTAREA
System.debug(descr.isHtmlFormatted()); // falseLong and rich text areas both report TEXTAREA. Use isHtmlFormatted() to tell them apart.
Common errors
- Choosing Text Area for long content. A user pastes 400 characters and gets a "data value too large" error. Check the real maximum length before you pick the type.
- Assuming rich text counts visible characters. The limit includes tags, so formatted content reaches it sooner.
- Building a report or SOQL filter on a long field. Plan a short searchable companion field at design time.
- Setting Text length too small. Increasing a length later is usually easy. Decreasing it can fail or risk data loss, which the chapter on changing field types covers.
Exercise
A support team wants to store troubleshooting steps with numbered lists and links, up to about 5,000 characters. Managers also want to list cases filtered by a short "Issue Code" of up to 20 characters. Which field types should you use, and why?
Show answer
Use a Text Area (Rich) field for the troubleshooting steps, because users need lists and links and 5,000 characters fits well within the limit, even after HTML markup is counted. Use a separate Text field for the Issue Code. Text supports filtering, sorting, and grouping and can be made unique or an external ID if needed, while rich text cannot be used in a SOQL WHERE clause.