function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
topartnrtopartnr 

Finding Dynamically the URL of the Salesforce instance

Hi,

 

I'm writing Apex trigger that needs to compose a URL to a dynamically created custom object.

Since I don't want to hard code the URL of the Salesforce instance and also don't want to change the code when switching between Sandbox and Prod org's, I'm looking for a way to find out from within Apex code which Salesforce instance it's running in.

So, for example, this code should return 'https://cs2.salesforce.com' when running in a Sandbox and 'https://na2.salesforce.com' when running in Prod.

I've noticed that Sandbox Id's start with 00DR whereas Prod Org IDs start with 00D<some digit>. But am not sure if this is something that can be used to identify the instance or if there is any better way.

 

Thanks.

ShikibuShikibu
this should do it:

 

 

new PageReference('/' + myCustomObject.id).getUrl()

 

 

 

 

Message Edited by Shikibu on 05-02-2009 03:06 PM
venkatSanthanamvenkatSanthanam

The solution to the problem does not work for me.

 String retURL = new PageReference('/' + thisNote.id).getUrl();

 

constructs a url as follows:

 

/00PS0000000RIQ6MAO

 Any thoughts ???

ShikibuShikibu

Hi Venkat,

 

The url you showed is a working relative url; better than an absolute one because it will bring you to that object in any salesforce instance.

 

 

How are you planning to use the url? If you return a PageReference of this type from Apex to VisualForce, it will cause a redirect, and it will operate correctly regardless of the base of the url.

 

 

 

Have you looked at the docs on this page?

 

Below is an example, showing that a snippet of code from those docs produces an url in the same style as you showed above. 

 

 

Account a = [SELECT ID FROM Account Limit 1][0];PageReference p = new ApexPages.StandardController(a).view();system.debug(p);20090701082833.787:AnonymousBlock: line 4, column 1: System.PageReference[/00120000006vIzQ]

 

 

 

 

 

venkatSanthanamvenkatSanthanam

Shikibu,

I want to use this url in an email so that the user can click on it to go directly to the page.

 

A relative URL wouldnt suffice in this case as the complete URL is necessary. I havent gone through the document you suggested, but will read that too.

 

But, if you know of a code snippet that would give me the complete URL, it would be great.

 

 

Thanks

Venkat

ShikibuShikibu

Hi Venkat,

 

I haven't needed an absolute URL before. There are others struggling with this. There are some (ugly) workarounds posted here.

 

 

ShikibuShikibu

Venkat,

 

Have a look at this thread too. 

jlevenejlevene

In case anyone else finds this helpful: I asked Salesforce developers (via opening a case) as to whether you can reliably identify a org as a sandbox or production from the org ID.

 

The answer I was given is slightly different to the post above:

 

- All production org IDs start with '00D*' where * is a number

- All sandbox org IDs start with '00D*' where * is a letter