+ Start a Discussion
UncrasherUncrasher 

How to dynamically get the web address?

I need a way to know what org I am in?  For example:

https://na7.salesforce.com

or

https://na4.salesforce.com

 

I want this so that I do not have to remember to manually change code when I move from DEV, to Sandbox to Production.

 

thanks,

Tom

 

Best Answer chosen by Admin (Salesforce Developers) 
sforce2009sforce2009

You can do what suits best to you. But when you move from prod to sanbox vice versa you dont have to point to CS or Na because that is a formula field... Once you create the field it automatically identifies which account it belongs to using {!$API.server_url} i believe. Again its a suggestion....

 

 

All Answers

sornasorna

If your are in a VF page, then you can get the instance...Please visit the follwing discussion:-

http://community.salesforce.com/t5/Apex-Code-Development/how-to-retrieve-server-URL-in-APEX-class-or-in-trigger/td-p/95659

 

Thanks,

sforce2009sforce2009

There is one property under $site or $Label I am not sure. Check the documentation on these.

Eager-2-LearnEager-2-Learn

Thank you for your responses.  The issue I am having is the code is all Apex via a schedule (no pages).  It produces an email and in the email I build an HTML table with contact names that is a hyperlink.  I wanted to have my code dynamically get the CS2 or NA4 portion for example.  That way I do not have to worry about forgetting to change prior to migration.

 

On another note, I have had issues with the sign up page coming up when I try to gain access to the forms; therefore, over the weekend I just created a new user (UNcrasher).  Is there a way to consolidate the two user users 'Eager-2-Learn' and 'Uncrasher' and/or is there a way to get into the forum when it keeps taking me to the sign up page?

RyanGuestRyanGuest

There isn't any easy way in Apex to get the current web address of the running code.

 

A couple of workarounds I've seen are to hard code the value in Apex or use a custom setting and update it to use the correct salesforce address.

 

You said that you were using scheduled apex, if you were using VisualForce I would suggest using: ApexPages.currentPage().getHeaders().get('Host');

UncrasherUncrasher

Then I guess I have to go with the hard-coded approach and hope I don't forget to change it prior to production move! :)

 

It would be nice if SFDC allows parameter in their Apex Schedule feature.  That way when I setup the job in production I would hard code it at the job level and have the program read the parameter.  I anticipate future release allowing parameter settings at the Apex scheduler level.

 

Thanks again.

sforce2009sforce2009

You dont have to hard code it on your app. Instead store it in some high level object like user using a formula field.. Then use that field in your app.

 

 

UncrasherUncrasher

I appreciate the suggestion but that would just be a different version of the same thing.  In other words, if I ever had to move the object to TEST (Sandbox refresh) then I would have to remember to change the formula to point to CS if I was working with the same code.  In fact that could be a worst situation because down the road I would stand the chance of forgetting even more.

 

I think with just the focus of conversation is going to assure that I do not forget when I move the code into production this month.

sforce2009sforce2009

You can do what suits best to you. But when you move from prod to sanbox vice versa you dont have to point to CS or Na because that is a formula field... Once you create the field it automatically identifies which account it belongs to using {!$API.server_url} i believe. Again its a suggestion....

 

 

This was selected as the best answer
UncrasherUncrasher

Well thank you for your persistance since I had my blinders on:smileyvery-happy:

I thought you were saying to hard code in a formula.  This looks like a solution that is worth checking into.  Thank you very much.

sforce2009sforce2009

I would be more than happy if it works for you. Thanks