3

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

2 Answers 2

4

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. ;-)

8
  • No luck still returns "c.cs9.visual.force.com" when I try salesforcebaseurl.toexternalform() I am using a VF page within a VF tab does that matter!!! Is there no way to get the Instance.salesforce.com value :(
    – Rao
    Commented Nov 13, 2012 at 22:55
  • Does Conga complain if you pass c.cs9.visual.force.com? The support article I linked to in my answer suggests this should work? Commented Nov 13, 2012 at 22:56
  • Nope it says "Error: Your Salesforce session has expired or the Salesforce API Servers are offline. INVALID SESSION ID or SERVER URL"
    – Rao
    Commented Nov 13, 2012 at 22:57
  • That is strange, their own support page suggests otherwise, I've even looked at the SF page they link to from approach 1 and it claims that URL's from Partner_Server_URL are usable for making API call backs such as those Conga is doing. You may have to call Conga support on this one. Commented Nov 13, 2012 at 23:05
  • Ok, I've updated my answer for you with a workaround, but as per my answer, i do recommend in the meantime you should ask them why their support article does not work. Enjoy! :) Commented Nov 13, 2012 at 23:14
3

Yes, it's possible that your organizations instance could be changed between 2 pods. I've been using one that's moved before.

For example:

na1->na13

SFDC Organizations are portable between pods. They should give you some warning, but it's good just to be safe.

Generally it would be better idea to use Javascript on the client-side, or on the server side, the APEX PageReference class to determine the current URL your code is executing on. Documentation of page-reference class.

Update

PageReference pageRef = ApexPages.currentPage();
Map<String,String> headers = new Map<String,String>();
headers = pageRef.getHeaders(); // returns a map
string host = headers.get('Host');

I believe this is how you get the hostname into a string!

5
  • Can you please tell me how I can use the pagereference method ? I tried doing apexpages.currentpage.getheaders.host(), I couldnt get the server URL is there a sample code you can post please
    – Rao
    Commented Nov 13, 2012 at 22:25
  • Added linked to documentation in my response, and an explanation of using the getHeaders() method to extract a string! Commented Nov 13, 2012 at 22:27
  • the problem is I am getting the host name as :c.cs9.visual.force.com/apex/Report_Builder since I am initiating the button from a VF page. Is it safe to strip just the server instance part and make a string that says serverinstance+'.salesforce.com' ?
    – Rao
    Commented Nov 13, 2012 at 22:32
  • When you say 'changed' do you mean at the time of a sandbox refresh... or just in normal operation it'll suddenly jump cs1->cs5? Commented Nov 14, 2012 at 6:04
  • No no, I mean it was announced: the organization administrator was notified. Commented Nov 14, 2012 at 6:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .