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 !!!
Attribution to: Rao
Possible Suggestion/Solution #1
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!
Attribution to: jordan.baucke
Possible Suggestion/Solution #2
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. ;-)
Attribution to: Andrew Fawcett
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/4368