Sub topic 15
Assigning and auditing access in Setup, Apex, and SOQL
This chapter assumes you can build a permission set and a permission set group, and that you know a user has exactly one profile. Here you will give those permission sets and groups to users, then check who holds what.
What an assignment is
Each assignment is one PermissionSetAssignment record. It links a user (AssigneeId) to either a permission set (PermissionSetId) or a permission set group (PermissionSetGroupId). A user's effective access is the profile plus every assignment.
Assigning in Setup
There are two directions:
- From the user: open the user record, find Permission Set Assignments, and click Edit Assignments. Use this to change one person.
- From the permission set or group: open it and click Manage Assignments, then Add Assignments. Use this to give many users the same access.
Assigning with Apex
The Apex below assigns a permission set group by its developer name. Querying PermissionSetGroup returns its Id.
PermissionSetGroup grp = [
SELECT Id
FROM PermissionSetGroup
WHERE DeveloperName = 'Sales_Representative'
LIMIT 1
];
User u = [SELECT Id FROM User WHERE Username = '[email protected]' LIMIT 1];
insert new PermissionSetAssignment(
AssigneeId = u.Id,
PermissionSetGroupId = grp.Id
);To assign a single permission set, set PermissionSetId instead, as in the earlier example. Set only one of the two fields on each record.
Inserting an assignment that already exists fails with a duplicate error. Query existing assignments first when you run the code repeatedly.
Auditing with SOQL
List who holds which permission sets and groups. Filtering on IsOwnedByProfile = false removes the hidden permission set that Salesforce keeps for every profile.
SELECT Assignee.Name, PermissionSet.Name, PermissionSet.Label,
PermissionSetGroupId
FROM PermissionSetAssignment
WHERE PermissionSet.IsOwnedByProfile = false
ORDER BY Assignee.NameEach permission set group has its own backing permission set, so group assignments appear in these results too. Rows with a value in PermissionSetGroupId come from a group. Assignments to the member permission sets of a group do not appear as separate rows for that user.
To answer "who can do this?", filter on one permission set:
SELECT Assignee.Name, Assignee.Username
FROM PermissionSetAssignment
WHERE PermissionSet.Name = 'Account_Viewer'For access held through a profile, query User by Profile.Name. A complete audit needs both queries.
Common errors
| Symptom | Likely cause |
|---|---|
| `MIXED_DML_OPERATION` | Setup and non-setup DML in one transaction |
| Duplicate assignment error | The user already has that permission set or group |
| Assignment is rejected | The user's license does not match the permission set license or user license it requires |
| No query rows for a profile | `IsOwnedByProfile = false` filtered it out |
Exercise
A query for a user's permission sets shows no rows for the permission sets inside the Sales Representative group, yet the user has that group assigned. Why, and how do you confirm the group assignment?
Show answer
The user is assigned to the group, not to each member permission set. Salesforce stores one PermissionSetAssignment with PermissionSetGroupId set. Query PermissionSetAssignment for that user and look for a row where PermissionSetGroupId is not null.