function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Banwari kevat1Banwari kevat1 

SOQL to find all Custom Permission name for a given profile.

Hi guys,
As we know we can easily find out all custom permissions for a user using SetupEntityAccess SAOP API.   
 But for a instance also as we can assign custom permissions to a profile from UI(profile daetail page). i want to access all custom permissions assigned to a profile.
Can anyone help me how to do.

Thanks,
Banwari
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Banwari,
public static List<User> getUsersWithCustomPermission(String name)
{
    Set<Id> permissionSetIds = new Set<Id>();
    for (SetupEntityAccess access : [
        SELECT ParentId 
        FROM SetupEntityAccess 
        WHERE SetupEntityId IN (
            SELECT Id 
            FROM CustomPermission 
            WHERE DeveloperName = :name
        )
    ]) {
        permissionSetIds.add(access.ParentId);
    }
    return permissionSetIds.isEmpty() ? new List<User>() : [
        SELECT Username FROM User WHERE Id IN (
            SELECT AssigneeId FROM PermissionSetAssignment
            WHERE PermissionSetId IN :permissionSetIds
        )
    ];
}

Please refer the below link for reference. Thanks 
Rahul
Banwari kevat1Banwari kevat1
Hi Rahul,
 Thanks for reply. But your reply is not the answer what i was asking. Please read question carefully. What answer you have given,I already mentioned in my question. I want to get all custom permissions assigned to profile not to user. You answered for User.
Thanks
Banwari
Milind Amin 9Milind Amin 9
What Rahul says is correct partially but you need to do little research over it. I did and got the answer after modifying query. Instead of PermissionSetAssignment, you need to use PermissionSet object to get list of profile.

SELECT DeveloperName, MasterLabel FROM CustomPermission

SELECT ParentId FROM SetupEntityAccess WHERE SetupEntityId IN (SELECT Id FROM CustomPermission WHERE DeveloperName = 'NAME')

SELECT Id, Label, ProfileId, Profile.Name, LicenseId, Name, IsCustom FROM PermissionSet WHERE ID = 'ParentId FROM above query'

Here you will get list of profile which has custom permission assigned. I hope you know how to write code now using above query.