Sub topic 10
Assigning Permission Sets and Licences to the New User
This chapter covers what happens after a user record exists: giving that user more access with permission sets, permission set groups and permission set licences. It assumes you can already create a user in Setup, that you know what a profile and a user licence are, and that you have the Manage Users permission or equivalent. The optional code sections assume basic SOQL and Apex.
Why assignment is a separate step
A new user starts with whatever the profile grants. The profile is tied to the user licence, and each user has exactly one. Permission sets add access on top of the profile. They never remove access, so a permission set cannot make a user more restricted than their profile allows.
Some permission sets also need a permission set licence (PSL). A PSL is a separate licence that unlocks a specific feature, such as a add-on product. The order of work is therefore:
- Confirm the user exists and is active.
- Assign any permission set licence the permission set needs.
- Assign the permission set or permission set group.
- Test as the user.
What a permission set can and cannot do
A permission set is compatible with a user only if the user's licence supports the permissions inside it. If a permission set is tied to a specific user licence, you can assign it only to users with that licence. If it has no licence, it can go to users with different licences, but permissions the licence does not allow are not usable.
| Item | What it controls | Limited by |
|---|---|---|
| User licence | The baseline set of features and objects available | Org's purchased count |
| Profile | Baseline permissions, one per user | The user licence |
| Permission set | Extra permissions, many per user | The user licence, and sometimes a PSL |
| Permission set group | A bundle of permission sets assigned as one | Same rules as its members |
| Permission set licence | Unlocks a named feature | Org's purchased count |
Check PSL capacity first
Before assigning a permission set that requires a PSL, check how many remain. This query comes from the earlier capacity chapter.
SELECT MasterLabel, TotalLicenses, UsedLicenses
FROM PermissionSetLicense
ORDER BY MasterLabelThe remaining count is TotalLicenses minus UsedLicenses. If it is zero, the assignment fails. You can free a seat by removing the PSL from another user or by buying more.
Assigning a permission set in Setup
- From Setup, enter Users in Quick Find and select Users.
- Click the name of the user.
- In the Permission Set Assignments related list, click Edit Assignments.
- Move the permission sets you want from Available Permission Sets to Enabled Permission Sets.
- Click Save.
You can also start from the permission set. Open Permission Sets, choose the set, click Manage Assignments, then Add Assignments. This route is quicker when you give the same set to many people.
Permission set groups are assigned the same way. Open the user, go to Permission Set Group Assignments, and click Edit Assignments. A group is better than assigning five separate sets when those five always go together, for example for a job role.
Assigning a permission set licence
Permission set licences are assigned from the user detail page, in the Permission Set License Assignments related list. Click Edit Assignments, select the licence, and save.
You often do not need to do this by hand. When a permission set requires a PSL and you assign that set, Salesforce can assign the PSL automatically if one is available. If none is available, you see an error and the assignment is not saved. Assigning the PSL first, as in the diagram, makes the cause of any failure clear.
Removing a PSL has a dependency rule. You cannot remove a PSL from a user while that user still has a permission set that requires it. Remove the permission set first, then the PSL.
Assigning with Apex
For automation, such as a post-install script or a test data setup, you create a PermissionSetAssignment record. This example creates the user and then links one permission set. Replace Your_Permission_Set_API_Name with a real API name, which you can find on the permission set detail page.
Profile p = [SELECT Id FROM Profile WHERE Name = 'Standard User' LIMIT 1];
User u = new User(
FirstName = 'Ada',
LastName = 'Lovelace',
Alias = 'alove',
Email = '[email protected]',
Username = '[email protected]',
ProfileId = p.Id,
TimeZoneSidKey = 'America/Los_Angeles',
LocaleSidKey = 'en_US',
EmailEncodingKey = 'UTF-8',
LanguageLocaleKey = 'en_US'
);
insert u;
PermissionSet ps = [SELECT Id FROM PermissionSet WHERE Name = 'Your_Permission_Set_API_Name' LIMIT 1];
insert new PermissionSetAssignment(AssigneeId = u.Id, PermissionSetId = ps.Id);Two points matter here.
- The query uses the
Namefield, which is the API name. The label shown in the UI isLabel. Mixing them up returns no rows, andps.Idthen throws aQueryExceptionbecause the list is empty. - Inserting a
Userand a setup object such asPermissionSetAssignmentin the same transaction can raise a mixed DML error when it runs in some contexts, for example in a test method or a trigger on a non-setup object. In those cases, run the setup DML in a separate transaction, such as a@futuremethod, or useSystem.runAsin tests.
For a permission set that needs a PSL, insert a PermissionSetLicenseAssign record first, using the user's ID and the PermissionSetLicenseId.
Verifying the result
Check the assignment with a query rather than relying on memory.
SELECT PermissionSet.Name, PermissionSet.Label, Assignee.Username
FROM PermissionSetAssignment
WHERE Assignee.Username = '[email protected]'Every user also has one assignment row for the profile's own hidden permission set, which has PermissionSet.IsOwnedByProfile = true. Add AND PermissionSet.IsOwnedByProfile = false to hide it.
To see what the user actually experiences, use Log in as from the user detail page, if your org allows administrators to do so. This is the most direct way to confirm that tabs, objects and fields appear.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Permission set does not appear in Available list | Its licence does not match the user's licence | Pick a set made for that licence, or a set with no licence |
| "No more licenses available" on assignment | PSL count is used up | Remove the PSL from an inactive or unneeded user, or buy more |
| Cannot remove a PSL | A permission set that needs it is still assigned | Remove that permission set first |
| User lacks access after assignment | Permission set group not yet Updated | Wait for the status, then retest |
| `QueryException` in Apex | Wrong API name in the `WHERE` clause | Use the `Name` value from the detail page |
Exercise
A user has the Standard User profile. You assign a permission set that grants access to a feature requiring a permission set licence. The assignment fails with a licence error. List the steps you take to diagnose and fix it.
Show answer
First query PermissionSetLicense and compare TotalLicenses with UsedLicenses for the required licence. If none remain, free one by removing the licence (after removing any permission set that depends on it) from a user who no longer needs it, or buy more. If seats remain, assign the PSL to the user first from the Permission Set License Assignments list, then assign the permission set again. Finally verify with a PermissionSetAssignment query or by using Log in as.
Exercise
Your Apex script inserts a User and then a PermissionSetAssignment in the same test method and fails with a mixed DML error. Name two ways to avoid the error.
Show answer
Run the setup-object DML in a separate transaction, for example in a @future method, or wrap the insert of one of the records in System.runAs so that it runs in a different context. Both approaches keep the User insert and the PermissionSetAssignment insert out of the same transaction.