+ Start a Discussion
Kinetic Growth 4Kinetic Growth 4 

Instance Information for Custom URLs

Hi,

I'm trying to figure out a way to obtain the instance information from Custom Production / Sandbox URLs.

For example: If a Standard URL is "https://eu3.salesforce.com/" the instance information I'm looking for is "eu3" which can be obtained by calling "System.URL.getSalesforceBaseUrl()" and then parsing out the string.

But, when the URL is something like, "https://abc.my.salesforce.com/", how do I get the instance? Any help appreciated.

Thanks,
Shashank
PratikPratik (Salesforce Developers) 
Hi Shashank,

This should help:
http://www.michaelforce.org/recipeView?id=a0Ga000000Ekp65EAB
http://salesforce.stackexchange.com/questions/27751/get-the-salesforce-internal-non-site-url-from-a-site

Thanks,
Pratik
Mert YALTIMert YALTI

Hi Shashank;

you can try the code below;

String urlInstance= String.valueOf(URL.getSalesforceBaseUrl().getHost()).substringBefore('.');
system.debug('urlInstance==>'+urlInstance);

Debug Log;

13:28:42:022 USER_DEBUG [2]|DEBUG|urlInstance==>eu5

Mert YALTIMert YALTI

Or if you are working at visualforce page you can implement the given Javascript code below;

<script>
//this script will get url instance and pass it through controller via InputHidden
$( document ).ready(function(){
       var a = window.location.host;
       var urlPrefix = a.substr(0, a.indexOf('.'));
       $('[Id$=myUrlInstance]').val('urlPrefix');
});
</script>

// Controller part
public String urlInstance {get;set;}
//VisualForce part
<apex:inputHidden id="myUrlInstance" value="{!urlInstance}" />
Mert YALTIMert YALTI

CORRECTION:

$( document ).ready(function(){ var a = window.location.host; var urlPrefix = a.substr(0, a.indexOf('.')); $('[Id$=myUrlInstance]').val(urlPrefix); });