This is my slightly less manual approach during the data import, but fundamentally the same as specimen's.
Before you begin
Replace the dataImportApi.js with this slightly modified rendition: dataImportApi.js
Be sure to backup the existing version. The revised file includes a feature to lookup existing records in the scratch org during the import.
Location: \Users\YOUR_USER\AppData\Local\sfdx\client\node_modules\salesforce-alm\dist\lib\data\dataImportApi.js
This also includes the fix specimen pointed out.
- Build your exports
sfdx force:data:tree:export --targetusername sandbox --query "SELECT IsActive,Name,ProductCode, (SELECT CurrencyIsoCode,IsActive,Pricebook2Id,Product2Id,UnitPrice,UseStandardPrice from pricebookentries) FROM Product2 where productcode in ('1004','1891','1088')" --outputdir .\data\ --plan
- Add a Pricebook lookup file
Add the following file to your exrtact folder: standardPricebook-lookup.json. add the following content:
{
"records": [
{
"attributes": {
"referenceId": "Pricebook2Standard",
"type": "Pricebook2",
"query":"IsStandard=true"
}
}
]
}
- Edit your plan file and include the lookup file
In the plan file, add reference to the price book lookup file:
[
{
"sobject": "Pricebook2",
"saveRefs": true,
"resolveRefs": true,
"files": [
"standardPricebook-lookup.json"
]
},
{
"sobject": "Product2",
...
- Update your PricebookEntrys extract with a referenceId of the standard price book
For all entries, replace the Pricebook2Id with the reference variable we looked up.
{
"records": [
{
"attributes": {
"type": "PricebookEntry",
"referenceId": "PricebookEntryRef2"
},
"Pricebook2Id": "@Pricebook2Standard1",
"UnitPrice": 200,
"IsActive": true,
"UseStandardPrice": false,
"Product2Id": "@Product2Ref5"
},
{
"attributes": {
"type": "PricebookEntry",
"referenceId": "PricebookEntryRef4"
},
"Pricebook2Id": "@Pricebook2Standard1",
"UnitPrice": 24,
Note that the reference id must be prefixed with "@" and include a record number. In our case, it will allways be "1" as there is only one standard price book in any SFDC org.
Load the data into a scratch org
Lastly, run the import to the org with the plan command.
sfdx force:data:tree:import --plan .\data\Product2-PricebookEntry-plan.json
Other uses
The lookup should work for any other purpose. What triggers the lookup instead of an insert is the addition of the query value. You may only include exactly 1 record per file.
{
"attributes": {
"referenceId": "Pricebook2Standard",
"type": "Pricebook2",
**"query":"IsStandard=true"**
}
The records returned will include the the record Id and add the Ids to the value map, using the following format:
@referenceId#