1

I have a Visualforce form which that contain a couple of fields restricited to only certain profiles. With Salesforce the fields are automatically hidden/shown depending on the permissions, what I would like to do is extend functionality to the surounding code I have written.

Here is an example of the code I want to show only if 'Profile B' user logged in.

        <div class="form-group col-md-6 col-xs-12">
            <label>Outlet ID</label>
            <apex:inputField value="{!Service_Order__c.Outlet_ID__c}" styleClass="form-control" />
        </div>

Currently it will still output div element and the label.

0

Have you tried the rendered tag something like:

rendered="{!$Profile.Name=='Name here'}"

Also look at the answers from this question:Conditionally Rendering Visualforce element based on user profile

Edit: If I understand correctly it's not the field but a section - you can use an apex:outputPanel> to wrap the section in and use the rendered tag there. Like below:

<apex:outputPanel id="panel1">  
    <div class="form-group col-md-6 col-xs-12">
        <label>Outlet ID</label>
        <apex:inputField value="{!Service_Order__c.Outlet_ID__c}" styleClass="form-control" />
    </div>              
</apex:outputPanel>

VF Doc: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_outputPanel.htm

| improve this answer | |
  • This will need to be attached to a apex tag? What would be the most suitable to wrap my code in? – Deployment Failure Oct 17 '14 at 13:00
  • Assuming it the input field something like: <apex:inputField value="{!Service_Order__c.Outlet_ID__c}" styleClass="form-control" rendered="{!$Profile.Name=='Name here'}" /> – Girbot Oct 17 '14 at 13:04
  • the input field is automatically shown/hidden depending on user permissions... its the surounding code I want to put under a render statement – Deployment Failure Oct 17 '14 at 13:05
  • I've updated my answer to include <apex:outputPanel> – Girbot Oct 17 '14 at 13:11
3

Even more robust than rendering based on hardcoded profiles, you should check that the running user has the necessary permissions to that specific field. You can do that using the $ObjectType global variable. This way if the profile permissions change, you don't necessarily need to update your page.

... rendered="{! OR( $ObjectType.Service_Order__c.Fields.Outlet_ID__c.createable, $ObjectType.Service_Order__c.Fields.Outlet_ID__c.updateable ) }" ...

If your page is designed solely for creating new records or solely for updating existing records, then you would only check for that specific condition instead of both.

$ObjectType: https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_objecttype.htm

Field Schema Details: https://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global_objecttype_schema_fields_reference.htm

| improve this answer | |

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