Are looking to build a data-factory in order to spin up sandboxes with customized and reproduce-able data. We'd like to use a process similar to how we utilize the Test.loadData method to reference static resource that contains the records in question when we build test classes. However, I'm having issues finding documentation to support the actual insert of these records. How would we accomplish this.

Specifically: We want to be able to load, from a file (or series of files) Accounts, Contacts, and two additional custom child objects. In order to easily customize or expand the data sets, we want the class to reference file(s) and import the records as written.

  • 2
    Does this have to be Apex native? If not, have a look at the SFDX CLI data commands. Oct 2, 2017 at 19:39
  • 1
    If you'd like to be able to do so automatically, you would use SandboxPostCopy. However, there's no tool like Test.loadData that you could use. If you instead create a Visualforce page, you could call the SObject Tree API. This data could be loaded from a Static Resource.
    – sfdcfox
    Oct 2, 2017 at 20:01
  • @DanielBallinger It doesn't HAVE to be Apex native, but we assumed it would be easiest to load a predefined dataset to a fresh sandbox via the ApexClass field on the sandbox refresh page.
    – HomerJ
    Oct 2, 2017 at 21:13
  • 2
    @HomerJ The point would be about the same as using the sfdx cli; you would load a VF page, and click a button to call the SObject Tree API (basically, JSON that describes records and their relationships). It does offer the convenience of not having tools installed, but basically plays out the same way. Unfortunately, asynchronous code doesn't get a session ID, so you couldn't call the SObject API using the SandboxPostCopy handler.
    – sfdcfox
    Oct 2, 2017 at 21:21
  • 1
    @sfdcfox, your last sentence is pretty important and I could see a developer spending hours working on the callout, including unit tests with mocks only to discover it doesn't run during SandboxPostCopy due to absence of a sessionId. Be a useful SFDC enhancement to provide a sessionId, perhaps for the user that initiated the sandbox create/refresh
    – cropredy
    Oct 2, 2017 at 22:06

1 Answer 1


As mentioned in the comments, the Salesforce CLI data commands could be really useful here. The catch is that they aren't Apex based, so you will need to drop out to the command line to populate the data into an org. That also means registering your new sandbox org with the SFDX cli tooling.

See Ways to Add Data to Your Scratch Org and Export and Import Data Between Orgs for examples of how this could work. While targeted at scratch orgs this should still work for Sandbox orgs.

You must log in to answer this question.

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