1

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. – Daniel Ballinger Oct 2 '17 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 '17 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 '17 at 21:13
  • @sfdcfox Can you say more about the Visualforce option? How does that work differently a class to handle the import? – HomerJ Oct 2 '17 at 21:14
  • 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 '17 at 21:21
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.