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
Anish Mahadik 1Anish Mahadik 1 

Permission set coding for Custom button is not working.

Hi All,
I am trying to hide the custom button based on permission set.For that i already wrote a code but it is giving me an error.

 public void Custombutton()
         {
                for(PermissionSetAssignment permission : [SELECT Id, PermissionSet.Name FROM PermissionSetAssignment WHERE AssigneeId =:UserInfo.getUserId() ])
            {
                 permissionSetNameList.add(permission.PermissionSet.Name); 
                                          
            }          
            userPermissionList.addAll(permissionSetNameList);   
            
            for(User userIterator : [SELECT Id, Profile.Name FROM User WHERE Id =: UserInfo.getUserId()])
            {
                userProfileSet.add(userIterator.Profile.Name);
            }                 
            if( userPermissionList.contains('Test_Permission')== true)
            {
                relatedListFlag = true;       
            } 

I called the Custombutton() in controller .
Afte that i added the relatedListflag in rendered property of the button.

Vf Page Code
<apex:commandButton id="hideEditbtn1" Styleclass="btn btn-primary" action="{!makeFormEdit}" rendered="{!!isEditMode && relatedListFlag}" value="Edit" reRender="contentPanel,actProjHist,formId" oncomplete="loadToggle();hidePOC();showHeader();hideHelptext();"> </apex:commandButton> &nbsp;&nbsp;  

But when i tested the app with  Test_Permission . it is giving me an error
  System.NullPointerException: Attempt to de-reference a null object       

Please help me on this.

Thanks,
Anish  
 
Best Answer chosen by Anish Mahadik 1
Anish Mahadik 1Anish Mahadik 1
Hi All,

The code is totally correct. Just i have intitalize the permissionSetNameList field and added !not(relatedListFlag) in rendered attribue of VF page. It resolve my issue.

Thanks,
Anish

All Answers

Raj VakatiRaj Vakati
give me the complete code  .. i think its an initialization error. please initialize  all your variable like permissionSetNameList 
Anish Mahadik 1Anish Mahadik 1
Hi Raj,

 Public Set<String> permissionSetNameList;
    Public Set<String> userPermissionList;
    Public Set<String> userProfileSet;
    Public Boolean relatedListFlag {get;set;}
 public void Custombutton()
         {
                for(PermissionSetAssignment permission : [SELECT Id, PermissionSet.Name FROM PermissionSetAssignment WHERE AssigneeId =:UserInfo.getUserId() ])
            {
                 permissionSetNameList.add(permission.PermissionSet.Name); 
                                          
            }          
            userPermissionList.addAll(permissionSetNameList);   
            
            for(User userIterator : [SELECT Id, Profile.Name FROM User WHERE Id =: UserInfo.getUserId()])
            {
                userProfileSet.add(userIterator.Profile.Name);
            }                 
            if( userPermissionList.contains('Test_Permission')== true)
            {
                relatedListFlag = true;       
            } 
       }

public TestController(ApexPages.standardController controller)
{
Custombutton();

}
This is the apex code used. Please let me know which values should i assign for permissionSetNameList to initialize.

Thanks,
Anish
Anish Mahadik 1Anish Mahadik 1
Hi All,

The code is totally correct. Just i have intitalize the permissionSetNameList field and added !not(relatedListFlag) in rendered attribue of VF page. It resolve my issue.

Thanks,
Anish
This was selected as the best answer