4

Is there a queryable ressource through the API from a Salesforce sandbox instance that can return the related production environment organization ID?

0

I don't think this is really an answer to the question as asked, but it is an effective workaround....

  1. Create a Hierarchy Custom Setting called "Production Org Ids".
  2. Add two custom fields, Org_Id_15_char__c, and Org_Id_18_char__c.
  3. 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.

0

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
);

enter image description here

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.