+ Start a Discussion
Tom H.Tom H. 


We were excited to read about this new class and method in the Summer 11 releas.  However, it doesn't look like this method behaves as it should. 


Documentation states:

Returns the URL of the Salesforce instance.
For example, https://na1.salesforce.com.


If I execute the following line from the system log....

String baseUrl = System.URL.getSalesforceBaseUrl().toExternalForm();

System.debug('baseUrl =' + baseUrl);


This produces this output...

baseUrl = https://na8.salesforce.com/apexdebug/SystemLog.apexp


which is incorrect because it includes 'apexdebug/SystemLog.apexp'.


Even worse, if I run the same code from a controller tied to a visual force page, I get...



Is this a bug or am I misunderstanding what the purpose of this method is?



Shashikant SharmaShashikant Sharma

did you tried to use in trigger, please see its result in trigger.

Tom H.Tom H.

OK.  I'll try in a trigger but perhaps you can spare me the suspense.  Does it work as expected from a trigger?


Regardless, I need it to get the base url in three different types of thread executions:


1) From a visual force page.

2) From a batch process or from a schedulable class.

3) Lastly, from a trigger.


I know right now it doesn't work in at least one of the three.


Right now, our customers who use our managed package store the base url persistantly.  If SaleForce moves an instance, lets say from na8 to na18, our customers need to update the base url that was stored.  This is somewhat of a maintenance issue that we would like to remove from our product.

Shashikant SharmaShashikant Sharma

I tried it and it worked as expected


It reurned me https://ap1.salesforce.com in trigger.  :)


Did you ever get any resolution on this? I'm having a similar issue. We have code in a Class that is called out from a trigger after insert. Previously we had the instance URL hardcoded, and I'm trying to update it.


public String sfdcURL = URL.getSalesforceBaseUrl().toExternalForm() + '/'; 

is currently returning:

09:51:11.032 (32476000)|USER_DEBUG|[2]|DEBUG|https://cs7-api.salesforce.com/


The -api is forcing you to have to re-login to see a record when you click on a link.

Tom H.Tom H.

No resolution yet.   I would think it should return the same result regardless of context (trigger, batch, visual force).


The work around that we have had in place for a long time is to store the base url persistantly (in a datbase sobject or as an org level preference setting).  The problem is that SF occationally moves org instrances to new locations.

Sasank VarsSasank Vars
Use URL.getSalesforceBaseUrl().getHost()