4

Is there a way I can make an Opportunity hyperlink? In the code below, I want the user be able to click and view their own Opportunity records.

Extension

public with sharing class OppExt {
     public List<Opportunity> myOpportunities { get; private set; }

     public OppExt(){
         myOpportunities = [ SELECT Id, Name, Account.Name
                 from Opportunity 
                 WHERE OwnerId = :UserInfo.getUserId()];

    }
}

Page Snippet

<apex:repeat value="{!myOpportunities}" var="opp" >
    <apex:outputText value="{!opp.Name}"/><br/>
</apex:repeat>
  • 1
    look at this documentation on apex:outputlink : developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/… – Rao Sep 17 '15 at 16:36
  • 1
    Have you read through the visual force developers guide? Or the online reference. You will stumble upon a component <apex:commandLink> or <apex:outPutLink> that will do what you wish – Eric Sep 17 '15 at 16:37
  • @Rao how do you add specific opportunity ID as hyperlink? – Metawaa Sep 17 '15 at 16:38
6

So @Monith's answer is correct in that you should use an outputLink tag, but I disagree with the link construction methodology enough to post a separate answer.

When you are constructing links for standard actions, you should use the $Action global variable:

{!URLFOR($Action.Opportunity.View, opportunity.Id)}

If you look through the Valid Values for the $Action Global Variable, you will see many that you can use with Opportunity:

  • AddInfluence
  • Clone
  • Delete
  • List
  • New
  • Share
  • Submit for Approval (not sure how the syntax works there...should probably be Submit)
  • View
| improve this answer | |
  • What does New do? – Metawaa Sep 17 '15 at 17:21
  • That would create a new record, so you would not pass an Id in there. – Adrian Larson Sep 17 '15 at 17:24
3

You will need to use apex:outputlink for same

<apex:repeat value="{!myOpportunities}" var="opp" >
        <apex:outputlink value="/{!opp.Id}">{!opp.Name}</apex:outputlink><br/>
 </apex:repeat>
| improve this answer | |
  • 3
    I would strongly recommend using standard view actions instead of rolling your own link. {!URLFOR($Action.Opportunity.View, opp.Id)} – Adrian Larson Sep 17 '15 at 16:38
  • @AdrianLarson Besides Edit and View is there anything else available Opportunity.View for opportunity? – Metawaa Sep 17 '15 at 16:53
  • Take a look at VisualForce Global Variables, specifically the $Action variable. – Adrian Larson Sep 17 '15 at 16:57
  • Opportunity has many valid values. – Adrian Larson Sep 17 '15 at 16:58

Your Answer

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

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