Sub topic 10
Assigning permission sets and licences to the new user
This chapter assumes you can create a user in Setup and know what a profile and a permission set are. You also need the Manage Users permission, or Assign Permission Sets. A new user has only the access their profile gives them. Extra access comes from permission sets, and some permission sets need a permission set licence first.
The order of assignment
A permission set that needs a permission set licence (PSL) cannot be assigned until the user holds that PSL. The user's user licence must also be compatible with the permission set. Work in this order:
- Create the user with a profile.
- Assign any required permission set licence.
- Assign the permission set or permission set group.
Check capacity first
Run this query in the Developer Console or Workbench before you assign a PSL.
SELECT MasterLabel, TotalLicenses, UsedLicenses
FROM PermissionSetLicense
ORDER BY MasterLabelIf UsedLicenses equals TotalLicenses, the assignment fails. You must free a licence by removing it from another user, or buy more.
Assign in Setup
- In Setup, open Users, then Users, and click the user's name.
- Find Permission Set License Assignments and click Edit Assignments. Select the licence and save.
- Find Permission Set Assignments and click Edit Assignments.
- Move the permission sets from Available Permission Sets to Enabled Permission Sets, then save.
Permission set groups appear in a separate related list, Permission Set Group Assignments. Prefer a group when a job role needs several permission sets together.
Assign with Apex
An assignment is a PermissionSetAssignment record. This snippet creates the user and links a permission set. Replace the permission set API name with a real one.
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);Apex handles PSLs the same way. Insert a PermissionSetLicenseAssign record first, with the user's ID as AssigneeId and the licence ID as PermissionSetLicenseId. Then insert the PermissionSetAssignment.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| The permission set is missing from the Available list | The user licence is not compatible with the permission set | Choose a permission set built for that licence, or change the profile's licence |
| Error about a required licence | The user does not hold the needed PSL | Assign the PSL first |
| Assignment fails with no licences available | `UsedLicenses` equals `TotalLicenses` | Free or buy capacity |
| Assignment fails in Apex | Duplicate assignment, or the PSL step was skipped | Query existing assignments and add the PSL |
Verify the result
Open the user record and review both related lists. You can also query the assignments.
SELECT PermissionSet.Name, PermissionSet.Label
FROM PermissionSetAssignment
WHERE Assignee.Username = '[email protected]'Every user has one assignment for the permission set that represents their profile. Its name starts with X00e, and it appears in this query. Ignore it when you look for the permission sets you assigned.
Exercise
A permission set you want to assign does not accept the user, and the PSL it needs is not yet held by the user. In what order do you work, and what do you check before assigning the PSL?
Show answer
Query PermissionSetLicense and confirm UsedLicenses is lower than TotalLicenses. Then assign the PSL to the user, and only then assign the permission set. If no capacity is left, free a licence from another user or buy more.
Exercise
Which object links a user to a permission set in Apex, and which two fields must you set?
Show answer
PermissionSetAssignment, with AssigneeId (the user's ID) and PermissionSetId.