[SalesForce] serverurl and visualforce

How do I get the server URL from a visual force page?

I am using conga and when I invoke from a button the code it passes the server URL as:

ServerUrl=https%3A%2F%2F***cs9.salesforce.com***%2Fservices....

When I use {!$Action.Partner_Server_URL_210} I get:

ServerUrl=https%3A%2F%2F***c.cs9.visual.force.com***%2Fservices ....

How do I get the actual server URL ?

Can I go ahead and hard code the URL ? Is there any time the Prod server URL would change?

Please let me know!!!

Update : Though I was able to get the params the serverURL and the sessionID in multiple ways,finally I ended up taking a different route I went over conga documentation and ended up taking approach 3 from the conga support page: (Thanks to Andrew)
http://knowledge.appextremes.com/appextremes/ext/kb73-how-to-create-a-visualforce-button-to-call-conga-composer

Different approaches I tried :
Approach 1: (FAILED)
1) Session Id and Server URL code was

<a href="https://www.appextremes.com/apps/Conga/Composer.aspx? 
***SessionId={!$Api.Session_ID} 
&ServerUrl={!$Api.Partner_Server_URL_210}*** 
&Id={!Account.Id} 
&TemplateId=something
&FP0=1 
&ReportId=something
&DS7=3" />

2) (FAILED)
Controller:

string href_string{get;set;}
***string sessionid = userinfo.getsessionID();
string serverurl = URL.getSalesforceBaseUrl().toExternalForm();***
href_string = 'https://www.appextremes.com/apps/Conga/Composer.aspx?'+ '\n'+
'SessionId='+sessionid + '\n'+ 
'&ServerUrl='+serverurl+ '\n'+ 
'&Id='+{!someaccountId}+ '\n'+ 
'&TemplateId='+something_id+ '\n'+
'&FP0=1'+ '\n'+ 
'&ReportId='+something_id;

Approach 3 : (PASSED)

<apex:commandButton value="Create PDF Quote"
onclick="openConga()"/>
<Script Language="JavaScript">
function openConga() { window.open('{!URLFOR($Action.Account.Conga_Composer_temp,Account_Id)}', '','scrollbars=yes,menubar=no,height=800,width=700,resizable=yes, toolbar=no,location=no,status=yes'); }
</Script>

controller :

public string Account_Id{get;set;}

Account_id = 'query accounts get accountid based on picklist name/Id combination';

I was able to get it done but the grudge of not having found what went wrong with the apexy approach will always remain 🙁

URL that the button and VF generated were the same except for the session ID ( I doubt that could be the culprit,because the session Id if invalid the page would fail to load ):
HERE IS THE COMPARE IT SCRESHOT :

Thanks to all who helped me solve this issue !!!

enter image description here

Best Answer

Here is a link to what Conga Support recommends. Recomended approach 2 is what your asking about regarding the Partner_Server_URL and in my view better than approach 1 they describe.

So what you tried in your question should actually work, {!$Action.Partner_Server_URL_210} returns a Visualforce domain in a VF page contect, which does look odd, however you can actually use these domains for API calls. I've come across this when using URL.getSalesforceBaseUrl to call the Metadata API.

From Apex another approach is to use the (relatively new) method on the URL.

URL.getSalesforceBaseUrl().toExternalForm();

URL.getSalesforceBaseUrl().getHost();

etc..

If the variant of the Salesforce base URL returned by the above in Visualforce contexts does not work for you. You can try this formula. However be warned it assumes that Salesforce will continue to utilise the c.instance.visual.force.com format. But for now it works!

{!SUBSTITUTE(SUBSTITUTE(LEFT($Api.Partner_Server_URL_210, FIND( '/services', $Api.Partner_Server_URL_260)), 'visual.force', 'salesforce'), 'c.', '')}

I would still be inclined to ask Conga Support why there advice is not working for you though. ;-)

Related Topic