Is there a queryable ressource through the API from a Salesforce sandbox instance that can return the related production environment organization ID?
I don't think this is really an answer to the question as asked, but it is an effective workaround....
- Create a Hierarchy Custom Setting called "Production Org Ids".
- Add two custom fields, Org_Id_15_char__c, and Org_Id_18_char__c.
- Populate the Default Organization Level Value with your Production Org Id's 15 and 18 character values.
Why a custom setting? So it gets faithfully re-created in your Sandbox, AND so you can populate the values using Apex in your unit tests.
Why a Hierarchy type Custom Setting? So it's available for Process Builder, Workflow Rules, and Formula fields.
I this is the basis of a design pattern I call "Instance Awareness". By comparing the Custom Setting to
UserInfo.getOrganizationId() we can ascertain whether or not the code is running in a Production Org or not.
I use this technique to ensure that API calls made from a Sandbox don't ever go to a Production endpoint.
Got production org id by parsing Deployment Settings page right from apex code:
// get content of 'Deployment Connections' page from setup String pageContent = new PageReference('/changemgmt/deploymentSettings.apexp').getContent().toString(); // regex to parse producion org id from html table markup Pattern productionOrgIdRegEx = Pattern.compile('(?i)/changemgmt/deployConnectionDetailPage.apexp?id=(\\w+)'); Matcher matcher = productionOrgIdRegEx.matcher(pageContent); String productionOrgId = matcher.find() ? matcher.group(1).trim() : null ; System.assertEquals( null, productionOrgId );