6

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

4

2 Answers 2

2

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

2
  • Just found this example as I need to get the production org. But is it the regexp correct? Because after matching I get null and in fact the System assert at the end checks for the Id to be null. Am I missing something?
    – zurdo
    Nov 23, 2023 at 17:17
  • Ok, the regexp that worked for me is Pattern productionOrgIdRegEx = Pattern.compile('(?i)\\/changemgmt\\/deployConnectionDetailPage.apexp\\?id=(\\w+)');
    – zurdo
    Nov 23, 2023 at 18:10
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .