[SalesForce] OutputText with Link

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.

Best Answer

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>