function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MrHammyMrHammy 

How do i build an apex:outputLink and pass it back to a Visualforce page

I am replacing a formula that has grown too large with a Visualforce page and an extension to do the work.  When passing the fully composed  apex:outputLink back as a string  is displays as just that a string. How should I be passing this back? I have stripped out all the logic so it is just building the first of about 20 different links that i need to build, once one is working the others will be easy 

VF page:
<apex:page standardController="SFDC_520_Quote__c" showHeader="false" sidebar="false" extensions="makelink">

 
 {!mylink}  
 


</apex:page>
Extention
 
public class makelink {

 private final SFDC_520_Quote__c q;

    public makelink(ApexPages.StandardController stdController) {
        this.q = (SFDC_520_Quote__c)stdController.getRecord();
    }

public string getmylink(){
string l;

l = '<apex:outputLink target="_blank" value="https://inside.legrandna.com/mainpage/Sales/tws_quote_program/find_quote.cfm?quote=' + q.Quote_ID__c + '&oppid=' + q.Opportunity__c + '&quoteid=' + q.id + '">WS Quote</apex:outputLink>';

return l ;
}
 

}

 
James LoghryJames Loghry
Yeah that's not going to work.  Instead, a better approach here would be to use a <apex:commandLink> in conjunction with PageReference methods.
 
public class makelink {

 private final SFDC_520_Quote__c q;

    public makelink(ApexPages.StandardController stdController) {
        this.q = (SFDC_520_Quote__c)stdController.getRecord();
    }

    public PageReference goToLinkOne(){
        PageReference pageRef = new PageReference("https://inside.legrandna.com/mainpage/Sales/tws_quote_program/find_quote.cfm");
         Map<String,String> parms = pageRef.getParameters();
         parms.put("quote",q.Quote_Id__c);
         parms.put("oppid",q.Opportunity__c);
         parms.put("quoteid",q.Id);
         return pageRef;
    } 
}
 
<apex:page standardController="SFDC_520_Quote__c" showHeader="false" sidebar="false" extensions="makelink">
<apex:form>
    <apex:commandLink action="{!goToLinkOne}" target="_blank" />
</apex:form>
</apex:page>

If the links get out of hand, then you may want to consider a different approach, perhaps using custom settings.
Rory HibbertRory Hibbert
James is right, passing back html isn't going to work this way. You'll need some way to inject the HTML into the DOM.

In the past I have used visualforce remoting to call an apex method that generates HTML, then in the callback function use jQuery to inject the HTML into the DOM.

I'm suspecting though that this may be overkill for your implementation
James LoghryJames Loghry
FYI, there should be single quotes, not double quotes in my example above...  Can't edit the post, though :-/
MrHammyMrHammy
The apex:commandLink  didn't work and like I said i have about 20 different links to deal with. I will look at a few other options. I wish I could do IF in the visual force page, that would be the fastest
 
James LoghryJames Loghry
How exactly did it not work?  Was it not compiling?  Did the link direct to the wrong place?  Did it not open in a new tab as you'd expect?  If you post what you have, perhaps we could help further.  To answer your last point, Visualforce does have conditional IF/ELSE syntax: https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_functions.htm
MrHammyMrHammy
It didn't display a link at all, i will look at the link you sent and see if I can find a solution
 
James LoghryJames Loghry
I missed it in my original example, but you'll also need to specify the "value" attribute of the command link, which gives the link some text:
 
<apex:commandLink action="{!goToLinkOne}" target="_blank" value="Go To Link One" />



 
MrHammyMrHammy
Ahhh! that was the issue, now the link shows.  I will look into wrapping everything in the if statements i need