Differentiate Production/Sandbox Instance?
You should be able to write a formula that will expose a line saying "Production" or "Sandbox" by using your production id from Administer->Company Information.
There are limitations using formulas that will not allow us to access all system fields. My suggestion, and based on your request of using a formula, is to create a field of data type formula that will compare the running Organization.Id with your production id. I don't know all your requirements but this is a simple approach that perhaps can be of some help.
If we take this production id:
cs45 - 00D8A0XXXXXXXXX
We can add a simple formula on any object to obtain, or print out, if an instance is a sandbox or if it is production environment. Depending your requirements you need to select the object the better suits your logic. Let's say that you add this field on the User object.
Field data type: "Formula"
Formula Return Type: "Text"
On the "Advanced Formula" canvas add this formula:
IF( $Organization.Id !='00D8A0XXXXXXXXX', 'Sandbox', 'Production')
This is the most simple approach I can think off.
Hope it helps.
This is by no means a perfect solution as there are definitely limitations but will work in some orgs
/* True if a sandbox */
CONTAINS($User.UserName,'@mycompany.com.')
Here, the assumption is that in PROD, usernames are all [email protected]
but in any sandbox, they will be [email protected]
. Note the extra period
If your company is a .org
or a .co.uk
, etc. change accordingly
Definitely fragile if users are added manually to sandbox without following conventions of appending the .sandboxname
or if usernames in PROD aren't always ending in @mycompany.com
Not for use in (un)managed packages.
This will need some refinement in testing, but could probably be made to work.
Examine the API URL domain to see if it has a subdomain indicating a sandbox.
CONTAINS($Api.Partner_Server_URL_360, '.cs') ||
CONTAINS($Api.Partner_Server_URL_360, '\\cs')
The .cs
is to handle custom domains and the \\cs
for ones without.
Hat tip to @eyescream who had suggested a similar idea in Visualforce using the OrgId that made me think about looking for the domain.
I was going to suggest using a REGEX()
in the forumla, but that doesn't appear to be available for a Checkbox formula.
Expanding on my comment.
From the OrgId, you could look at the pod identifier to figure out if it is a Sandbox org. See Converting the Salesforce Pod Id to ServerUrl/Name and partial solution in Apex that could potentially be converted to a formula.
To your example sandbox OrgIds, we now need to use the first two characters after the keyprefix to identify pod.
E.g.
- 00680 -> NA6
- 0068A -> CS45
- 00620 -> EU0
- 00D29 -> CS19
The big problem with this approach is that it needs updating every time Salesforce adds a new pod. It's really fragile, so don't come looking for me if your code mistakes a sandbox for a production org and does something unsavory.