Sub topic 16
Representing and Deploying FLS as Metadata
This chapter assumes you can set field access in Setup, you know what profiles and permission sets are, and you have used Salesforce DX or a change set at least once. It covers how field-level security (FLS) is stored as metadata, how to retrieve and deploy it, and the errors that tend to appear.
Where FLS lives in metadata
A field has no FLS settings of its own. The Read and Edit flags are stored on the containers that grant access. Each container holds one fieldPermissions entry per field.
| Metadata type | File location in a DX project | Holds FLS as |
|---|---|---|
| `PermissionSet` | `force-app/main/default/permissionsets/Name.permissionset-meta.xml` | `fieldPermissions` entries |
| `Profile` | `force-app/main/default/profiles/Name.profile-meta.xml` | `fieldPermissions` entries |
| `PermissionSetGroup` | `force-app/main/default/permissionsetgroups/Name.permissionsetgroup-meta.xml` | No FLS. It lists permission sets, and the combined access is calculated. |
| `CustomField` | `force-app/main/default/objects/Account/fields/Name__c.field-meta.xml` | No FLS. It defines the field only. |
Deploying a new custom field does not make it visible to anyone. When you create a field in Setup, you pick which profiles get access. A field deployed from source has no such step, so you must also deploy a permission set or profile that grants access. Users in profiles without a grant cannot see the field.
Anatomy of a fieldPermissions entry
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Account Notes Access</label>
<hasActivationRequired>false</hasActivationRequired>
<fieldPermissions>
<editable>true</editable>
<field>Account.Internal_Notes__c</field>
<readable>true</readable>
</fieldPermissions>
<fieldPermissions>
<editable>false</editable>
<field>Account.Region_Code__c</field>
<readable>true</readable>
</fieldPermissions>
<objectPermissions>
<allowCreate>false</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>true</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Account</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
</PermissionSet>Each entry has three elements:
fieldis the object API name and the field API name, joined with a dot. It is always in the formObject.Field__c.readableallows the user to see the field.editableallows the user to change it. It requiresreadableto betrue.
The combination editable=true, readable=false is invalid. Deployment fails with an error saying the field cannot be editable without being readable.
The objectPermissions block is included deliberately. FLS has no effect on an object the user cannot read. Metadata keeps the two in separate elements, so a permission set that grants a field but not the object will deploy cleanly and then appear to do nothing.
Fields you cannot set
Some fields do not accept fieldPermissions entries:
- Required fields. A field that is required at the database level, such as a required custom field or a master-detail relationship field, is always readable and editable wherever the object is accessible. Including it in a permission set causes a deployment error.
- Standard system fields such as
Id,CreatedDate, andOwnerIdon most objects. - Fields that are not permissionable, such as most formula-based or auto-number fields, only accept
readable. A formula field witheditable=trueis rejected.
If a deployment fails with a message that a field "is not permissionable" or "cannot be edited," remove the entry. The access it describes is already fixed.
Retrieving FLS with the Salesforce CLI
Retrieve a permission set by name:
sf project retrieve start --metadata "PermissionSet:Account_Notes_Access"Retrieve a profile by the same method, but be aware of a quirk. A profile retrieved on its own contains only a small subset of its settings. Profile files include fieldPermissions only for the fields and objects that are part of the same retrieve. To get a complete profile, retrieve it together with the objects whose fields you want to see:
sf project retrieve start --metadata "Profile:Standard User" --metadata "CustomObject:Account"This asymmetry is the main reason teams prefer permission sets for source control. A permission set file lists exactly the grants it owns. A profile file shows only a slice of what exists in the org.
Deploying FLS
Deploy a single permission set along with the field it grants:
sf project deploy start \
--source-dir force-app/main/default/objects/Account/fields/Internal_Notes__c.field-meta.xml \
--source-dir force-app/main/default/permissionsets/Account_Notes_Access.permissionset-meta.xmlThe order within one deployment does not matter, because the platform resolves dependencies. A permission set that references a field missing from both the deployment and the target org fails with In field: field - no CustomField named Account.Internal_Notes__c found.
The diagram below shows the dependency order the platform resolves.
User assignment is data, not metadata. Deploying a permission set never assigns it to anyone. Assign it with Setup, a PermissionSetAssignment record, or the CLI:
sf org assign permset --name Account_Notes_Access --target-org my-sandboxMerge behavior and destructive changes
Deployments of permission sets and profiles are additive for the entries in the file. Entries you omit are left alone. This has two consequences.
- Removing a
fieldPermissionsentry from a file and redeploying does not revoke access in the target org. To revoke, set both flags tofalseand deploy, or edit the permission set in Setup. - Deleting a field removes its
fieldPermissionsfrom every container automatically. You do not clean those up by hand.
Setting readable and editable to false is the explicit way to express "no access" in source, and it keeps a revoke visible in version control.
Permission set groups and the muting permission set
A permission set group combines several permission sets. The effective FLS for a user is the union of every grant. To remove a specific grant inside a group without editing the shared permission set, add a muting permission set to the group. A MutingPermissionSet file uses the same fieldPermissions element, but a readable or editable value of true means "mute this access" within that group.
<?xml version="1.0" encoding="UTF-8"?>
<MutingPermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Mute Internal Notes Edit</label>
<fieldPermissions>
<editable>true</editable>
<field>Account.Internal_Notes__c</field>
<readable>false</readable>
</fieldPermissions>
</MutingPermissionSet>Here editable=true mutes edit access and readable=false leaves read access unmuted. Muting affects only users reaching the access through that group. A user who also holds the permission set directly keeps the grant.
Common deployment errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `no CustomField named ... found` | The field is not in the deployment or target org | Include the field, or deploy it first |
| Field is not permissionable | The entry targets a required or system field | Remove the entry |
| Editable without readable | `editable=true` with `readable=false` | Set `readable` to `true` |
| Deploy succeeds, users see nothing | No object permission, or the permission set is not assigned | Add `objectPermissions` and assign the set |
| Profile deploy changes little | Profile retrieved without related objects | Retrieve with the objects, or move grants to a permission set |
| Revoked entry still grants access | Omitting an entry does not remove it | Deploy both flags as `false` |
Verifying after deployment
Confirm the result with the same checks you would use for any FLS change. Query the field permissions directly:
SELECT Parent.Name, Field, PermissionsRead, PermissionsEdit
FROM FieldPermissions
WHERE Field = 'Account.Internal_Notes__c'Run this in the Developer Console Query Editor or with sf data query. Each row is one grant, and Parent is the permission set or the profile's owning permission set. Compare the rows with your source file to confirm the deployment matches your intent.
Exercise
You deploy a new custom field Account.Contract_Value__c and a permission set that grants it read and edit access. The deploy succeeds, but a sales user reports they cannot see the field. List two causes you would check, and one way to confirm each.
Show answer
First, the permission set may not be assigned to the user. Confirm by querying PermissionSetAssignment for that user, or by opening the user's Permission Set Assignments in Setup. Second, the permission set may lack objectPermissions for Account, or the user's profile may not give Read on Account. Confirm by opening the permission set's Object Settings, or by querying ObjectPermissions. A third possibility is a muting permission set in a group the user belongs to, which you can find by inspecting the group's components.
Exercise
A teammate removes the fieldPermissions entry for Account.Internal_Notes__c from a permission set file and deploys it to production. Users still see the field. Why, and what should the file contain instead?
Show answer
Deployments only apply entries that are present, so omitting an entry leaves the existing grant in place. The file should contain the entry with readable and editable both set to false, which revokes the access and records the change in version control.