+ Start a Discussion
Sébastien Van Cutsem 8Sébastien Van Cutsem 8 

Custom settings as environment variable ?

We would like to avoid to hardcode value in an apex class and use "environment variable". Is the use of custom settings the right way to do that ? 

Today I created a custom settings containing 2 fields, an url and a authorization token. The values are differents from one sandbox to another, so I create one data set for each of my sandbox (dev-int-uat-prod). Now my problem is to know in wich sandbox am I ? To retrieve the right values from my custom setting and of course I don't want to hardcode it. I used UserInfo.getUserName().substringAfterLast('@').substringAfterLast('.') to retrieve the sandbox name but for production this code doesn't work, so can anyone help me on the best way to do that ?

Thanks in advance for your answers and your help.

You can use org Id. 

Which will be different in all orgs.  

You can get that in apex code by UserInfo.getOrganizationId

in order to save in custom settting you can get the org Id from

Setup | Company Profile | Company Information 

there you can find "Salesforce.com Organization ID"

Hope this might help you .

Abhishek BansalAbhishek Bansal
Hi Sebastein,

You are making it complex.
Just follow the steps mentiond below :

1. Create a List custom setting with two fields.
2. After createing two fields , Click on Manage and create a new record of Custom setting.
3. Enter the value of two fields according to the organisation.
4. You will migrate this Custom Setting into different environments but in each environment you have to create records manually so you can enter the value of those two fields differently in each organisation
5. Please make sure that the name of the record which you are creating in each Environment is same as you will reference the record with this name.
6. Now use this custom setting record in your apex class.

Please let me know if you need more clarification on this.

HI  Sébastien ,

Custom metadata is the better approach then custom setting . I am not sure why you want to track which organisation you are ? how it will help you after knowing the organisation details .

Instead of creating the custom setting I think it will be beneficial to use custom metadata if you are not modifying your configuration programmatically in such case custom metadata will not work.

You can deploy the custom Metadata and the other most important thing is that they get refreshed to sandboxes, so you don't need to create Apex classes to generate the default Custom Setting records.

Hope this is helpful to you.

Schmitt HickersonSchmitt Hickerson
You could directly fetch the Org data using Organization global variable 
e.g., {!$Organization.Id} {!$Organization.Name}
Or, you could use SOQL on Organization:
[SELECT ID, Name FROM Organization]
If you have additional settings for each Org that you want to access in Apex, you can go for Custom Setting. Otherwise, it is fine to simply use the MyKFCExperience (https://www.mykfcexperience.us/)
Ajeeth Kumar SAjeeth Kumar S

Hi sebastien,

Your code is working for me.Pelase follow like below.

You can check using Anonymous Wondow in production developer console.

open developer console and Ctrl+E .

type your code like below.And excecute





amelia pearlamelia pearl
You could directly fetch the Org data using Organization global variable e.g., {!$Organization.Id} {!$Organization.Name} MyKFCExperience (https://www.mykfcexperience.xyz/)