2

I'm using a JavaScript framework to build an app. I need to code a custom apex rest endpoint. After a ton of trial and error, I found out that my custom apex rest endpoint url look like this

https://my-domain.my.salesforce.com/services/apexrest/NAMESPACE/CustomRestEndPoint/

My problem is I need to store

my-domain

in a variable on my Visualforce page to use in my app. I don't want to hardcode it, so I can install the app in other orgs.

Currently "{!$Site.Domain}" returns an empty string. Why is that?

<apex:page standardStylesheets="false" sidebar="false">
  <apex:slds />
  <head>
    <title>My App</title>
  </head>   

  <script>
    const url        = "https://" + window.location.hostname;    
    const restApiUrl = "https://" + window.location.hostname + "/services/data/v39.0/";
    const apiToken = {
      headers: { "Authorization" : "Bearer " + "{!$Api.Session_Id}",
                 "Content-Type" : "application/json"
               }
    }    

    console.log(url);
    console.log(restApiUrl);
    console.log(apiToken);   
    console.log("{!$Site.Domain}");

  </script>

  <div class="slds" id="root" />

</apex:page>
  • Is there any possibility to use a controller as using controller you can have it by String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm(); – cloudy-ritz Apr 17 '17 at 8:07
  • I suppose. I was trying to avoid that. – Tyler Zika Apr 17 '17 at 8:09
  • @cloudy-ritesh weird I tried that. System.URL.getSalesforceBaseUrl().toExternalForm() return the proper url in apex, when I system.debug() it, but it returns a different url on the vf page... – Tyler Zika Apr 17 '17 at 8:17
  • Please refer this similar thread salesforce.stackexchange.com/questions/11744/… – cloudy-ritz Apr 18 '17 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.