TransWikia.com

how to Get API Server Name in Apex code

Salesforce Asked on September 16, 2020

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 Answers

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

Answered by Daniel Ballinger on September 16, 2020

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://bigass--c.eu2.visual.force.com/services/Soap/u/34.0/00D...

Answered by bigassforce on September 16, 2020

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.

Answered by Lance Shi on September 16, 2020

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

Answered by Mohith Shrivastava on September 16, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP