Explore Courses Blog Tutorials Interview Questions
+1 vote
in Salesforce by (11.9k points)

I am integrating our back end systems with Salesforce using web services. I have production and stage environments running on different URLs. I need to be able to have the endpoint of the web service call be different depending on whether the code is running in the production or sandbox Salesforce instance.

How do I detect the environment?

Currently, I am considering looking up a user to see if their user name ends in 'devsandbox' as I have been unable to identify a system object that I can query to get the environment.

Further clarification:

The location I need to determine this is within the Apex code that is invoked when I select a button in Salesforce. My custom controller needs to know if it running in the production or sandbox Salesforce environment.

1 Answer

+2 votes
by (32.1k points)
edited by

You can determine the environment in Salesforce through APEX by using getHost() method on the System.URL class.

Want to learn Salesforce from basics! Here's the right video for you on Salesforce provided by Intellipaat:

Sample Code:

System.debug('Host: ' + URL.getSalesforceBaseUrl().getHost()); So try public Boolean isSandbox() { return URL.getSalesforceBaseUrl().getHost().left(2).equalsignorecase('cs'); }

Browse Categories