4

If you look on a standard page for viewing an object, you will see fields where there is a field label and field value, with the value being a hyperlink to e.g. a user.

I am trying to replicate the same functionality on a custom VisualForce page but have not been able to get it to work.

What I have tried so far

First, I tried embedding the link in the output text:

<apex:outputText label="Field Label"
    value="&lt;a href=&quot;/&quot;&gt;{!some.value}&lt;/a&gt;"/>

This did not work: it escaped my escapes and put the whole mess in the page.

Next, I tried parameterizing it:

<apex:outputText label="Field Label" value="{0}">
  <apex:param value="&lt;a href=&quot;/&quot;&gt;{!some.value}&lt;/a&gt;"/>
</apex:outputText>

Same result.

Next, I tried a link element:

<apex:outputLink value="/">
  <apex:outputText label="Field Label" value="{!some.value}"/>
</apex:outputLink>

Partial success! I get a link now, but the field label is gone and the value is where the label should be.

Finally, I tried turning off escaping and embedding a link:

<apex:outputText label="Field Label" escape="false"
    value="&lt;a href=&quot;/&quot;&gt;{!some.value}&lt;/a&gt;"/>

I get the raw HTML displaying on the page. When I inspect the element in my browser, it displays correctly. I imagine that is because it is interpreting the escapes that I added and which are necessary to avoid breaking VisualForce parsing.

There are literally tons of examples of what I am trying to do. Look at anything such as Contact. The contact owner, email, etc. are all hyperlinks but they also have a field label to the left. That is all I am trying to do.

In fact if there is a way to pass in an object ID and have Salesforce construct a link for me, that would be ideal. I am displaying IDs and names of objects and want links to view those specific objects based on the ID I have stored.

4

You'll need to use two tags. Both an outputLink as well as an outputLabel tag.

<apex:pageBlockSectionItem>
    <apex:outputLabel value="Your Label Text" />
    <apex:outputLink value="/">Your Hyperlink Text</apex:outputLink>
</apex:pageBlockSectionItem>
  • That broke the rendering: I have a label with no value, and the link is on the next line. – user6861 Aug 7 '14 at 20:27
  • @Snowman, this pattern works when the elements are within a pageBlockSectionItem element which supports two child elements. If you can provide more context about the surrounding markup, we can help you better. – Mark Pond Aug 7 '14 at 20:46
  • that worked after I made some other changes. It looks like mixing different elements broke the rendering. When I changed everything to use apex:pageBlockSectionItem it worked fine. – user6861 Aug 7 '14 at 20:51
0

You can do this like this:

<apex:outputLabel for="link" value="Label" />
<apex:outputLink id="link" value="/{some.Id}">{!some.value}</apex:outputLink>
  • If you read my question, it is imperative that this works like an apex:outputText: in addition to the link and text, I need a label to the left. – user6861 Aug 7 '14 at 20:25
  • Then try this: <apex:outputLabel for="link" value="Label" /> <apex:outputLink id="link" value="/{some.Id}">{!some.value}</apex:outputLink> – Paul Boiko Aug 7 '14 at 20:40
0

I presume you are trying to replicate standard Salesforce layout, link Created date or Last Modified Date. The code that worked for me is below:

<apex:pageBlock >
            <apex:pageBlockSection columns="2" id="pBlockSysinfo" title="System Information" rendered="{!!bIsEdit}">
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Created By"  /> 
                    <apex:outputText value="{0, date, yyyy-MM-dd hh:mm a z}">
                        <a href='/{!opp.CreatedById}'> {!opp.CreatedBy.name}</a>,
                        <apex:param value="{!opp.CreatedDate}" /> 
                    </apex:outputText> 
                </apex:pageBlockSectionItem>                
            </apex:pageBlockSection>
        </apex:pageBlock>

WARNING: Please note that date itself will be printed in GMT timezone, therefore you should use code below that takes into account running users timezone settings:

Apex:

string defaultDateFormat = 'dd/MM/yy HH:mm a';
            lastModifiedDate = ( opp.LastModifiedDate != null ) ? opp.LastModifiedDate.format( defaultDateFormat ) : '' ;
            createdDate = ( opp.createdDate != null ) ? opp.createdDate.format( defaultDateFormat ) : '' ;

VF:

<apex:pageBlockSectionItem>
                    <apex:outputLabel value="Created By"  /> 
                    <a href='/{!opp.CreatedById}'> {!opp.CreatedBy.name}</a>, {!createdDate}
                </apex:pageBlockSectionItem>   
                <apex:pageBlockSectionItem>
                    <apex:outputLabel value="Last Modified By"  /> 
                        <a href='/{!opp.LastModifiedById}'> {!opp.LastModifiedBy.name}</a>, {!lastModifiedDate}
                </apex:pageBlockSectionItem>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy