Sub topic 15
Encrypted text and protecting sensitive fields
This chapter assumes you know how to create custom fields in Object Manager. It also assumes you know what field-level security (FLS) and permission sets do. It covers the Encrypted Text field type, what it protects and what it doesn't, and how it relates to other ways of protecting sensitive data.
What the Encrypted Text field is
Encrypted Text is a custom field type for short, sensitive strings such as a national ID number, a partial card number, or an access code. Salesforce stores the value encrypted with 128-bit AES. Users see it masked, unless their profile or permission set grants View Encrypted Data.
Key properties:
- The maximum length is 175 characters.
- You choose a mask character, either an asterisk (
*) orX. - You choose a mask type: all characters, last four characters, credit card number, Social Security number, Social Insurance number, or National Insurance number.
- The mask is a display feature. It does not stop a user from editing the field.
Because the field is encrypted at rest and masked on screen, it fits data that people need to enter or confirm but rarely need to read in full.
Limits you will run into
Encrypted fields give up several features that plain Text fields have. Plan for these before you pick the type.
| Capability | Encrypted Text |
|---|---|
| Unique or External ID attribute | Not available |
| Default value | Not available |
| Filtering in list views, reports, and SOQL `WHERE` | Not available |
| Roll-up summary criteria | Not available |
| Length | 175 characters maximum |
| Conversion to or from another type | Not available |
The last row matters most for later maintenance. You cannot change an existing Text field to Encrypted Text, or the reverse. To move data you create a new field, copy the values with a data load or script, and repoint any validation rules, page layouts, and code. Chapter material on changing field types covers that process in general.
The missing filter support also means you cannot look up a record by its encrypted value. If you need to find a person by tax ID, an encrypted field is the wrong place for it. A common pattern is a separate searchable field that holds a non-sensitive reference, such as a customer number.
Layering protection
No single setting protects a field. Use the layers in order.
- Object permissions and sharing decide whether a user can see the record at all.
- Field-level security decides whether the user can see or edit the field. Remove the field from profiles and permission sets that don't need it.
- Masking and View Encrypted Data decide whether visible users see the clear value.
- Page layouts and record types hide the field from screens. They are a convenience, not a security measure, because the field stays reachable through the API and reports.
Grant View Encrypted Data through a dedicated permission set, not a broad profile. That keeps the list of people who can read clear values short and easy to audit.
Encrypted Text versus Shield Platform Encryption
The two features have similar names and different purposes.
| Row | Encrypted Text field | Shield Platform Encryption |
|---|---|---|
| What it is | A dedicated custom field type | A feature that encrypts existing fields |
| Which fields | Only fields created as Encrypted Text | Many standard and custom field types, plus files and attachments |
| Extra license | No | Yes |
| Key management | Managed by Salesforce | Tenant secrets, with options to bring your own key |
| Filtering and search | Not available | Limited, depending on the scheme (deterministic encryption allows exact-match filtering) |
If you only need to mask one short field and have no compliance requirement for encrypting standard fields, Encrypted Text is enough. If a regulation requires encryption of fields such as Email, Phone, or long text, or of files, you need Shield. Shield avoids the type-change problem, because you turn encryption on for an existing field.
Seeing the type in metadata and Apex
In metadata, an Encrypted Text field declares its type, length, and mask. Here is a field that shows only the last four characters:
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>Tax_Id__c</fullName>
<label>Tax ID</label>
<type>EncryptedText</type>
<length>20</length>
<maskChar>asterisk</maskChar>
<maskType>lastFour</maskType>
<required>false</required>
</CustomField>In Apex, a describe call reports the type as ENCRYPTEDSTRING. Checking the filterable flag confirms the query limits described above. The example assumes a custom field Tax_Id__c on Contact.
Schema.DescribeFieldResult tax = Contact.Tax_Id__c.getDescribe();
System.debug(tax.getType()); // ENCRYPTEDSTRING
System.debug(tax.getLength()); // 20
System.debug(tax.isFilterable()); // falseUse this check in code that builds queries or reports dynamically. It lets you skip encrypted fields in filter clauses instead of failing at runtime.
Common mistakes
- Choosing Encrypted Text for a field that must be unique. It cannot be unique or an External ID. Store a separate non-sensitive key for matching.
- Expecting reports to filter on it. Report and list view filters ignore encrypted fields, so users find them missing from the filter picker.
- Forgetting the 175-character limit. Long notes with sensitive content do not fit. Shield on a Long Text Area is the alternative.
- Granting View Encrypted Data too widely. Granting it by profile exposes clear values to every user on that profile.
- Creating the field as Text first and converting later. The conversion is not allowed, so decide before you create the field.
- Treating the mask as an edit lock. Remove edit access in FLS when users should only view.
Exercise
Your team stores a customer's tax ID on Contact. Support agents must confirm the last four digits on calls. Auditors must be able to search for a contact by full tax ID. Can a single Encrypted Text field satisfy both needs, and what would you do?
Show answer
No. Encrypted Text can mask the value to the last four characters for agents, but it cannot be filtered in SOQL, reports, or list views, and it cannot be unique or an External ID, so auditors could not search by full tax ID. Either use Shield Platform Encryption with a deterministic scheme, which allows exact-match filtering, or keep the tax ID in an Encrypted Text field and add a separate non-sensitive reference field for lookups. Restrict View Encrypted Data to a dedicated permission set for the auditors, and set the field read-only in FLS for agents.