Sub topic 16
Permission Sets and Groups as Metadata
This chapter assumes you know how permission sets and permission set groups work in Setup, and that you have used a sandbox. Here you will see how both are stored as metadata files, so you can keep them in version control and deploy them between orgs.
Why treat access as metadata
Clicking through Setup works for one org. It does not work when you need the same access model in a sandbox, a scratch org, and production. Permission sets and permission set groups are metadata types, so you can retrieve them as XML files, review changes in a pull request, and deploy them like code.
A permission set file
A permission set file lists each grant as its own element. This one gives 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>The file name is the API name plus the suffix .permissionset-meta.xml, so this one is Account_Viewer.permissionset-meta.xml. In a source-format project it lives in force-app/main/default/permissionsets/.
A permission set group file
A group refers to its member permission sets by API name, not by label.
<?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 mute permissions, add a mutingPermissionSets element that names a muting permission set. Group files use the suffix .permissionsetgroup-meta.xml and live in the permissionsetgroups folder.
Deployment order
A permission set refers to objects and fields, and a group refers to permission sets. Anything a file names must exist in the target org, or be in the same deployment.
Assignments are data, not metadata. Deploying a permission set does not give it to any user. Assign it in the target org with Setup, Apex, or a data load.
Common errors
- Missing dependency. A deployment fails if a permission set names a field or Apex class that does not exist in the target org. Deploy those components first or in the same package.
- Unretrieved permissions. A retrieved permission set only contains permissions for the components you listed in the manifest. Retrieve the objects and fields together with the permission set, or you will get an incomplete file.
- Group status. After a deployment changes a group's members, the group recalculates. It shows Updating, then Updated. A Failed status means you need to fix the cause and recalculate.
- Profiles. Profile files behave differently. They merge into the target org and are often noisy. A permission-set-led model keeps most access in files that are easier to review.
Exercise
A permission set group deploys successfully to production, but users in the matching sandbox group have access and production users do not. What is the most likely cause?
Show answer
Permission sets and groups are metadata, but assignments are data, and deployment does not carry them. Assign the group to the users in production.