+ Start a Discussion
DeptonDepton 

Get the instance URL for a Visualforce page

Hi,

 

I have a VisualForce page with a link:

 

https://na1.salesforce.com/apex/Visualforce_Page

 

Soon we are migrating from na1 to another instance and would like to know how to automatically retrieve the Salesforce Instance to do the same changes in all the visualforce pages.

 

I have tried to use relative links as in any custom link:

 

/apex/Visualforce_Page

 

but is not working

 

I have found this link below

 

http://stackoverflow.com/questions/9369653/retrieve-salesforce-instance-url-instead-of-visualforce-instance

 

Does anyone knows how should I replace

 

https://na1.salesforce.com/apex/Visualforce_Page

 

So next month with the instance change all will keep working?

 

Thank you

 

Best Answer chosen by Admin (Salesforce Developers) 
Edwin VijayEdwin Vijay
<apex:page>
  <h1>Linked</h1>
  <apex:outputLink value="{!$Page.otherPage}">
    This is a link to another page.
  </apex:outputLink>
</apex:page>

 

All Answers

SeAlVaSeAlVa

The following code displays https://c.na7.visual.force.com/

 

{!LEFT($CurrentPage.URL,FIND('/',$CurrentPage.URL,9))}

 Regards

DeptonDepton

I will check in an hour and get back to you! Thank you!!

 

:)

Edwin VijayEdwin Vijay

/apex/pagename should be working.. Can you let us know the problem you face.. You could also try the $Page notation..

 

Lastly, its not a best practice to hard code URL's unless you have no other way

DeptonDepton

It is a VF Email Template:

 

<messaging:emailTemplate subject="Thank you for blablabla, {!relatedTo.Title}" recipientType="User" relatedToType="Idea">

<messaging:HtmlEmailBody >
<style type="text/css">
    
    body {font-family: arial,helvetica;}
    
    h2, h3, h4, h5 {
        color: #ed1d23;
    }
    
 
    h2 {
        text-transform: uppercase;
        font-size: 15px;
        margin-bottom: 10px;
    }

    h3{
        font-size: 50px;
    }
    
    h4, h5 {
        font-size: 10px;
    }
    
    p {
        font-size: 15px;
    }

    p.big{
        line-height: 22px
    }
    
    pb {
        font-size: 15px;
        font-weight: bold
    }
    a:link, a:visited {color:#ed1d28; text-decoration:none}
    a:hover, a:active {color:black; text-decoration:underline}

</style>
    <table border="0" cellspacing="0" cellpadding="0" width="600px" align="center" style="border: 1px solid #aaaaaa;">
            
            <tr>
                <td bgcolor="#ed1d23" style="padding:10px"><img src="https://test.com/assets/labs/images/header_new.jpg" /></td>
            </tr>
            
            <tr>
                <td style="padding: 10px">
                <br/>
                <p><b>Dear {!relatedTo.CreatedByName__c},</b></p>
                
                <h2>Thank you for blablabla!</h2>
                <!--<h3>{!relatedTo.Title} as part of the {!relatedTo.Category} challenge.</h3>-->
                        
                <p class="big">You can blabla clicking <a href="/apex/yyyyyyyyy">here.</a></p>
                
                
                <p class="big">Again, blabla!</p>
                <br/>
                <pb><b>Kind Regards,</b></pb><br/><br/>
                <br/>
                <a href="https://test.com/labs">https://test.com/labs</a>
                </td>
            </tr>
            
        </table>
                
</messaging:HtmlEmailBody>
</messaging:emailTemplate>

 Here is the code:

 

where i have put :

  <p class="big">You can blabla clicking <a href="/apex/yyyyyyyyy">here.</a></p>

 yyyyyyyy is the name of the apex class...but the "here" link should open a visualforce page

 

and the browser opens with:

apex/yyyyyyyyy

instead of 

https://cs1.salesforce.com/apex/yyyyyyyyy

 

Thank you for your help!

 

 

Edwin VijayEdwin Vijay

aah, since its an Email template the /apex/pagename notation wouldn't work.. Have you tried with the $Page notation? I think that might work

DeptonDepton

never used the $Page notation before could you give me some tips or an example in my template please!

 

Thank you!

 

Edwin VijayEdwin Vijay
<apex:page>
  <h1>Linked</h1>
  <apex:outputLink value="{!$Page.otherPage}">
    This is a link to another page.
  </apex:outputLink>
</apex:page>

 

This was selected as the best answer