1

Test.loadData is used to load CSVs into an object during Test Class execution.
This test data exists only in the eyes of the test class.

In order to load master data using Apex after a refresh, is there a Test.loadData equivalent to load CSVs as real, permanent, data?

1
  • 1
    You're going to have to write your own job.
    – Adrian Larson
    Sep 25, 2017 at 13:50

3 Answers 3

3

Use a sandbox "post copy" script, available since Spring'16.

Questions with this tag: .

Of course this will limit you to 10K rows created so if you need a complex structure - use this to kick off a batch job / series of @futures? Make a callout to other org from which you'll pull data... That kind of stuff.

I don't think there's easy way to automatically parse CSVs outside of unit tests, you'll need some custom code for that. But maybe you could store your sample data as JSON objects in static resource? Could be very handy & easy to load as long as you have some external id fields...

P.S. You do remember that this is a problem only in Developer (Pro) sandboxes? Partial Copy has built-in option to pull some slices of data during the refresh.

2
  • Thanks @eyescream. So I should deserialise JSON in Apex and insert? Sep 26, 2017 at 9:35
  • 1
    Pretty much yeah. Try to plan what data slices you need (which accounts, contacts...), SELECT them on prod, ideally with some unique external ids like Account Number. Save to your files (without Id field, maybe you can call clone() before serializing?), upload as static resources... And read up how upsert with external id field can easily link Contacts to account as long as account is referenced by that ext id, no need to manually map Ids between sandbox and production.
    – eyescream
    Sep 26, 2017 at 11:18
1

See the Salesforce DX CLI data commands:

sfdx force:data:tree:export -q "select x, y, z from My_Object__c"
sfdx force:data:tree:import --sobjecttreefiles My_Object__c.json 
0

There is no standard way of automatically loading data after refresh since Salesforce has no way of knowing what data is relevant for your org.

You will have to use Dataloader to load csv after refresh. Unfortunately loading for one object that way is fine, but if you want to build associated data set (accounts, corresponding contacts etc), it gets very tricky. You will have to load each file, replace lookup values in each object and load.

1
  • eyescream's answer is the the correct one
    – cropredy
    Sep 26, 2017 at 1:24

You must log in to answer this question.

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