6

I'm looping through a few fields in a fieldset and displaying them with outputField. I would like the Object__c.Name field to hyperlink to the record. Is it possible to do this?

The only solutions I know of are to manually put the hyperlink on the first loop of the apex:repeat using apex:variable or to check if {!field == 'name}. However, I would prefer a simpler solution.

Here is a sample of my display code.

<apex:repeat value="{!initTable}" var="results">
    <tr>
    <apex:repeat value="{!initFieldsOrder}" var="field">
        <td><apex:outputField value="{!results[field]}" /></td>           
    </apex:repeat>
    </tr>
</apex:repeat>
  • A hack to do it would be to pull a record that looks up to the main object record into the controller/extension then output the value of the lookup as the name field instead of the usual name field of the record. i.e. for Opportunity, grab a related OLI and output the OpportunityId field as the opportunity name. But this depends on the structure of your page and code on if it makes sense or not – Eric Aug 4 '16 at 23:56
  • Is this still an open question? – Adrian Larson Apr 17 at 20:27
  • @AdrianLarson I don't think any of the answers are simpler or better, so yes. – Jacob Raccuia Apr 18 at 14:12
1

This should work:

<apex:outputField value="{!results[field]}" rendered="{!field!='Name'}"/>
<apex:outputLink value="/{!results['id']}" rendered="{!field=='Name'}">{!results[field]}</apex:outputLink>
0

Taking a concrete example to my comment:

<apex:page controller="myexample">

   <apex:outPutfield value="{!oli.OpportunityId}"/>

</apex:page>


public class myExample{

    Opportunity o;
    OpportunityLineItem oli;

    public myExample(){
      o = [Select ID, (Select Id, OpporutnityId From OpportunityLineItems) From Opportunity Where ID = :apexPages.currentPage().getParameters().get('id')];
      oli = o.OpportunityLineItems[0].OpportunityId;
    }


}

Adding in all requires checks for present records, null values, etc. and then adjust to be used in your table as needed.

Granted this uses additional queries etc and depends on a related record to be present but it may make sense if you do no like the <apex:variable> option. As an added benefit you get the hover lookup details

  • This is a cool workaround - but what if there were no records that looked up the record I needed to show? I'm using the example in my code to display search results, so any record could show and I can't guarantee that this exists. – Jacob Raccuia Aug 5 '16 at 3:09
  • Therein lies the rub.......Only works if you have a related records....The variable might be your best option in that case – Eric Aug 5 '16 at 3:10
  • I was trying to see if it was possible to pass in the current record ID and get it to hyperlink with the name, but that didn't seem to work. – Jacob Raccuia Aug 5 '16 at 3:30
-1

Try this

<apex:repeat value="{!initTable}" var="results">
    <tr>
    <apex:repeat value="{!initFieldsOrder}" var="field">
        <td>
<apex:outputLink value="/!{results[field].id}">{!results[field]}</apex:outputLink>

    </apex:repeat>
    </tr>
</apex:repeat>
  • 1
    OP asked about putting link only on Name field, not every field – cropredy Aug 4 '16 at 19:19
  • 1
    Also your page will not compile as the merge fields are not correct – Eric Aug 5 '16 at 3:45

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.