+ Start a Discussion
Daniel Morton 15Daniel Morton 15 

sfdx force:data:tree:import MALFORMED_ID

Hi,

Just in case this helps somebody! It tooks quite a bit of time to track this issue down.

We've had an issue with the sfdx force:data:tree:import command failing with a MALFORMED_ID message when trying to resolve references during data imports.   It turns out the issue was realated to our namespace, which has numbers in it, which the sfdx tool didn't handle.  
 
An example is :-

plan.json
 
[
    {
        "sobject": "i42as__testParent",
        "saveRefs": true,
        "files": [
            "parent.json"
        ]
    },
    {
        "sobject": "i42as__testChild",
        "resolveRefs": true,
        "files": [
            "child.json"
        ]
    }
]

parent.json 
 
{
    "records": [
        {
            "attributes": {
                "type": "i42as__testParent__c",
                "referenceId": "parentRef1"
            },
            "i42as__Message": "Hello"
        }
    ]
}

child.json
{
    "records": [{
        "attributes": {
            "type": "i42as__testChild__c",
            "referenceId": "testChildRef1"
        },
        "i42as__parent__c": "@parentRef1"
    }]
}

When run with the following command, this returns an error.  
 
sfdx  force:data:tree:import --plan plan.json

STATUSCODE    MESSAGE                                                                    FIELDS
────────────  ─────────────────────────────────────────────────────────────────────────  ───────────────────────────
MALFORMED_ID  Object: id value of incorrect type: @parentRef1  i42as__parent__c
=== testChildRef1 [1]

In turns out this was an error in the salesforce-alm library, the regex used to look for the @parentRef1 replacement was not expecting a number in the namespace.  I've managed to work around the issue by changing the file .local/share/sfdx/client/node_modules/salesforce-alm/dist/lib/data/dataImportApi.js as follows;-
 
const jsonRefRegex = /[.]*["|'][A-Z_]*["|'][ ]*:[ ]*["|']@([A-Z0-9_]*)["|'][.]*/igm;
const jsonRefRegex = /[.]*["|'][0-9A-Z_]*["|'][ ]*:[ ]*["|']@([A-Z0-9_]*)["|'][.]*/igm;



 
Eric HofmeisterEric Hofmeister
Thanks for the help, we were having the exact same issue with having a number in our namespace, and this solved our issue.
FilikinFilikin
Hi,
I am obviously missing something in this answer - but where is the dataImportApi.js file?
I just searched my file system, on both Windows and Mint and cannot find it.
Eric HofmeisterEric Hofmeister
Mac:
.local/share/sfdx/client/node_modules/salesforce-alm/dist/lib/data/dataImportApi.js

Windows:
C:\Program Files\sfdx\client\node_modules\salesforce-alm\dist\lib\data\dataImportApi.js

Those are where the files are located on my Mac and on Windows through Parallels.

Also, make sure that your regex doesn't have the bold tag:
const jsonRefRegex = /[.]*["|'][0-9A-Z_]*["|'][ ]*:[ ]*["|']@([A-Z0-9_]*)["|'][.]*/igm;
David WeryDavid Wery
Perfect workaroud, thanks!
Jens Petter AbrahamsenJens Petter Abrahamsen
On my Windows PC, I found that the dataImportApi.js file exists in several places, 4 to be exact. Although the one in program files is probably the original, the one that's being used is in C:\Users\xxx\AppData\Local\sfdx\client\node_modules\salesforce-alm\dist\lib\data\dataImportApi.js

The other instances I found were
C:\Program Files\Salesforce CLI\client\node_modules\force-language-services\node_modules\salesforce-alm\dist\lib\data\dataImportApi.js
C:\Program Files\Salesforce CLI\client\node_modules\salesforce-alm\dist\lib\data\dataImportApi.js
C:\Users\xxx\AppData\Local\sfdx\client\node_modules\force-language-services\node_modules\salesforce-alm\dist\lib\data\dataImportApi.js

One more thing:
SFDX has one more bug in the regex. It doesn't account for field names with numbers in them. Strange actually, when there are standard fields such as Product2 and Pricebook2. So the final regex is:
const jsonRefRegex = /[.]*["|'][A-Z0-9_]*["|'][ ]*:[ ]*["|']@([A-Z0-9_]*)["|'][.]*/igm;

 
Tom Hoban 2Tom Hoban 2
We encountered this issue too.  I'm happy there is a work around, but is there a plan to fix this in the tool?  At somepoint, we'll need to upload to a new version of sfdx and will be broken again.