+ Start a Discussion
ShikibuShikibu 

adding a link to apex outputField

 If I want to display, for instance, an account name, with a label, in a vf pageSection, I can do this:

 

 

<apex:outputField value="{!theAccount.Name}"/>

 

But if I want to have it link to the account, do I really have to write all this markup?

 

 

 

<apex:pageBlockSectionItem>
<apex:outputLabel value="Account Name" for="the_account"/>
<apex:outputLink value="/{!theAccount.Id}" id="the_account">
<apex:outputText value="{!theAccount.Name}"/>
</apex:outputLink>
</apex:pageBlockSectionItem>

 

 I'm wishing I could write:

 

 

<apex:outputText value="{!theAccount.Name}" linksto="{!theAccount.Id}"/>

 

 

 

 

 

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Unfortunately, if you want to have a name that you click on that takes you to the record for the account, then you do have to create that markup.

 

Of course, there is nothing to stop you wrapping this markup into a VisualForce component something like the following:

 

 

 

<apex:component>

<apex:attribute name="labelTxt" description="Label text" type="String" required="true/>

<apex:attribute name="objId" description="the sobject id" type="String" required="true/>

<apex:attribute name="objName" description="the sobject name" type="String" required="true/>

<apex:outputLabel value="{!objName}" for="the_link"/>

<apex:outputLink value="/{!objId}" id="the_link">

<apex:outputText value="{!objName}"/>

</apex:outputLink>

</component>

 

 and then you can use this in your page like so:

 

 

 

 

<apex:pageBlockSectionItem>

<c:myLinkComponent labelTxt="Account Name" objId="{!theAccount.id}" objName="{!theAccount.Name}"/>

</apex:pageBlockSectionItem>

 

 

 

 

 

Message Edited by bob_buzzard on 10-30-2009 02:31 AM

All Answers

Anand@SAASAnand@SAAS

Have you tried

 

<apex:page standardController="Opportunity"> <apex:outputField value="{!Opportunity.AccountId}"/></apex:page>

 This will automatically display the hyperlink linked to the account.

 

 

 

 

prageethprageeth

Hi 

 

Don't you like this kind of solution.

 

<a href="/{!theAccount.id}">

<apex:outputText value="{!theAccount.Name}"/>

</a>

 

 

 

Message Edited by prageeth on 10-29-2009 10:24 PM
bob_buzzardbob_buzzard

Unfortunately, if you want to have a name that you click on that takes you to the record for the account, then you do have to create that markup.

 

Of course, there is nothing to stop you wrapping this markup into a VisualForce component something like the following:

 

 

 

<apex:component>

<apex:attribute name="labelTxt" description="Label text" type="String" required="true/>

<apex:attribute name="objId" description="the sobject id" type="String" required="true/>

<apex:attribute name="objName" description="the sobject name" type="String" required="true/>

<apex:outputLabel value="{!objName}" for="the_link"/>

<apex:outputLink value="/{!objId}" id="the_link">

<apex:outputText value="{!objName}"/>

</apex:outputLink>

</component>

 

 and then you can use this in your page like so:

 

 

 

 

<apex:pageBlockSectionItem>

<c:myLinkComponent labelTxt="Account Name" objId="{!theAccount.id}" objName="{!theAccount.Name}"/>

</apex:pageBlockSectionItem>

 

 

 

 

 

Message Edited by bob_buzzard on 10-30-2009 02:31 AM
This was selected as the best answer
prageethprageeth

If you are using an output label you can use something like this.

 

 

<apex:outputlabel onclick="window.location='/{!account.id}'" value="{!account.name}"/> 

Matthew Pantaleone 9Matthew Pantaleone 9
I had a similar problem. You can make an outputField as a link by using a pageBlockSectionItem like below. It will make the page formatting correct. 
 
<apex:pageBlock title="MyBlock">
    <apex:pageBlockSection title="MySection" columns="1" collapsible="false">
        <apex:pageBlockSectionItem>
            <apex:outputLabel>My Label</apex:outputLabel>
            <apex:outputLink value="/{! account.Id }">{! account.Name }</apex:outputLink>
        </apex:pageBlockSectionItem>
    </apex:pageBlockSection>
</apex:pageBlock>

The above code works as long as the account variable is set.
 
David Roberts 4David Roberts 4
How do you do this on an <apex:column value="{!account.id}"/> within a pageBlockTable?
ShikibuShikibu
<apex:column headerValue="Related To" >
        <apex:outputLink value="/{!p.recordId}">{!p.recordName}</apex:outputLink>
    </apex:column>

 
David Roberts 4David Roberts 4
Many thanks, Shikibu.
And it's also important to put the / in the link to make it relative to the org!