[SalesForce] SFDX force:data:tree:import MALFORMED_ID

I am creating scripts to export and import test data for our sfdx scratch orgs. I've used the sfdx force:data:tree:export command with -p to generate my plan file, plus three object files (one parent (Account object), and two different child custom object types)

I'm at the point where I'm attempting import the plan into the target scratch org with sfdx force:data:tree:import, and I receive the following error:

MALFORMED_ID ChildObj2: id value of incorrect type: @namespace_xx__ChildObj2__cRef1 namespace_xx__ChildObj2__c
ERROR: ERROR_HTTP_400.

Here is the record that is causing the error (in the file namespace_xx__ChildObj1__cs.json), if I remove the line referencing the ChildObj2 record the import works, but the record of course doesn't link to ChildObj2 once imported.

{
  "attributes": {
    "type": "namespace_xx__ChildObj1__c",
    "referenceId": "namespace_xx__ChildObj1__cRef2"
  },
  "namespace_xx__Client__c": "@AccountRef2",
  "namespace_xx__Display_Name__c": "Some Business ChildObj1",
  "namespace_xx__ChildObj2__c": "@namespace_xx__ChildObj2__cRef1",   <<<< THIS LINE <<<<
  "namespace_xx__ChildObj2_Created_Date__c": "2018-07-12",
  "namespace_xx__Open_Date__c": "2018-07-12T19:16:47.000+0000",
  "namespace_xx__Phone__c": "888-123-5678",
  "namespace_xx__Qualified__c": false,
  "namespace_xx__Status__c": "Converted",
  "namespace_xx__Total_Calls__c": 0,
  "namespace_xx__Total_Emails__c": 0,
  "RecordTypeId": "0123B0000003FzfQAE"
}

Here is the json file it is referencing (namespace_xx__ChildObj2__cs.json):

{
  "records": [
    {
      "attributes": {
        "type": "namespace_xx__ChildObj2__c",
        "referenceId": "namespace_xx__ChildObj2__cRef1"
      },
      "namespace_xx__Client__c": "@AccountRef2",
      "namespace_xx__Open_Date__c": "2018-07-12",
      "namespace_xx__Primary_ChildObj1__c": "@namespace_xx__ChildObj1__cRef2",
      "namespace_xx__Status__c": "Open",
      "RecordTypeId": "0123B0000003FzhQAE"
    }
  ]
}

Here is the plan file contents:

[
    {
        "sobject": "Account",
        "saveRefs": true,
        "resolveRefs": false,
        "files": [
            "Accounts.json"
        ]
    },
    {
        "sobject": "namespace_xx__ChildObj1__c",
        "saveRefs": true,
        "resolveRefs": true,
        "files": [
            "namespace_xx__ChildObj1__cs.json"
        ]
    },
    {
        "sobject": "namespace_xx__ChildObj2__c",
        "saveRefs": false,
        "resolveRefs": true,
        "files": [
            "namespace_xx__ChildObj2__cs.json"
        ]
    }
]

Any idea on why I'm receiving this error? Any assistance is appreciated, I've spent most of the day on this.

Best Answer

Make sure that "resolveRefs" and "saveRefs" are set to true. For you basically it should be enough to set "saveRefs" to true for litify_pm__ChildObj2__c. When sfdx cli inserts its (litify_pm__ChildObj2__) records the "saveRefs" flag tells if Ids should be cached (remembered) for use by other objects.

It is very well explained by sfdcfox at What Are saveRefs and resolveRefs?

{
        "sobject": "Account",
        "saveRefs": true,
        "resolveRefs": true,
        "files": [
            "Accounts.json"
        ]
    }
    {
        "sobject": "litify_pm__ChildObj2__c",
        "saveRefs": true,
        "resolveRefs": true,
        "files": [
            "litify_pm__ChildObj2__cs.json"
        ]
    }
Related Topic