How can I determine if the org that I’m currently connected to is a scratch org?

When I’m within a Salesforce org, how can I tell if that org is a Scratch Org or not?

Ideally this would be via Apex, but it could be an API call if required.

This would be useful information for the creator of a managed package who may not otherwise know what type of org is running the code.


I’ve had a couple of thoughts on how this might be achieved:

  1. Check if the pod/instance that Scratch orgs are created on are separate from the general population of sandbox orgs. I suspect that scratch orgs have dedicated hardware. E.g. a new scratch org I just created is on p0/CS31.
  2. Look for describe Metadata that is unique to only scratch orgs.

Answer

The SourceMember sObject is only present in scratch orgs. Its presence would be a sign that you are in a scratch org. The gotcha would be that it is only accessible via the Tooling API and not Apex alone.

https://developer.salesforce.com/docs/atlas.en-us.api_tooling.meta/api_tooling/tooling_api_objects_sourcemember.htm

Attribution
Source : Link , Question Author : Daniel Ballinger , Answer Author : Anthony Heber

Leave a Comment