7

I was wondering if there was any way to get the API Server Url in apex code

ex . API.Partner_Server_URL_280

Or how to pass the value from VisualForce to apex.

I tried

<apex:param name="apiSessionId" value="{!$Api.Session_ID}" />
<apex:param name="apiServerURL" value="{!$Api.Partner_Server_URL_280}" /> 
<apex:outputText value="API Session Id: {!apiSessionId}"/><br></br>
<apex:outputText value="API Server URL: {!apiServerURL}"/><br></br>

and

public class TestController {
    public String apiSessionId {get;set;}
    public String apiServerURL {get;set;}

    public TestController() {
    }
}

but my Visual force page always says nothing.

Any help would be appreciated! Thanks

4
  • I do not see anywhere in your code where you are actually performing any action to submit the values to the controller,
    – Eric
    Commented Sep 3, 2015 at 22:38
  • I would prefer not to do it that way. So I was wondering if there was any way to get the parameter just from the apex controller
    – James
    Commented Sep 3, 2015 at 22:42
  • Your question and example code do not seem to be saying the same thing.....
    – Eric
    Commented Sep 3, 2015 at 22:45
  • what's ur requirement - to get the sandbox name ? or session id ? or any API url ? - for session id use UserInfo.getSessionId(), for sandbox name refer this discussion - salesforce.stackexchange.com/questions/16181/… Commented Sep 3, 2015 at 23:31

4 Answers 4

3

You can pass those back to the controller with help of Action Functions check the below example

<apex:page controller="TestController1">

 <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" />

 <script type="text/javascript">
   $(document).ready(function() {
      passSessionParameters('{!$Api.Session_ID}','{!$Api.Partner_Server_URL_280}');
  });
</script>

   <apex:form id="theform">
    <apex:actionFunction action="{!GetSessionIdFromVF}" name="passSessionParameters" reRender="theform">
      <apex:param name="SessionID" assignTo="{!apiSessionId}" value=""  />
      <apex:param name="ServerURL" assignTo="{!apiServerURL}" value="" />
    </apex:actionFunction>
   </apex:form>
 </apex:page>

The apex class

public class TestController1 {

public String apiSessionId {get;set;}
public String apiServerURL {get;set;}

public TestController1() {

}

  public pagereference GetSessionIdFromVF(){
  system.debug('%%%%'+apiSessionId );
  system.debug('%%%%'+apiServerURL );
  return null;
  }
}

Logs

34.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 17:12:48.030 (30634810)|EXECUTION_STARTED 17:12:48.030 (30675382)|CODE_UNIT_STARTED|[EXTERNAL]|066900000065dL2|VF: /apex/PassServerURL 17:12:48.032 (32824861)|VF_DESERIALIZE_VIEWSTATE_BEGIN|066900000065dL2 17:12:48.037 (37617577)|VF_DESERIALIZE_VIEWSTATE_END 17:12:48.038 (38149217)|CODE_UNIT_STARTED|[EXTERNAL]|TestController1 set(apiSessionId,00D90000000aRkL!ARIAQEdL8VKMDLcVxlDlaQyDS5Fm69vneozbl5eoBmPE5XjhLHu.OZ9JBRQqr_KLem7ubOS.Q7Ga0rqPu4mmgBWgwBF3x2qe) 17:12:48.038 (38171283)|SYSTEM_MODE_ENTER|true 17:12:48.038 (38200434)|CODE_UNIT_STARTED|[EXTERNAL]|TestController1 set(apiSessionId,00D90000000aRkL!ARIAQEdL8VKMDLcVxlDlaQyDS5Fm69vneozbl5eoBmPE5XjhLHu.OZ9JBRQqr_KLem7ubOS.Q7Ga0rqPu4mmgBWgwBF3x2qe) 17:12:48.038 (38458876)|METHOD_ENTRY|[1]|01p90000006pn7M|TestController1.TestController1() 17:12:48.038 (38476585)|METHOD_EXIT|[1]|TestController1 17:12:48.038 (38578429)|CODE_UNIT_FINISHED|TestController1 set(apiSessionId,00D90000000aRkL!ARIAQEdL8VKMDLcVxlDlaQyDS5Fm69vneozbl5eoBmPE5XjhLHu.OZ9JBRQqr_KLem7ubOS.Q7Ga0rqPu4mmgBWgwBF3x2qe) 17:12:48.038 (38605312)|CODE_UNIT_FINISHED|TestController1 set(apiSessionId,00D90000000aRkL!ARIAQEdL8VKMDLcVxlDlaQyDS5Fm69vneozbl5eoBmPE5XjhLHu.OZ9JBRQqr_KLem7ubOS.Q7Ga0rqPu4mmgBWgwBF3x2qe) 17:12:48.038 (38655433)|CODE_UNIT_STARTED|[EXTERNAL]|TestController1 set(apiServerURL,https://googleflowauth-dev-ed--c.ap1.visual.force.com/services/Soap/u/28.0/00D90000000aRkL) 17:12:48.038 (38665857)|SYSTEM_MODE_ENTER|true 17:12:48.038 (38674885)|CODE_UNIT_STARTED|[EXTERNAL]|TestController1 set(apiServerURL,https://googleflowauth-dev-ed--c.ap1.visual.force.com/services/Soap/u/28.0/00D90000000aRkL) 17:12:48.038 (38698069)|CODE_UNIT_FINISHED|TestController1 set(apiServerURL,https://googleflowauth-dev-ed--c.ap1.visual.force.com/services/Soap/u/28.0/00D90000000aRkL) 17:12:48.038 (38713915)|CODE_UNIT_FINISHED|TestController1 set(apiServerURL,https://googleflowauth-dev-ed--c.ap1.visual.force.com/services/Soap/u/28.0/00D90000000aRkL) 17:12:48.039 (39597675)|CODE_UNIT_STARTED|[EXTERNAL]|01p90000006pn7M|TestController1 invoke(GetSessionIdFromVF) 17:12:48.039 (39720817)|SYSTEM_METHOD_ENTRY|[11]|TestController1.__sfdc_apiSessionId() 17:12:48.039 (39777897)|SYSTEM_METHOD_EXIT|[11]|TestController1.__sfdc_apiSessionId() 17:12:48.039 (39898143)|SYSTEM_METHOD_ENTRY|[11]|System.debug(ANY) 17:12:48.039 (39913663)|USER_DEBUG|[11]|DEBUG|%%%%00D90000000aRkL!ARIAQEdL8VKMDLcVxlDlaQyDS5Fm69vneozbl5eoBmPE5XjhLHu.OZ9JBRQqr_KLem7ubOS.Q7Ga0rqPu4mmgBWgwBF3x2qe 17:12:48.039 (39923615)|SYSTEM_METHOD_EXIT|[11]|System.debug(ANY) 17:12:48.039 (39943237)|SYSTEM_METHOD_ENTRY|[12]|TestController1.__sfdc_apiServerURL() 17:12:48.039 (39987982)|SYSTEM_METHOD_EXIT|[12]|TestController1.__sfdc_apiServerURL() 17:12:48.040 (40013875)|SYSTEM_METHOD_ENTRY|[12]|System.debug(ANY) 17:12:48.040 (40025410)|USER_DEBUG|[12]|DEBUG|%%%%https://googleflowauth-dev-ed--c.ap1.visual.force.com/services/Soap/u/28.0/00D90000000aRkL 17:12:48.040 (40034170)|SYSTEM_METHOD_EXIT|[12]|System.debug(ANY) 17:12:48.040 (40066725)|CODE_UNIT_FINISHED|TestController1 invoke(GetSessionIdFromVF) 17:12:48.040 (40100194)|VF_APEX_CALL|j_id3|{!GetSessionIdFromVF}|PageReference: none 17:12:48.043 (43493979)|VF_SERIALIZE_VIEWSTATE_BEGIN|066900000065dL2 17:12:48.044 (44828524)|VF_SERIALIZE_VIEWSTATE_END 17:12:48.049 (49862772)|CUMULATIVE_LIMIT_USAGE 17:12:48.049 (49862772)|LIMIT_USAGE_FOR_NS|(default)|

2

How about a bit of the old component action?

ApiUrl.component

<apex:component>{!$Api.Partner_Server_URL_340}</apex:component>

Execute anonymous

PageReference pr = new PageReference('/apexcomponent/apiurl');
pr.getContent().toString();

gives you:

https://mydomain--c.eu2.visual.force.com/services/Soap/u/34.0/00D...

2
  • 1
    wow!!Crafty work :) Commented Sep 4, 2015 at 3:04
  • 1
    I guess from winter 16 getContent is counted as API callout . Commented Sep 4, 2015 at 4:43
1

This is no direct access to $API in apex. To retrieve something like $Api.Partner_Server_URL_280 you can try this code:

URL.getSalesforceBaseUrl().toExternalForm() + '/services/Soap/u/28.0/' + UserInfo.getOrganizationId();

You can simply use UserInfo.getSessionId() to retrieve the session Id.

1

The Winter '19 release provided the new System.Url.getOrgDomainUrl() method.

Returns the canonical URL for your org. For example, https://yourDomain.my.salesforce.com or, for orgs without My Domain enabled, https://yourInstance.salesforce.com.

This is much easier to use than System.Url.getSalesforceBaseUrl() as it will adjust for My Domain being enabled. It will also handle a Visualforce context, which would otherwise alter the domain.

There is an example of how to use it in Streamline API Calls to Your Org from Apex Code:

Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(Url.getOrgDomainUrl().toExternalForm() + '/services/data/v44.0/limits');
req.setMethod('GET');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
HttpResponse res = h.send(req);

You must log in to answer this question.

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