Sub topic 8
Sandbox templates: controlling which data is copied
This chapter assumes you know how to open Setup, that you can tell metadata (configuration and code) from data (records), and that you have read the earlier chapters on Partial Copy and Full sandboxes and their storage limits.
What a sandbox template is
A sandbox template is a saved list of objects. When you create or refresh a sandbox with a template, Salesforce copies records only for the objects on that list. The template controls data only. Every sandbox still receives all metadata from its source org: custom objects, fields, Apex classes, flows, page layouts, and permission sets. A template never removes a field or a class.
Templates are available for Partial Copy and Full sandboxes. Developer and Developer Pro sandboxes copy no production records, so there is nothing for a template to control.
Why use one
Without a template, each sandbox type applies its own default:
| Sandbox type | Default data behavior | What a template changes |
|---|---|---|
| Developer, Developer Pro | No records copied | Not available |
| Partial Copy | A sample of records, up to the data storage limit | You choose which objects are sampled |
| Full | All records for all objects | You can leave out objects you do not need |
Templates help in three situations:
- Smaller, faster copies. A Full sandbox that skips large objects such as activity history or custom logging objects finishes sooner.
- Focused test data. A QA team testing an order flow can request only Accounts, Contacts, Products, and Orders.
- Less exposure of sensitive records. If a team does not need an object that holds sensitive records, leaving it off the template keeps those records out of that sandbox. Templates are not a substitute for data masking, because the objects you do select still arrive unmasked.
How a template shapes a copy
The flow below shows what happens when a sandbox is created with a template.
Creating a template
You create templates in the production org, because that is where you create sandboxes. Follow these steps:
- In Setup, enter
Sandbox Templatesin Quick Find and open the page. - Click New Sandbox Template.
- Enter a name and a description. Use names that describe the purpose, such as
QA_OrdersorUAT_Cases, so others can tell templates apart. - In the object list, select the objects whose records you want copied.
- Save the template.
Some details affect what you get:
- Related objects are added for you. If you select an object that has a required relationship to another object, Salesforce adds the related object to the template. A template with Contact will therefore include Account. Review the saved list rather than assuming it holds only what you clicked.
- Selecting an object selects its records, not unrelated children. Records of an unselected object are not copied, even if they look up to a selected one. The lookup field exists in the sandbox but is empty for those rows.
- You can edit a template later. Edits apply to future creates and refreshes only. Existing sandboxes do not change.
Template behavior by sandbox type
Partial Copy
A Partial Copy sandbox copies a sample of records for the objects it includes. Without a template, Salesforce chooses the sample across objects. With a template, the sample covers only the objects you picked. The size limits still apply: the sandbox cannot exceed its data storage limit, and each object has a cap on the number of sampled records. Check the current limits in the Salesforce documentation, because they change between releases.
A template is the better choice when your test needs depth in a few objects. Selecting a smaller set of objects does not make the sandbox hold more records of each object beyond the per-object cap. It only stops the sample from being spread across unneeded objects.
Full
A Full sandbox copies every record of each selected object. With a template, objects you leave out arrive empty. This is useful when production holds very large objects that your testing does not touch. The Full sandbox storage limit matches production, so a template is mostly a time and relevance decision, not a storage workaround.
Using a template when you create or refresh
When you create a Partial Copy or Full sandbox from Setup, the creation page has a Sandbox Template lookup. Choose a template, or leave it blank to use the type default. When you refresh a sandbox, the same selection appears again, so you can switch templates between refreshes. A QA sandbox might use QA_Orders one cycle and QA_Cases the next.
If you clone a sandbox from another sandbox, the source sandbox is the data source. The template selection still decides which objects' records are copied from it.
Checking what actually arrived
After a copy completes, confirm the data is what you expected. Run this anonymous Apex in the new sandbox to count records for several objects:
List<String> objectNames = new List<String>{'Account', 'Contact', 'Opportunity', 'Case'};
for (String name : objectNames) {
Integer total = Database.countQuery('SELECT COUNT() FROM ' + name);
System.debug(name + ': ' + total);
}Open the Developer Console's debug log and filter for USER_DEBUG. Objects missing from the template should report 0, unless a SandboxPostCopy class or a later data load added records.
Common errors and gotchas
- Expecting metadata to be filtered. A template that leaves out an object does not remove the object's fields or Apex code. If a test fails because the object is empty, the cause is the template, not missing configuration.
- Missing parent records. If a child object is on the template but its parent is not, lookups may be blank. The automatic inclusion of required related objects covers required relationships only, so optional lookups can still be empty.
- Forgetting the template at refresh time. Refreshing without choosing the template applies the type default, which for a Full sandbox means copying everything. Check the selection each time.
- Treating the template as a security control. Excluding an object keeps its records out, but the objects you include are copied as they are in the source.
- Using templates with the wrong sandbox type. A Developer sandbox never shows the option. If you need data in a Developer sandbox, load it yourself.
Summary
Templates give you object-level control over which records a Partial Copy or Full sandbox receives, and they never alter metadata. Create them in the source org, name them by purpose, review the objects Salesforce adds automatically, and select the template on every create and refresh.
Exercise
Your QA team uses a Full sandbox but only tests the order process. You create a template containing Account, Contact, Product, and Order, then refresh the sandbox using that template. After the refresh, a tester opens the Case object and finds no cases, but the Case custom fields and page layouts are all there. Why, and how would you get cases back?
Show answer
Templates control data only, not metadata. Case was not on the template, so its records were not copied, but its fields, layouts, and code were still copied from the source. To get cases back, add Case (and any related objects) to the template and refresh again, or create a second template for case testing and use it on the next refresh. Alternatively, load a specific set of case records into the sandbox after the refresh.