If we think about it, it’s an interesting problem; To find Salesforce org type via Apex code. There can be numerous use cases where one might need to identify the type of org he is in. Perhaps, there are certain lines of code that should only be executed if those are on a Developer org or perhaps, there is a code block that shouldn’t run on certain Sandboxes but others. It’s all about each individual’s use case that can define the advantage of knowing the current org/Salesforce instance details.
Before I describe how a user can know the Salesforce org details via Apex code, I want to discuss the basics of Salesforce Organizations.
What’s a Salesforce Org?
As per Salesforce’s official definition in their Glossary:
“An org is an abbreviation of organization as it pertains to a deployment of Salesforce with a defined set of licensed users. An org is the virtual space provided to an individual customer of Salesforce. Your org includes all of your data and applications, and is separate from all other orgs.”
Now that is established that a Salesforce org is a virtual space or a container to hold all the data, metadata, and apps for a single entity – individual customer, company, developer, etc, it should also be discussed that technically, an org, instance, and sandbox are all different things and have different definitions even they are often used interchangeably.
A Salesforce instance is not a Salesforce org but it is a “cluster of software and hardware represented as a single logical server that hosts an organization’s data and runs their applications.” We can consider an instance more of a hosting thing.
Meanwhile, a sandbox is an identical copy of a Production organization. We can say that every sandbox is a separate org with different org ids but not vice versa. Because a Production org is not called an org.
While we are talking about all this, let’s also discuss the term “Environment” because it’s also very common. An environment itself is not a separate entity of its own, instead, it is more of a logical definition of a certain org or collection of orgs. For example, a Salesforce org that has live access to users can be called a Production Environment. Similarly, the organizations that we use for development can collectively be called Development Environments. This normally includes our sandboxes, Scratch, and Developer Edition orgs. And the same logic for defining Testing Environment.
Types of Orgs
So, if we talk about the types of orgs, we would have the following:
- Production Org
- Developer Edition Org
- Scratch Org
- Partial Copy
- Developer Pro
Yes, that is all. If I missed a secret type of org, do let me know in the comments!
How can we identify the current org from the Apex code?
Following is the standard solution.
If we want to know about the org details, the following are the steps that we can take.
Querying the Organization Object
Salesforce provides us with an object called “Organization” and by querying it in Apex code or Query Editor we can get almost all of the basic information about the current org.
The query for the most basic knowledge will look like this.
SELECT Id, Name, OrganizationType, IsSandbox, InstanceName FROM Organization
The output will vary for each organization. For example, my personal Developer Edition org returned the following:
The good thing is it also tells about the instance, therefore you can know both the org type and instance name at the same time.
With the Organization Type, you can identify the edition of your org such as Developer, Enterprise, etc.
As the name suggests, IsSandbox tells you whether your org is a sandbox or not. Since I am using a Developer Edition which is technically not a sandbox, it shows the value as False.
Although, one very important piece of information is missing from this data; The Sandbox Type.
How to identify the Sandbox Type in Apex?
By sandbox type, I mean Full, Partial Copy, Developer Pro and Developer. The question is how can we identify which sandbox type we are using via the Apex code. Is there any standard way of doing this? Unfortunately not. Because the aforementioned Organization object doesn’t give us this information.
However, there is a workaround for this. You need to fetch the URL of the current org via System.URL and analyze it. It should be noted that the sandbox name is included in the URL, so you can check the name of the sandbox and based on that it can be concluded that on which exact sandbox the code is currently executing.
To get the URL, you use the following code.
Let’s say it returns, talhaSaqib–partialCopy.sandbox.salesforce.com.
The ‘partialCopy’ is the custom name of the sandbox that I gave to my partial org. If I look for this key term in the URL string, I can identify if the current org is PartialCopy sandbox or not. The important thing is to know the names of the sandboxes.
And I guess, it’s time to wrap up the topic.
I hope by now you are clear on how to find Salesforce org type via Apex code. I cover a new Salesforce guide every week, so if you are interested in problem-solving, keep visiting this blog. Last time, I wrote a detailed guide on adding cross-object fields as merge fields in Lightning Email Template, so read that out too if haven’t already.
See you with a new Salesforce guide next week.
- Salesforce Glossary
- Salesforce Instances vs Orgs vs Environments
- Salesforce organizations
- Can we determine if the Salesforce instance is production org or a Sandbox org?
- How to implement Grid System using CSS in VF page with renderAs PDF
- Convert Country Code to Country Name in Google Sheets for Data Import Wizard
- How to delete a project in DevOps Center Salesforce?