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
GoForceGoGoForceGo 

How to get server URL in Apex?


I want to send a e-mail with a link to an object...How would one get server URL in Apex?



Best Answer chosen by Admin (Salesforce Developers) 
sornasorna

All Answers

Sam.arjSam.arj

This is the format:
 
  https://YOUR INSTANCE.salesforce.com/YOUR OBJECT ID

example:

  https://cs2.salesforce.com/00120000000WNVDAA4

GoForceGoGoForceGo
I need the dynamic instance...based upon the actual server...it could be cs2, na1, na2, na5...



Sam.arjSam.arj
Where are you developing s-control or VF page?
In each there are some functions that you may be able to use.


GoForceGoGoForceGo
VF/Apex


TehNrdTehNrd
http://ideas.salesforce.com/article/show/10091004/Apex_method_to_identify_salesforcecom_server
Sam.arjSam.arj
You may be able to use the following code:

Code:
  PageReference myPage = ApexPages.currrentPage();
  
  String Url = myPage.getUrl();

 


 

GoForceGoGoForceGo
TehNrd, thanks. I will add my vote to the idea...

based on the link, you posted, the best idea so far seems to be using login.salesforce.com?startURL=Object.id...won't work for sandbox, but it seems better compared to creating a custom object (that's what I had thought  of as well).

Sam.arj.

I tried that before.  http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=1099

It just returns a relative URL, not the full URL. I took a standard controller for Account(my context isn't a VF page), and tried using getuRl...didn't work...


Sam.arjSam.arj
You can always use the following javascript code to get the URL:

Code:
// getDomain() is called to obtain the domain portion of web app
function getDomain()
{
    var url = window.location.toString();
    var domain = url.match( /:\/\/(www\.)—([^\/]+)/ ); 
    domain = domain[2]?domain[2]:''; 
          
    return domain;
}

Message Edited by Sam.arj on 07-29-2008 10:10 AM

Message Edited by Sam.arj on 07-29-2008 10:18 AM
GoForceGoGoForceGo
Thanks! Sam,

Sounds like a good idea...won't work for me...

My code is in a trigger that is fired due to a workflow rule being executed. I am trying to send e-mail reminders to people on their tasks (with a URL link to task)...

See http://community.salesforce.com/sforce/board/message?board.id=apex&message.id=5896

TLFTLF

Hi GoForceGo,

Did you ever figure this one out? I'm trying to do something very similar to what you are doing, i.e., programmatically create a task and email notification which contains the full URL to the task, using Apex. Just wondering if you ever came up with a satisfactory solution on this before I go and try to re-invent the wheel. TIA.

GoForceGoGoForceGo
The best solution so far is:

login.salesforce.com/?startURL=Object.id

This won't work for Sandbox, but will do the job for production.



TLFTLF
Hmm... I tried that. I kept getting a "URL No Longer Exists" error. That's ok. I worked around it by saving the Salesforce hostname as a field on a custom object type that I use to store other application settings. That approach is good enough for my purposes. Thanks.
GoForceGoGoForceGo
May be the object got deleted.

A custom "configuration" object is obviously a good idea!




sornasorna
This was selected as the best answer
GoForceGoGoForceGo

Cool and thanks!  Hidden stuff

 

A standard page's header has "Host" in it's map!

 

This works in Apex, but how would I get the server URL in a standard e-mail template? Frequently you want to to send a link to the user to an record in an e-mail template.