My use case: Rather than showing "An insufficient privileges message" after clicking on a button, I want to hide/show the button using the apex code whether to display the button based on the user's assigned Custom Permissions, so that preventing users from getting an insufficient privilege message after the event.


Is there a way to customize custom permissions for the custom objects? in the object.

What I'm trying to do is to add a new column in the Object Permission table as shown below called "Can Push" or whatever the name then let the Admin assign the access to that particular permission.

enter image description here

  • Can you go higher level? What are you trying to accomplish? – Adrian Larson Jul 21 '16 at 17:58
  • So on VFP we have couple of buttons that can be only accessible/available based on certain conditions so what I'm trying to do that if I can create the custom permission access in the above custom object permission, in that way will have all the access/permissions in one place. – Nick Kahn Jul 21 '16 at 18:18
  • Still not really clear what you are going for...am I headed in the right direction? – Adrian Larson Jul 21 '16 at 18:43
  • I have updated my question, please have a look – Nick Kahn Jul 21 '16 at 18:51
  • Are you looking for how to set Visualforce access? Or just how to infer it to control rendering? – Adrian Larson Jul 21 '16 at 19:07

If you want to check via Apex which pages the running user can access, you just need to hit up a few configuration objects:

Set<Id> assigned = new Map<Id, PermissionSet>([
    SELECT Id FROM PermissionSet WHERE Id IN (
        SELECT PermissionSetId FROM PermissionSetAssignment
        WHERE AssigneeId = :UserInfo.getUserId()
for (ApexPage page : [
    SELECT Name FROM ApexPage WHERE Name IN ('SiteLogin', 'CreateTask')
    AND Id IN (
        SELECT SetupEntityId FROM SetupEntityAccess
        WHERE SetupEntityType = 'ApexPage'
        AND ParentId IN :assigned
]) system.debug(page.Name);

You could store a Set<String> accessiblePages collection and then inspect its contents to set properties like canAccessPage1, canAccessPage2, etc.

If you are trying to make Custom Permissions accessible in a Visualforce Page, the $Permission global variable actually tells you whether or not the running user has a specific Custom Permission assigned.

  1. Create a Custom Permission for each button you wish to control.
  2. Add them to Permission Sets as needed.
  3. Manage Assignments for those Permission Sets to select which users have access.

Then your Visualforce would look something like:

<apex:commandButton ... rendered="{!$Permission.CanAccessButton1}" />
<apex:commandButton ... rendered="{!$Permission.CanAccessButton2}" />
<apex:commandButton ... rendered="{!$Permission.CanAccessButton3}" />
| improve this answer | |

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.