Sub topic 5
Granting access with permission sets
This chapter assumes you can administer a Salesforce org (users, profiles, objects, fields), read basic SOQL, and have admin rights in a sandbox or Developer Edition org. It covers how to give the integration user its access through permission sets, and which mistakes to avoid.
Why permission sets carry the access
A profile is assigned to a user exactly once, and every user must have one. A permission set is an additive bundle of permissions. A user can have many of them. Permission sets can only grant access, never remove it. The user's effective access is the union of the profile and all assigned permission sets.
That union rule shapes the whole design:
- The profile should be as empty as possible, so it grants almost nothing.
- Each permission set should describe one job, such as "read Accounts" or "create Cases".
- To remove access, you remove a permission set assignment, or you remove a permission from the profile or the permission set that grants it.
If you put the integration's rights in the profile, you cannot reuse them for a second integration. You also cannot see at a glance what that one integration may do. A dedicated permission set is named, versioned in source control, and easy to compare with the integration's written requirements.
What a permission set contains
A permission set can grant these kinds of access:
| Section in metadata | What it controls | Typical integration value |
|---|---|---|
| `objectPermissions` | Read, create, edit, delete, View All, Modify All on an object | Only the flags the integration uses |
| `fieldPermissions` | Read and edit on individual fields | Only the fields the integration touches |
| `userPermissions` | System permissions such as `ApiEnabled` | `ApiEnabled` and nothing else, unless justified |
| `classAccesses` | Which Apex classes the user may run | Only classes the integration calls |
| `customPermissions` | Custom permission flags checked by your code | Only if your code checks them |
| `tabSettings`, `applicationVisibilities` | UI visibility | None, since the user never signs in to the UI |
An API-only user needs no tabs, apps, or page layouts. Leave them out.
A minimal permission set
The following permission set is in metadata format. It grants API access, read access to the Account object, and read access to one field. It has no create, edit, or delete rights, and no View All or Modify All.
<?xml version="1.0" encoding="UTF-8"?>
<PermissionSet xmlns="http://soap.sforce.com/2006/04/metadata">
<description>Read-only access to Account for the integration user</description>
<fieldPermissions>
<editable>false</editable>
<field>Account.Industry</field>
<readable>true</readable>
</fieldPermissions>
<hasActivationRequired>false</hasActivationRequired>
<label>Integration Account Read</label>
<objectPermissions>
<allowCreate>false</allowCreate>
<allowDelete>false</allowDelete>
<allowEdit>false</allowEdit>
<allowRead>true</allowRead>
<modifyAllRecords>false</modifyAllRecords>
<object>Account</object>
<viewAllRecords>false</viewAllRecords>
</objectPermissions>
<userPermissions>
<enabled>true</enabled>
<name>ApiEnabled</name>
</userPermissions>
</PermissionSet>A few details are worth noting.
- The
Account.Industryfield permission is explicit. Standard fields such asNameare readable once the user can read the object. Many custom fields are not, until you grant them. - Writing
falsefor permissions you do not want is optional, because omitted flags default to false. The explicit form makes a review easier, since a reader sees that the omission was deliberate. hasActivationRequiredisfalse, so the permission set applies as soon as you assign it. Session-based permission sets, which need activation, are for human users who step up their access temporarily. They add nothing for an unattended integration.- The permission set has no name in the file. The file name sets the API name, so saving it as
Integration_Account_Read.permissionset-meta.xmlgives it the nameIntegration_Account_Read, which the later queries use.
Deploying and assigning it
Deploy the file with Salesforce CLI from a project that contains it under force-app/main/default/permissionsets/. Then assign it to the integration user.
sf project deploy start \
--source-dir force-app/main/default/permissionsets/Integration_Account_Read.permissionset-meta.xml \
--target-org my-sandbox
sf org assign permset \
--name Integration_Account_Read \
--on-behalf-of [email protected] \
--target-org my-sandboxYou can also create the permission set in Setup under Permission Sets and assign it from Manage Assignments. The metadata route is better for a real integration. It gives you a reviewable file, and you can deploy the same definition to each sandbox and to production without clicking through the screens again.
Permission set licenses and the user license
A permission set can only contain permissions that the user's license allows. A permission set that you leave without a license works for any user license, but it cannot grant a permission the user's license does not include. If a permission set contains something the license does not allow, the assignment fails with an error about the license.
Some features need a separate permission set license, which you assign in addition to the permission set. Check whether your integration needs one before you debug a failed assignment. Most basic object access does not need one.
Permission set groups
A permission set group bundles several permission sets and assigns them together. It is useful when one integration needs several unrelated jobs, such as "read Accounts" and "create Cases", and you want one assignment per user.
Groups also have a muting permission set. A muting permission set removes specific permissions from the group's combined result, but only within that group. This is the one place where something other than removing the grant takes access away.
For a single integration user, a group is often more than you need. Use one when the same combination applies to several integration users, or when the number of separate assignments becomes hard to review. Keep in mind that a group makes the effective access harder to read, because you now have to expand the group and then apply the muting set.
Designing the set of permission sets
One approach that works for most teams is to split permission sets by capability, not by integration.
- Create a permission set per object-and-action group, such as
Integration_Account_ReadorIntegration_Case_Create. - Put
ApiEnabledin a separate permission set, such asIntegration_Api_Access, assigned to every API-only integration user. - Assign to each integration user only the capabilities it needs.
The alternative is one permission set per integration, such as Billing_Sync_Access. This is simpler to read and to retire along with the integration, and it is a good choice when each integration has a different set of needs. The cost is duplication. If two integrations both read Accounts, the same object permission appears in two places, and the two copies can drift apart.
The diagram shows how the pieces combine into the access the integration user actually has.
Common mistakes
- Using View All or Modify All for convenience. These flags ignore sharing and field-level restrictions on records of that object. An integration that needs to read all Accounts can use View All on Accounts, but you should confirm that it really needs every record. Modify All on an object is almost never justified for an integration.
- Granting View All Data or Modify All Data. These system permissions apply to every object. They are the quickest way to get an integration working and the quickest way to give it too much.
- Leaving permissions on the profile. If you clone the Standard profile and forget to strip it, the permission sets are not the real source of access. Check the profile too.
- Granting delete by default. Add delete only when the integration deletes records, and think about whether a soft-delete field would work instead.
- Giving edit on fields the integration only reads. Field permissions have separate read and edit flags. Grant edit only on the fields it writes.
- Forgetting the dependencies. To create a Case that references an Account, the integration needs read access to Account. To write to a lookup field, it needs access to the field and to the object it points to. If a call fails with an insufficient access error, work out which of these is missing before you widen anything.
- Granting
ApiEnabledthrough the profile, then forgetting about it. Prefer one permission set that holds it, so a review has one place to look.
Checking what you granted
After assigning, inspect the result with SOQL. First, list every permission set the user holds. The result includes the hidden permission set that backs the profile, marked by IsOwnedByProfile.
SELECT PermissionSet.Name, PermissionSet.Label, PermissionSet.IsOwnedByProfile
FROM PermissionSetAssignment
WHERE AssigneeId = '005XXXXXXXXXXXXXXX'Then look at the object permissions in one set. Scan for any Delete, View All, or Modify All value that is true and should not be.
SELECT SobjectType, PermissionsRead, PermissionsCreate, PermissionsEdit, PermissionsDelete, PermissionsViewAllRecords, PermissionsModifyAllRecords
FROM ObjectPermissions
WHERE Parent.Name = 'Integration_Account_Read'Replace the placeholder user Id with the Id of your integration user. Later chapters use the same queries again when they cover verification and access reviews.
Exercise
An integration must read Account records and create Case records that link to an Account. A teammate proposes one permission set that grants read, create, edit, and delete on both objects, plus Modify All Data, "so it never fails." List what you would change, and name the minimum object permissions the integration needs.
Show answer
Remove Modify All Data, because it grants write access to every object and is not needed. Remove edit and delete on both objects, because the integration neither edits nor deletes. Remove create on Account, because it only reads Accounts. The minimum is read on Account, plus read and create on Case, with read and edit on only the specific fields the integration touches. Add ApiEnabled, ideally in a separate permission set. Read access to Account is required anyway, because a Case that references an Account needs that object to be readable.
Exercise
You assign a permission set containing a permission to the integration user, but a later query shows the user still cannot read a custom field on an object it can read. Name two places to check.
Show answer
Check that the permission set has a fieldPermissions entry with readable set to true for that field, because object read access does not grant access to custom fields. Then check the other permission sets and the profile for conflicts in your understanding, since permissions only add up and cannot deny. If the field is still not visible, confirm the user's license supports the object and that the field is not hidden by something else, such as a restriction in the field's own setup.