+ Start a Discussion
vfDeveloper.ax341vfDeveloper.ax341 

Accessing partner server url from apex code


I need to acces Partner server url global variable from apex code in order to pass it into web service.

In visualforce i can write the followin statement in page layout  {!$Api.Partner_Server_URL_120} which renderes something like that https://na5.salesforce.com/services/Soap/u/12.0/461200D70000000IfeF

How can i access {!$Api.Partner_Server_URL_120}  variable from apex code?

Thanks in advance


TomSnyderTomSnyder
How did you overcome this?
ColoradoMikeColoradoMike
Did you ever solve this?  It would sure be helpful...
VladikVladik

If you using visualforce controller, you can try following code

 

String url = ApexPages.currentPage().getHeaders().get('Host');

TheWolfTheWolf

Two options

 

on a std controller environment

 

 

 

String partnerURL = 
https://'+ApexPages.currentPage().getHeaders().get('Host')+'/services/Soap/u/26.0/'+UserInfo.getOrganizationId();

 

 

another way is to create a custom formula field somewhere usefull, and the return as text 

 

$Api.Partner_Server_URL_260

 

cheers.

Koen (BvD)Koen (BvD)

I searched for this subject hoping that with all the updates of apex by now there would be a cleaner way to access this. I have been using over the years a component with a static property containing the url, I can then use the static property eslewhere. I just include the component in the pages where I use the variable to assign it

 

<apex:component controller="APIParameterController" >
    <apex:attribute name="ApiServiceUrl" type="String" required="true" assignTo="{!ApiServiceUrl}" description="apex missing global workaround" />
</apex:component>

 the controller just has a static property

 

public with sharing class APIParameterController 
{           
    public static String ApiServiceUrl
    {
        get;
        set;            
    } 
}

 

and in pages where I know that some other aprt of my code requires the url, I include the component with the correct assignment

 

<c:APIParameterInitialization ApiServiceUrl="{!$Api.Partner_Server_URL_260}" />

 

I hope this can be of help to someone searching for this, as apparantly Salesforce has no intention of providing access to these urls in apex code (I see several posts with the same question and this thread for example dates from 5 years ago

TheWolfTheWolf
since that also requires a VF page, and not always you will have a VF page
in context, like batch process, triggers, @Future methods, etc ...

The way I do it, and maybe someone have something better, is like this.

I create a formula field in the user: API_URL__c and the formula will
return a text: $Api.Partner_Server_URL_260

then in any apex context you can always do:

String url = [select API_URL__c from User where id=:userinfo.getUserId()].
API_URL__c;

Cheers.

--
Federico Larsen

Mobile: +34644590488
Phone: +34917400957
U.K. : +442070973946

Twitter/Skype: larsenfed
LinkedIn: http://www.linkedin.com/in/federicolarsen
Blog: http://larsenfed.wordpress.com

You should use Dropbox , it's free!
Nirmal ChristopherNirmal Christopher
I had a situvation to use apes code to fetch the partner service URL. I used formula field to obtain it and used it in Apex code.