Sub topic 1
What a sandbox is and why you need one
Prerequisites
This chapter assumes you already know the following:
- You can find your way around Salesforce Setup and know what an org is.
- You can tell metadata (configuration and code such as objects, fields, flows, and Apex classes) from data (the records stored in those objects).
- You know that a production org is the live environment your users work in, and that changes usually move through a development lifecycle before they reach it.
What a sandbox is
A sandbox is a separate Salesforce org that starts as a copy of another org, usually your production org. The copy always includes the metadata. Depending on the sandbox type and options you choose, it also includes some or all of the data. Later chapters cover the types and options in detail.
Because the sandbox is its own org, it has its own login URL, its own users and its own limits. Nothing you do in it changes production. You can delete records, break a flow, or deploy half-finished code without affecting real customers.
A sandbox is a working copy, and it does not stay in sync with production on its own. After the copy is made, the two orgs drift apart. Each change in production or in the sandbox exists only in the org where it was made, unless you move it deliberately.
Why you need one
Production holds live customer records, runs automation that sends emails and updates data, and serves users who expect it to be available. Changing it directly carries three risks:
- Data damage. A bad data load or a flow with a faulty criterion can overwrite or delete real records. Recovery from backups takes time and may be incomplete.
- Disruption. A validation rule or required field added too early can block users in the middle of their work.
- No safe place to test. Many problems appear only with realistic metadata and, for some tests, realistic data volumes. An empty test org will not show them.
A sandbox lets you handle each of these by working in an environment that looks like production but is disposable.
Common uses include:
- Development. Build and unit test new Apex, Lightning components, flows and configuration.
- Quality assurance (QA). Run regression tests on a build before release.
- User acceptance testing (UAT). Let business users confirm a change does what they asked for.
- Integration testing. Connect external systems to a non-production org and check the full round trip.
- Training. Give new users a place to practice without touching live records.
- Debugging. Reproduce a production problem against a copy of the same configuration.
Where a sandbox fits in the lifecycle
A typical path moves changes from development through test environments to production. You build in one sandbox, validate in another, and deploy to production only after the change has passed checks.
The arrows from production to a sandbox are copies. The arrows between environments are deployments that you perform with tools such as change sets, the Salesforce CLI, or a source control pipeline. A sandbox never pushes its changes back to production by itself.
What the copy contains
A sandbox starts from a snapshot of the source org taken at creation or refresh time. In general terms:
| Item | In the sandbox |
|---|---|
| Metadata (objects, fields, Apex, flows, page layouts, profiles) | Copied |
| Record data | Depends on the sandbox type and on any template you apply |
| Users | Copied, with changed usernames and, by default, no way to email them from the sandbox |
| Production passwords | Not usable to log in to the sandbox unless the usernames are adjusted correctly. Use the sandbox login URL and the sandbox username |
| Changes made after the copy | Not included until you refresh |
The data row is the main thing that separates the sandbox types, and it is why choosing a type is its own decision, covered in a later chapter.
Behavior that differs from production
Salesforce changes a few things in a sandbox so that testing does not harm real people or systems. These differences surprise many new users.
- Usernames. Each username has the sandbox name appended. For example,
[email protected]in production becomes[email protected]in a sandbox nameddev1. - Email deliverability. Email access is set to System email only by default. Emails triggered by workflows, flows and Apex do not reach customers. An admin can raise the setting when testing needs real delivery.
- Login URL. You sign in at
https://test.salesforce.com, or through your My Domain sandbox URL, instead of the production login URL. - Integrations. Connections to external systems are copied as they were in production. Endpoints and credentials may need to be changed so the sandbox does not call live systems.
The last point matters most for data safety. A sandbox that still points to a live payment gateway or a live external system can send test activity to that system. Always review named credentials, remote site settings, and scheduled jobs after a copy.
Detecting the environment in code
Code often needs to behave differently in a sandbox than in production. For example, you might skip a call to a live service. The Organization object has an IsSandbox field for this purpose.
Organization org = [SELECT Id, IsSandbox FROM Organization LIMIT 1];
if (org.IsSandbox) {
System.debug('Running in a sandbox');
} else {
System.debug('Running in production');
}You can run this snippet in the Developer Console as anonymous Apex. Open the Execute Anonymous window, paste the code, and check the debug log. Use this check to guard behavior that must not run outside production. Avoid using it to hard-code settings. Store environment-specific values, such as endpoint URLs, in custom metadata or named credentials so that they can be changed without a code deployment.
Common mistakes
- Treating the sandbox as a backup. A sandbox is not a backup of production. It is incomplete by design, and a refresh replaces its contents.
- Doing all work in one sandbox. If development, QA, and UAT share an environment, a half-built change can block testers. Use separate sandboxes for stages that need to be stable.
- Leaving changes only in the sandbox. Work that exists only in a sandbox is lost when that sandbox is refreshed or deleted. Keep changes in source control or deploy them to the next environment promptly.
- Forgetting that data drifts. Test results from a sandbox reflect the data as of the last copy. Results can differ from production if the data has since changed.
- Testing with live integrations. Check endpoints and credentials before running tests that send data outward.
Key points
- A sandbox is a separate org copied from a source org, used for work that should not touch production.
- It always copies metadata. Whether it copies data depends on the sandbox type and options.
- It does not stay in sync. You move changes between environments on purpose, and you refresh a sandbox to bring it back in line with production.
- Salesforce modifies usernames and email settings to reduce the risk of side effects, but you must still check integrations yourself.
- Code can detect a sandbox by querying
Organization.IsSandbox.
Exercise
A teammate builds a new flow directly in a sandbox named dev1 and later refreshes dev1 to get newer production data. The flow was never deployed anywhere. What happens to the flow, and what should the teammate have done?
Show answer
The flow is lost, because a refresh replaces the sandbox with a fresh copy of production's metadata and data, and production does not contain the flow. Before refreshing, the teammate should have deployed the flow to another environment, for example with a change set or the Salesforce CLI, or saved it to source control.
Exercise
Your Apex code calls a live external service. How can the code avoid calling it when it runs in a sandbox, and what is a better long-term way to handle the differing endpoints?
Show answer
Query the Organization object (SELECT Id, IsSandbox FROM Organization LIMIT 1) and skip the call when IsSandbox is true. A better approach is to keep the endpoint in a named credential or custom metadata record so that the sandbox points to a test endpoint, with no change to the code.