After stumbling across this question (as well as the above linked question) in search of an answer, I think I've figured out two partial solutions that make a whole. Like you, I attempted to query FROM ObjectPermissions WHERE SObjectType = Task/Event/Activity
to no avail.
This query will pull all permission sets that have Activities access, and then grab all AssigneeIds
for User
records that have applicable PermissionSetAssignments
:
List<PermissionSetAssignment> permSetsWithAccess = [
SELECT AssigneeId
FROM PermissionSetAssignment
WHERE PermissionSetId
IN (SELECT Id FROM PermissionSet WHERE (PermissionsEditEvent = true OR PermissionsEditTask = true))
];
Optionally, you can add any of the following depending on what you are specifically looking for:
(PermissionsActivitiesAccess = true OR PermissionsEditEvent = true OR PermissionsEditTask = true)
Documentation for UserPermissionAccess here.
The second half of the equation is the following query for checking profile access:
List<User> usersWithAccessViaProfile = [
SELECT Id
FROM User
WHERE ProfileId
IN (SELECT Id FROM Profile WHERE (PermissionsEditEvent = true OR PermissionsEditTask = true))
];
From here, you can put iterate through the List<PermissionSetAssignment>
to put everything into a List<User>
, and then put both List<User>s
into a single set to get your users where access to specified activities is true
:
List<User> usersWithAccessViaPerms = new List<User>();
for(PermissionSetAssignment psa : permSetsWithAccess) {
usersWithAccessViaPerms.add(new User(Id=psa.AssigneeId));
}
Set<User> usersWithAccess = new Set<User>(usersWithAccessViaPerms)
usersWithAccess.addAll(usersWithAccessViaProfile);
return usersWithAccess;
Voila, you've now got a set of User
records that have access to the Task
or Event
objects.