6

I need to check the type of Sandbox to verify whether it is one of the following

  • Partial Copy Sandbox
  • Full Sandbox
  • Developer Sandbox

The Organization object helps in identifying if its a sandbox as follows:

[SELECT Id, isSandbox FROM Organization LIMIT 1].isSandbox

But, how to check the Type of Sandbox?

7

I don't think there's a straightforward solution.

As you've said, we have the Organization SObject which we can query to find certain environment information, such as:

SELECT Id, InstanceName, IsSandbox, Name, OrganizationType FROM Organization

This will tell us if we're on a sandbox environment, but it won't tell us which of sandbox it is.

The best solution I can offer is to include sandbox type in the sandbox type when creating them.

So if you have the following environments:

  • Prod: CompanyName
  • Sandbox: CompanyName.UAT
  • Sandbox: CompanyName.QA
  • Sandbox: CompanyName.DEV
  • Sandbox: CompanyName.DEVPRO

You will be able to query Organization.Name to find the current environment type.

It's pretty messy and requires the use of a naming convention in order to work, but I think it's the easiest way for now. Probably worth a post on the Ideas site, as it would be a pretty useful feature.

| improve this answer | |
1

Depending on the name of your Sandbox, you could retrieve this information from the base URL.

Something like:

String baseURL = URL.getSalesforceBaseUrl().getHost();
String testPrefix = '--test';

Boolean isTestSandbox = baseURL.containsIgnoreCase(testPrefix);

System.debug('Value of baseURL: ' + baseURL);
System.debug('Value of isTestSandbox: ' + isTestSandbox);

Debug results:

[4]|DEBUG|Value of baseURL: companyname--test.csXX.my.salesforce.com [5]|DEBUG|Value of isTestSandbox: true

| improve this answer | |
0

I have a use case where I want to create sample test data with a SandboxPostCopy class. It runs into this problem how do I know if it's a dev with no data or a partial or full with data.

I thought of the solution if you query for Account data, the probably most used object in the system, and if records are returned then you know it's a full or partial and if there are no records then it's a dev.

| improve this answer | |
-3

As we all know sandbox username looks like "behera@mycompany.com.dev "

dev- is the sandbox name, How can we get it this dynamically ?

Use below code :-

if(UserInfo.getUserName().substringAfterLast('.com')!= null)
                    contact.Email = contactInfo.Email+'.'+UserInfo.getUserName().substringAfterLast('.');
                else
                    contact.Email = contactInfo.Email;
}

UserInfo.getUserName().substringAfterLast('.') - will give you sandbox name.

UserInfo.getUserName().substringAfterLast('.com') will tell you whether anything is there after " .com ". if not there that means we are in production otherwise in sandbox. We can also used same method to identify type of org like prod or non prod. There is another approach for this.

Organization org = [select id,IsSandbox from Organization]; org.IsSandbox = true if it a sandbox. It willwork classs of higher version.

Feel free to suggest more.

| improve this answer | |
  • if its a sandbox, how to check the type of sandbox? – San Tosh Feb 18 '16 at 7:49
  • This solution may not work if users change their username. – javanoob Feb 18 '16 at 12:51
  • This doesn't answer the question, it only shows the name of the sandbox, not the type. Also only works if the current user's username ends in .com. – Ben Aug 3 '16 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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