1
votes

I'm new to Salesforce and I was looking into making a page layout for Leads/Contacts/Accounts to have special owner visibility.

I have the detail page looking as needed using Standard Salesforce page layouts, but am wondering how can I show or hide a section of the layout based on whether the viewing User is the Owner of the record.


Desired Scenario:

Record R is owned by User A. Detail Page for Record R is a Standard Salesforce Page Layout with 2 sections: one section containing Contact info (that ONLY User A, the owner, can see), and one section containing additional info (that ALL users can see).


I have reviewed the documentation and from what I can see the only way is to either build a custom controller or extension. Is this the only way and if so, does anyone have a good walkthrough in changing only a section of a page layout rather than creating an entirely new page?

1

1 Answers

3
votes

Your best option (Approach 1) is to separate your fields into FieldSets (see Working with Field Sets in Visualforce ) and then override the detail page using a Visualforce Page that uses those FieldSets to determine which fields to display AND only shows certain FieldSets if the User viewing the page is the Owner of the record. This approach requires no custom controller / extension, allows you to hide various sections of your page to non-Owners, and allows you (or another admin) to modify the fields in each section going forward using the drag-and-drop FieldSet editor, which is very similar to the Drag-and-Drop Page Layout editor.

Another method (Approach 2) that also requires no custom controller / extension would be to create a Visualforce Page that contains the fields you only want to show to the owner, and then only render these fields if the running user is the record owner. YOu can then add this Visualforce Page to your Page Layout. The reason I don't recommend this approach is that it is a pain to get the styling of the fields in this Page to match up with the rest of the standard page layout.

Just FYI, there is no straightforward way (read: without JavaScript hacks) to show/hide sections of a standard Page Layout without using Visualforce.

APPROACH 1:

<apex:page standardController="Contact">
     <!-- Fields everyone should see -->
     <!-- (stored in the 'FieldsEveryoneSees' fieldset) -->
     <apex:repeat value="{!$ObjectType.Contact.FieldSets.FieldsEveryoneSees}" var="f"> 
         <apex:outputField value="{!Contact[f]}" /><br/>
     </apex:repeat>
     <!-- Fields only the Owner should see -->
     <!-- (stored in the 'OwnerOnlyFields' fieldset) -->
     <apex:repeat value="{!$ObjectType.Contact.FieldSets.OwnerOnlyFields}" var="f"
              rendered="{!$User.Id == Contact.OwnerId}"> 
         <apex:outputField value="{!Contact[f]}" /><br/>
     </apex:repeat>
</apex:page>

APPROACH 2:

<apex:page standardController="Contact" showHeader="false" sidebar="false">
     <apex:outputPanel rendered="{!Contact.OwnerId == $User.Id}">
          <!-- Fields only the Owner should see -->
          <apex:outputField value="{!Contact.LastModifiedDate}"/>
          <!-- etc... -->
     </apex:outputPanel>
</apex:page>