Determine whether the Salesforce organization is a Sandbox or a Production

To determine if a Salesforce organization is a sandbox or a production environment in Apex. Salesforce's sObject Organization has a field called IsSandbox, which is true for sandboxes and false for Production.

Boolean isSandbox = UserInfo.getIsSandbox();

Organization org = [Select Id, Name, IsSandbox from Organization];

if (org.isSandbox) {
    System.debug('The organization is a sandbox.');
} else {
    System.debug('The organization is a production environment.');

}


You can use this check to conditionally execute logic based on whether the Apex code is running in a sandbox or production environment. This can be useful for implementing different behaviors or configurations specific to each environment.

In Salesforce, the Organization object represents metadata and settings for a Salesforce organization. It provides information about the organization's configuration, such as its name, address, edition, features, limits, and more. The Organization object is represented by the Organization SObject in Apex and API.

Here are some common fields available on the Organization object:

Name: The name of the organization.
Address: The address of the organization.
Phone: The phone number of the organization.
IsSandbox: Indicates whether the organization is a sandbox or production environment.
Edition: The edition of Salesforce (e.g., Enterprise Edition, Developer Edition).
NamespacePrefix: The namespace prefix assigned to managed packages installed in the organization.
DefaultNamespacePrefix: The default namespace prefix for unmanaged code in the organization.
Limits: Information about the organization's limits and usage (e.g., daily API requests, file storage).
Features: Flags indicating enabled features and settings in the organization (e.g., Lightning Experience, Chatter, Communities).

You can query the Organization object in Apex using SOQL to retrieve information about the organization. 

For example:

Organization orgInfo = [SELECT Id, Name, Phone, IsSandbox, Edition, NamespacePrefix FROM Organization LIMIT 1];

System.debug('Organization Name: ' + orgInfo.Name);
System.debug('Phone Number: ' + orgInfo.Phone);
System.debug('Is Sandbox: ' + orgInfo.IsSandbox);
System.debug('Edition: ' + orgInfo.Edition);
System.debug('Namespace Prefix: ' + orgInfo.NamespacePrefix);

It's important to note that not all fields on the Organization object are accessible to all users. Some fields may require special permissions or access settings to be queried or modified. Additionally, certain fields may only be available in specific contexts or environme

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach