Use Case
In Salesforce, encrypted fields provide an extra layer of security by allowing access only to users with the View Encrypted Data permission. In standard layouts and Lightning pages, Salesforce automatically manages the visibility of encrypted data based on a user's profile and permissions.
However, if you're building a Lightning Web Component (LWC) that displays Social Security Numbers (SSN), you must manually handle data masking. The goal is:
- ✅ If a user has View Encrypted Data permission, they should see the full SSN (e.g.,
113-212-4444
). - ❌ If a user does not have View Encrypted Data permission, they should see only the last 4 digits, with the rest masked (e.g.,
***-***-4444
).
Since LWC does not inherently handle encrypted field visibility, we need to manage this logic in Apex before sending the formatted SSN to the component.
The best way to handle this is to:
- Check if the user has View Encrypted Data permission.
- Format the SSN based on the user's permission.
- Send the formatted SSN to LWC for display.
1️⃣ Utility Method to Check "View Encrypted Data" Permission
The following Apex method verifies if a user has the View Encrypted Data permission. If they do, they can see the full SSN; otherwise, we restrict it.
- The method queries the PermissionSetAssignment object to check if the View Encrypted Data permission is assigned to the user.
- If the permission exists, it returns true; otherwise, it returns false.
2️⃣ Utility Method to Format SSN Based on User Permission
Once we determine if the user has permission, we format the SSN accordingly before sending it to the LWC component.
- If the user does not have View Encrypted Data the function masks the first 5 digits (***-***-) and only shows the last 4 digits.
- If the user has permission, it returns the full SSN.
3️⃣ Apex Controller to Fetch Formatted SSN for LWC
- Retrieves the Lead's SSN field from Salesforce Checks whether the current user has the View Encrypted Data permission
- Formats the SSN accordingly before returning it to the LWC component.
4️⃣ LWC Component To Display SSN
🔹 leadSSN.js
leadSSN.html
<template> <lightning-card title="Lead SSN"> <div class="slds-p-around_medium"> <p><b>SSN:</b> {formattedSSN}</p> </div> </lightning-card></template>📌 Final Steps: Deploy & Test
✅ 1️⃣ Deploy the LWC Component
- Add the LeadSSN component to the Lead Record Page.
✅ 2️⃣ Assign the Lightning Page to Users
- Ensure Lightning Pages are enabled for all profiles.
✅ 3️⃣ Test in Different User Profiles
- Login as a user with View Encrypted Data → See full SSN.
- Login as a user without View Encrypted Data → See masked SSN.
🎯 Conclusion
In standard Salesforce layouts, encrypted fields are automatically handled.
However, when displaying sensitive data in an LWC component, you must:
- Check if the user has View Encrypted Data permission.
- Format the data accordingly in Apex before passing it to LWC.
- Ensure secure handling by displaying only the last 4 digits when necessary.
This approach ensures compliance with data security policies while allowing
authorized users to view full SSNs.