4

I want to get the list of permission set assigned to loggedin user. And a conditional check one that.

If a user has 'TestABC' permission set assigned then boolean value should be true

I am using this approach

List<PermissionSetAssignment> lstcurrentUserPerSet =    [   SELECT Id, PermissionSet.Name,AssigneeId
                                                                FROM PermissionSetAssignment
                                                                WHERE AssigneeId = :Userinfo.getUserId() ];
system.debug('##lstcurrentUserPerSet' + lstcurrentUserPerSet);

for (PermissionSetAssignment psa: lstcurrentUserPerSet)
{
    system.debug('##psa.PermissionSet.Name' + psa.PermissionSet.Name);
    if(psa.PermissionSet.Name.equals('TestABC'))
                Test = true;
    else
            Test = false;
}

I am not able to get any values in debug log. The list is not retreving any value it seems

5
  • Are you have assigned any PS to your user? Are you sure PS name is correct? Apr 15 '14 at 11:58
  • Yes i have assigned PS to user. Is the Name the API Name
    – TestUser
    Apr 15 '14 at 12:02
  • Hmmmm, could you put there more debugs? User.Id for example. Because if you have PS assigned to user this query should return it... Also is this code run in trigger or controller? Apr 15 '14 at 12:16
  • Its in controller.. ok fine will put more debug and check
    – TestUser
    Apr 15 '14 at 12:24
  • It also could be help if you post controller code Apr 15 '14 at 12:42
4

Is there a requirement that you need all the Permission sets to be queried for some other use? Or else if its just to check why don't we directly use this query

SELECT count(Id) FROM PermissionSetAssignment WHERE AssigneeId = :Userinfo.getUserId() AND PermissionSet.Name = '-permission set api name here-'

If result = 1 means the permission set is assigned for current user else if result = 0 means its not.

1

I would have a look at your logic there, inside your for loop, you check to see if the name is equal, if it is then you set Test to true, else to false.

So if the user has 2 permission sets assigned, and the first one will set Test to true, the second will set it to false.

Boolean user_has_permission = false;
for ( PermissionSetAssignment psa : lstcurrentUserPerSet ) {
    System.debug('##psa.PermissionSet.Name' + psa.PermissionSet.Name);
    if ( psa.PermissionSet.Name.equals('TestABC') ) {
        user_has_permission = true;
    }
}

This will work, also ensure that you are using the API name of the Permission set in the case of TestABC

0

may be useful to others if they looking SOQL or APEX to achieve this count requirement,

http://theblogreaders.com/many-list-users-assigned-salesforce-permissionset/

Thanks & Regards, ​​​​​​​Sakthivel Madesh

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.