Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As part of my OAuth2 flow, I need to determine the login URL for the current session from my Apex class.

In order to "Redirect User to Obtain Access Authorization", I have to know whether I should be using https://login.salesforce.com/ or https://test.salesforce.com/ (for a sandbox).

I thought I would be able to find some system method or configuration value to easily determine the URL, but have come up empty after searching Google, Stack Overflow and this site.

Is this possible? If so, how?

share|improve this question
up vote 1 down vote accepted

You can now use the isSandbox property of the Organization object to accurately determine the org type.

/* use a static property so the SOQL is only run once per request */
public static Boolean isSandbox { 
    get {
        if ( isSandbox == null ){
            isSandbox = [SELECT Id, IsSandbox FROM Organization LIMIT 1].IsSandbox;
        }
        return isSandbox;
    }
    private set;
}
share|improve this answer

You could use the System URL methods to get the current host:

URL.getSalesforceBaseUrl().getHost()

If the result starts with 'cs' then you know you're looking at a Sandbox and will need to use test.salesforce.com, otherwise use login.salesforce.com.

This isn't particularly pretty, but it will work.

share|improve this answer

Here's a link to a blog post on how to determine if you are in a Sandbox or not: http://www.michaelforce.org/recipeView?id=a0Ga000000Ekp65EAB. It takes into consideration custom domains and a few other special circumstances.

Here's the code he posted.

public class whereAmI{


    // First method will give us the edition as a string 

    public Static String getEdition(){

        // Organization object FTW!
        Organization[] org = new Organization[]{};
        org = [select Id, OrganizationType from Organization limit 1];

        // Check to make sure we have a result
        if(org.size()==1)
            return org[0].OrganizationType;
        else
            return '[still lost...]';
    }


    // Next method tells us if we're in a sandbox

    public Static Boolean isSandbox(){

        String host = URL.getSalesforceBaseUrl().getHost();
        String server = host.substring(0,host.indexOf('.'));

        // It's easiest to check for 'my domain' sandboxes first 
        // even though that will be rare
        if(server.contains('--'))
            return true;

        // tapp0 is a unique "non-cs" server so we check it now
        if(server == 'tapp0')
            return true;

        // If server is 'cs' followed by a number it's a sandbox
        if(server.length()>2){
            if(server.substring(0,2)=='cs'){
                try{
                    Integer.valueOf(server.substring(2,server.length()));
                }
                catch(exception e){
                    //started with cs, but not followed by a number
                    return false;
                }

                //cs followed by a number, that's a hit
                return true;
            }
        }

        // If we made it here it's a production box
        return false;
    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.