Sub topic 16
Permission Sets and Groups as Metadata
This chapter assumes you can build permission sets and permission set groups in Setup and know the difference between object permissions and field-level security. Here you represent that access model as metadata files, so it can live in source control and deploy between orgs.
Why treat access as metadata
Permission sets and groups are metadata components. You can retrieve them as XML, review them in a pull request, and deploy them to a sandbox or production. This gives you three things that clicking in Setup does not:
- A history of who changed access and when.
- The same definition in every environment.
- A review step before a permission reaches production.
Assignments to users are not part of this. A PermissionSetAssignment is data, not metadata. Deploying a permission set does not give it to anyone.
The metadata types
| Setup concept | Metadata type | Typical file path |
|---|---|---|
| Permission set | `PermissionSet` | `force-app/main/default/permissionsets/Account_Viewer.permissionset-meta.xml` |
| Permission set group | `PermissionSetGroup` | `force-app/main/default/permissionsetgroups/Sales_Representative.permissionsetgroup-meta.xml` |
| Custom permission | `CustomPermission` | `force-app/main/default/customPermissions/Approve_Discounts.customPermission-meta.xml` |
| Profile | `Profile` | `force-app/main/default/profiles/Admin.profile-meta.xml` |
The file name is the API name. The label inside the file is what users see. Reference other components by API name, never by label.
A permission set in XML
The following permission set grants read access to Account and read-only field-level security on one field.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Account Viewer</label>
<description>Read-only access to Accounts and the Annual Revenue field</description>
<hasActivationRequired>false</hasActivationRequired>
<objectPermissions>
<allowCreate>false</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>false</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Account</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
<fieldPermissions>
<editable>false</editable>
<field>Account.AnnualRevenue</field>
<readable>true</readable>
</fieldPermissions>
</PermissionSet>Each kind of access maps to a repeating element:
objectPermissionsholds the six object-level flags for one object. One element per object.fieldPermissionsholdsreadableandeditablefor one field. The field name includes the object, as inAccount.AnnualRevenue.userPermissionsholds system permissions, each with anameandenabledvalue.customPermissionsenables custom permissions by name.applicationVisibilities,tabSettings,recordTypeVisibilities, andclassAccessescover apps, tabs, record types, and Apex classes.
A permission set group in XML
A group refers to its member permission sets by API name.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSetGroup xmlns="http://soap.sforce.com/2006/04/metadata">
<label>Sales Representative</label>
<description>Everything a sales rep needs</description>
<permissionSets>Account_Viewer</permissionSets>
<permissionSets>Opportunity_Editor</permissionSets>
</PermissionSetGroup>To add a muting permission set, add a mutingPermissionSets element that names it. A muting permission set is itself a MutingPermissionSet metadata component, stored in the mutingpermissionsets folder. It lists the permissions to remove, using the same element structure as a permission set.
Deploy order and dependencies
A permission set can only reference components that already exist in the target org. If Account_Viewer mentions Account.AnnualRevenue, and Opportunity_Editor mentions a custom field, that field must exist or be in the same deployment.
The usual dependency chain is:
- Objects and fields.
- Apex classes, custom permissions, record types, and tabs.
- Permission sets and muting permission sets.
- Permission set groups.
When you deploy with Salesforce CLI, a single deployment of a source directory sorts these dependencies for you. Problems appear when you deploy a partial manifest that includes the group but leaves out a member. Deploying a group that names a missing permission set fails.
sf project deploy start --source-dir force-app/main/default/permissionsets --source-dir force-app/main/default/permissionsetgroups --target-org my-sandboxTo pull a permission set from an org into your project, name it in the retrieve command.
sf project retrieve start --metadata PermissionSet:Account_Viewer --target-org my-sandboxRecalculation after deploy
When a deployment changes a permission set that belongs to a group, the group is recalculated. While that runs, the group status is Updating. It returns to Updated when it finishes. Recalculation can take longer in orgs with many assignments, so do not run data loads or tests that depend on the new access until the status is Updated. If a deployment leaves a group in Failed, fix the cause and resave or redeploy the group.
Assignments are data
Deployment moves definitions. To give a user access, you insert a PermissionSetAssignment record. This Apex does that for one user:
PermissionSet ps = [
SELECT Id
FROM PermissionSet
WHERE Name = 'Account_Viewer'
LIMIT 1
];
User u = [SELECT Id FROM User WHERE Username = '[email protected]' LIMIT 1];
insert new PermissionSetAssignment(
AssigneeId = u.Id,
PermissionSetId = ps.Id
);A permission set group is also assigned through this object. Its PermissionSetGroupId field takes the group's Id instead.
Because assignments are data, they differ between orgs. A sandbox refresh copies assignments from production, but a deployment into production does not create them. Teams commonly keep a small post-deployment script, or use a user-provisioning process, to assign access in each org.
To confirm what is assigned after a deploy, run this query. It leaves out the hidden permission set that Salesforce keeps for every profile.
SELECT Assignee.Name, PermissionSet.Name, PermissionSet.Label
FROM PermissionSetAssignment
WHERE PermissionSet.IsOwnedByProfile = false
ORDER BY Assignee.NameCustom permissions travel with the metadata
A custom permission is a metadata component you create once and enable in permission sets. Code then checks it by API name.
if (FeatureManagement.checkPermission('Approve_Discounts')) {
// Show or run the discount approval logic
} else {
// Hide the feature or return an error
}The check reads true when the user has the permission through any profile, permission set, or group. If the custom permission is missing in the target org, the permission set that enables it fails to deploy. Deploy the CustomPermission first or in the same package.
Profiles are harder to deploy
Profile files are large and behave differently from permission sets. A retrieved profile contains only the components in the retrieve request, so two retrievals of the same profile can differ. Merges produce conflicts, and a deploy can overwrite settings you did not intend to change. This is a practical reason teams move access into permission sets and keep profiles small, as covered in the minimum access chapter.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Deploy fails with "no such field" in a permission set | Field is not in the target org or the deployment | Include the field, or deploy it first |
| Group deploy fails on a member | Member permission set missing | Deploy the permission sets with the group |
| User lacks access after deploy | Nothing was assigned | Insert a `PermissionSetAssignment` |
| Access is not yet effective | Group is Updating | Wait for status Updated |
| Permission present in source but not in org | Permission set requires a license the org lacks | Check the permission set license and user license |
| Muted permission still works | It comes from the profile, not from a group member | Muting affects only permission sets in that group |
Exercise
You deploy a permission set group and its member permission sets to production. Testers report that nobody can see the new objects. The deployment succeeded and the group status is Updated. What is the most likely cause, and how do you fix it?
Show answer
Deployment moves definitions, not assignments. No PermissionSetAssignment records exist for the group in production. Assign the group to users in Setup, or insert PermissionSetAssignment records with the group's Id in PermissionSetGroupId, then verify with a SOQL query on PermissionSetAssignment.
Exercise
A permission set in your repository enables the custom permission Approve_Discounts. Deployment to a new sandbox fails. Name two possible reasons and the order of deployment that avoids them.
Show answer
The CustomPermission component may be missing from the sandbox, or the deployment manifest may omit it. Deploy the custom permission, along with any referenced fields, objects, and classes, before the permission set, then deploy the permission set, then the group that contains it.