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
SergiSergi 

Identify the enviroment

Do you know if there's any way to know in witch enviroment (production or sandbox) is running an Apex application (class, trigger...)?

 

I mean inside the code of course. Any "global variable" that tells the enviroment?

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Ah, that would be useful. I'm not sure if there is a direct way of doing it, but I would use either of these,

 

1. Using 'Custom Settings' create a setting to server your purpose, or,

2. Use ApexPages.currentPage().getHeaders().get('Host'); to discover which server you are on and then decide based on the result.

 

Personally I'd use number one, it's quite a clean solution and can be thought of as global variables.

 

Custom settings can be accessed by clicking Setup > Develop > Custom settings.

 

Cheers,

Wes

All Answers

WesNolte__cWesNolte__c

Hey

 

I don't know of a way, but if you detail what you're trying to do perhaps there could be another solution.

 

Cheers,

Wes 

SergiSergi

What I want is to call an external webservice from an apex trigger.

So I have the URL hardcoded in a class variable.

 

While developing I have to call one webservice (test enviroment) and when on production I want to call the production server.

 

If this class variable could be parametrized depending on the enviroment, there will be dificult to deploy the testing URL to production and so... 

WesNolte__cWesNolte__c

Ah, that would be useful. I'm not sure if there is a direct way of doing it, but I would use either of these,

 

1. Using 'Custom Settings' create a setting to server your purpose, or,

2. Use ApexPages.currentPage().getHeaders().get('Host'); to discover which server you are on and then decide based on the result.

 

Personally I'd use number one, it's quite a clean solution and can be thought of as global variables.

 

Custom settings can be accessed by clicking Setup > Develop > Custom settings.

 

Cheers,

Wes

This was selected as the best answer
AkiTAkiT

Hi guys,

 

One alternative is to create a custom formula field of type text that contains API server url. From this string you can determine which system you're running in, csX, naX, emea...

 

Field can be deployed together with your code.

 

-AT