Sub topic 1
What a sandbox is and why you need one
Prerequisites
This chapter assumes you know the following:
- What a Salesforce org is, and how to find your way around the Setup menu.
- The difference between metadata (configuration and code) and data (records).
- 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 copy of your production org that runs separately from it. It holds a copy of your metadata, and depending on the sandbox type, some or all of your data. You can build, test, and break things there without any effect on production users or records.
Sandboxes are created from production or from another sandbox. They have their own login URL (test.salesforce.com or your My Domain sandbox URL) and their own usernames. Nothing you do in a sandbox reaches production unless you deploy it on purpose.
Why you need one
Changing production directly carries real risk. A faulty trigger can block record saves for the whole company. A bad data load can overwrite customer records. A sandbox lets you avoid these problems.
- Development. Build and change configuration and code without disturbing users.
- Testing. Run QA and user acceptance testing against realistic metadata, and often realistic data.
- Integration testing. Connect external systems to a non-production endpoint before you go live.
- Training. Let new users practice in an environment where mistakes cost nothing.
- Deployment rehearsal. Check that a release package deploys cleanly before you run it in production.
Where a sandbox sits in the lifecycle
A typical path moves changes through several environments. Each step gives you a chance to catch problems before production.
Checking which org you are in
Code and automation sometimes need to behave differently in a sandbox, for example to avoid contacting a live external service. The Organization object has an IsSandbox field for this.
Organization org = [SELECT Id, IsSandbox FROM Organization LIMIT 1];
if (org.IsSandbox) {
System.debug('Running in a sandbox');
} else {
System.debug('Running in production');
}Run this in the Developer Console's Execute Anonymous window. The debug log shows which message was printed.
Common mistakes
- Treating a sandbox as a backup. It is not one. Data in a sandbox can be stale, partial, or absent.
- Building only in production. Changes made there cannot be rehearsed and are hard to reverse.
- Assuming a sandbox behaves exactly like production. Usernames, email settings, and integrations change after a copy. The chapter on post-refresh changes explains how.
Exercise
A teammate wants to test a new Apex trigger that updates Account records. Why should they do it in a sandbox first, and how can the trigger's code tell whether it is running in a sandbox?
Show answer
A sandbox isolates the trigger from production users and records, so a bug cannot block saves or corrupt live data. The code can query the Organization object and check the IsSandbox field: [SELECT Id, IsSandbox FROM Organization LIMIT 1]. If IsSandbox is true, the code is running in a sandbox.