Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

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>
share|improve this answer
    
That broke the rendering: I have a label with no value, and the link is on the next line. –  Snowman Aug 7 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 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. –  Snowman Aug 7 at 20:51

You can do this like this:

<apex:outputLabel for="link" value="Label" />
<apex:outputLink id="link" value="/{some.Id}">{!some.value}</apex:outputLink>
share|improve this answer
    
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. –  Snowman Aug 7 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 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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