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.

I have an apex:outputLink that redirects to an Account detail page, selectedId is the Account Id, eg:

<apex:outputLink
    value="{!URLFOR($Action.SObject.View, selectedId)}" //pseudocode example
    rendered="{!IF(selectedId != null, true, false)}"
/>

What if I wanted to make this more general so selectedId could be an Account Id or a Contact Id?

Can I modify this line of code to account for both cases, or will I need two separate outputLinks and render one or the other?

share|improve this question

2 Answers 2

You can do this the naughty way (least code) using just the Id:

<apex:outputLink
    value="/{!selectedId}" //pseudocode example
    rendered="{!IF(selectedId != null, true, false)}"
/>

or the nice, robust way (bleeds complexity into Apex) by using a controller:

<apex:outputLink
    value="{!View}" //pseudocode example
    rendered="{!IF(selectedId != null, true, false)}"
/>

public PageReference getView() {
    return new ApexPages.StandardController(selectedId).view();
}

or render a variant for each (more complexity in your Visualforce)

<apex:outputLink
    value="{!URLFOR($Action.Account.View, selectedId)}" //account
    rendered="{!BEGINS(selectedId, '001')}"
/>

<apex:outputLink
    value="{!URLFOR($Action.Contact.View, selectedId)}" //contact
    rendered="{!BEGINS(selectedId, '003')}"
/>
share|improve this answer
1  
Thank you very much for the various ways to implement it and EXTRA thank you for that nifty BEGINS method. This whole time I was using a massive IF statement –  Louis Pujol Jun 17 at 20:54

I'm not quite sure what $Action.SObject.View is but you can likely just use the exact same code for this.

If you can't then doing something like the following will allow you to:

<apex:outputLink target="_top" value="{!selectedId}" rendered="{!selectedId != null}">

You may need a / in front of the Id. I can't remember. I always have to test this part:

<apex:outputLink target="_top" value="/{!selectedId}" rendered="{!selectedId != null}">
share|improve this answer

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.