Sub topic 15
Encrypted text and protecting sensitive fields
This chapter assumes you know how to create custom fields in Object Manager and understand profiles and permission sets at a basic level. It covers the Encrypted Text field type and the access controls that protect sensitive data.
What an encrypted text field is
An Encrypted Text field stores text that Salesforce encrypts at rest. Users see the value masked with asterisks unless their profile or permission set has the "View Encrypted Data" permission. You choose a mask type and a mask character when you create the field.
Common uses are national ID numbers, license keys and other values you want hidden from most users.
Limits to know before you create one
Encrypted fields trade functionality for protection. The main restrictions are:
- The maximum length is 175 characters.
- You cannot use the field as a unique field or an external ID.
- You cannot use it in SOQL
WHEREfilters orORDER BYclauses, and reports and list views cannot filter on it. - You cannot use it in standard search criteria in the usual way.
- Formula fields can reference it only in limited ways, for example with
ISBLANKorISNULL. - You cannot change an existing field to or from the Encrypted Text type without data consequences.
Check the current Salesforce documentation for the full list, because the restrictions change between releases.
Field-level security comes first
Encryption masking is not the same as access control. Field-level security (FLS) decides whether a user can see or edit a field at all. Use both layers:
- Set FLS so only the profiles and permission sets that need the field can see it.
- Grant "View Encrypted Data" only to the smaller group that must read the clear value.
Classic encryption versus Shield Platform Encryption
Encrypted Text is the older, "classic" feature. It masks values and limits what you can do with them. Shield Platform Encryption is a separate paid add-on. It encrypts many standard and custom field types and supports more operations, such as searching on encrypted fields under certain conditions. If you need to protect fields like Email, Phone or Text Area, or you have compliance requirements, evaluate Shield instead of the Encrypted Text type.
Reading the field in Apex
The query below respects the running user's field access:
List<Contact> contacts = [
SELECT Id, Name, National_Id__c
FROM Contact
WITH USER_MODE
LIMIT 10
];
for (Contact c : contacts) {
System.debug(c.Name);
}If the user lacks read access to National_Id__c, the query throws an exception instead of returning the value. Catch it or check access with Schema.DescribeFieldResult.isAccessible() first.
Choosing this type
Use Encrypted Text when the value is short, you only need to display it, and you never need to filter or report on it. If you must search or filter on the value, choose a different design, such as a regular text field with tight FLS, or Shield encryption.
Exercise
A team wants to store a customer's passport number and let support agents look up customers by that number. Why is Encrypted Text a poor fit, and what are two alternatives?
Show answer
Encrypted Text fields cannot be used in SOQL WHERE filters, as unique fields or as external IDs, so agents cannot look customers up by that value. Alternatives are a regular Text field with strict field-level security, or Shield Platform Encryption on the field, which supports more operations on encrypted data.