3

I am trying to write a SOQL query to find a list of Users who have read access to the standard Task object but my query keeps coming back empty. Is there a better way to do this?

I have a dropdown list populated with Users but they need to all have read permissions to the Task object.

Here is what I currently have that is returning 0 results:

SELECT AssigneeId 
FROM PermissionSetAssignment 
WHERE PermissionSetId 
IN (
    SELECT ParentId 
    FROM ObjectPermissions
    WHERE PermissionsRead = true
    AND SObjectType = 'Task'
)

2 Answers 2

1

Your query is only querying against PermissionSetAssignment. Do you want to see user's that have permission sets with read access to Task objects or are you looking for any user who's profile has read access?

If you want to see profiles with read access you can use:

SELECT Parent.Profile.Name 
FROM ObjectPermissions
WHERE Parent.IsOwnedByProfile = true 
AND SObjectType = 'MyObject__c'

From there it would be trivial to query for users with the profiles that are returned.

This post asked a similar question (from which the answer was sourced).

1
  • Thanks, I looked at that post earlier but all the examples provided give me no results when I put "Task" as the object, even your example. Not sure why its empty on Task and not for any other object like Account
    – 626
    Commented Dec 20, 2019 at 16:47
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .