You need to sign in to do that
Don't have an account?
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}"/>
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>
All Answers
Have you tried
<apex:page standardController="Opportunity"> <apex:outputField value="{!Opportunity.AccountId}"/></apex:page>
This will automatically display the hyperlink linked to the account.
Hi Shikibu;
Don't you like this kind of solution.
<a href="/{!theAccount.id}">
<apex:outputText value="{!theAccount.Name}"/>
</a>
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>
If you are using an output label you can use something like this.
<apex:outputlabel onclick="window.location='/{!account.id}'" value="{!account.name}"/>
The above code works as long as the account variable is set.
And it's also important to put the / in the link to make it relative to the org!