Newer Version Available
Checking for Object Accessibility
If a user has insufficient privileges to view an object, any Visualforce page that uses a controller to render that object will be inaccessible. To avoid this error, you should ensure that your Visualforce components will only render if a user has access to the object associated with the controller.
You can check for the accessibility of an object like this:
This expression returns a true or false value.
1{!$ObjectType.objectname.accessible}For example, to check if you have access to the standard Lead object,
use the following code:
1{!$ObjectType.Lead.accessible}For custom objects, the code is similar:
where MyCustomObject__c is
the name of your custom object.
1{!$ObjectType.MyCustomObject__c.accessible}To ensure that a portion of your page will display only if a user
has access to an object, use the render attribute on a component. For example, to display a page block if
a user has access to the Lead object, you would do the following:
1<apex:page standardController="Lead">
2 <apex:pageBlock rendered="{!$ObjectType.Lead.accessible}">
3 <p>This text will display if you can see the Lead object.</p>
4 </apex:pageBlock>
5</apex:page>It is good practice to provide an alternative message if a user
cannot access an object. For example:
1<apex:page standardController="Lead">
2 <apex:pageBlock rendered="{!$ObjectType.Lead.accessible}">
3 <p>This text will display if you can see the Lead object.</p>
4 </apex:pageBlock>
5 <apex:pageBlock rendered="NOT({!$ObjectType.Lead.accessible})">
6 <p>Sorry, but you cannot see the data because you do not have access to the Lead object.</p>
7 </apex:pageBlock>
8</apex:page>