2

We would like to avoid to hardcode values 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 two fields, an url and an authorization token. The values are different from one sandbox to another, so I create one data set for each of my sandboxes (dev-int-uat-prod). Now my problem is to know in which sandbox I am? 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.

2 Answers 2

4

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 above.

0

wouldnt this work? (im facing the same issue and this is what i came up with)

Using an if statement to determine if its a sandbox, if it is use your original code, else set it to prod

String environment ='';
Organization org = [select id,IsSandbox from Organization];
    if (org.IsSandbox == true) {
        //if its a sandbox, peel off your username from the last '.' to get dev, test, etc
        environment = UserInfo.getUserName().substringAfterLast('.');
    } else {
        //if boolean returns false, then its prod
        environment= 'prod';
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .