0

So problem is with Contact records. There is required lookup field called Account Name ( API Account ). So i did download records with SFDX commend :

sfdx force:data:tree:export --query "SELECT FirstName, LastName, Account.ExternalID__c, CurrencyIsoCode, Phone, MailingAddress, email FROM Contact" --prefix demo-data --outputdir FixedData --plan

But when i try to then use this :

sfdx force:data:tree:import -u [email protected] -f FixedDane\demo-data-Contacts.json

I do get error:

=== ContactRef1 [1] STATUSCODE MESSAGE FIELDS ───────────── ───────────────────────────────────────────── ────── INVALID_FIELD Cannot reference a foreign key field Account.

{
    "records": [
        {
            "attributes": {
                "type": "Contact",
                "referenceId": "ContactRef1"
            },
            "FirstName": "Villllli",
            "LastName": "BuyNow",
            "Account": {
                "attributes": {
                    "type": "Account",
                    "url": "/services/data/v43.0/sobjects/Account/0010E00000PQAnHQAX"
                },
                "ExternalID__c": "000000000000000001"
            },
            "CurrencyIsoCode": "USD",
            "Phone": "33 810 22 32",
            "MailingAddress": {
                "city": "Kraków",
                "country": "Poland",
                "countryCode": "PL",
                "geocodeAccuracy": null,
                "latitude": null,
                "longitude": null,
                "postalCode": "33-332",
                "state": null,
                "stateCode": null,
                "street": "Juliana Dunajewskiego 1"
            },
            "Email": "[email protected]"
        }
    ]
}

I did add to Account object field ExternalId and marked it as external Id field. Where is my mistake ?

3 Answers 3

1

It seems to be a fundamental problem if both nested records are to be created and at the same time the link to another object is to be established on the basis of a relationship.

Similar problems are described here:

A solution has unfortunately not been posted anywhere yet.

UPDATE: It seems that it doesn't work when using "composite/tree". I solved the problem by dividing the call into multiple subrequests and using the composite resource. This makes it possible to create the record and link it to the parent in a first step, then get the ID and finally create the child records - all in one request.

You will find a detailed description and examples here:

In your case, I would first create the contact and link it to the account. Then retrieve the ID of the contact and finally assign the mailing address to the contact using the ID.

I hope this helps.

1

There is a different way to achieve the problem you have.

You should redesign your query as parent-to-child query, like following

sfdx force:data:tree:export --query "SELECT Name, ExternalID__c, (SELECT FirstName, LastName, CurrencyIsoCode, Phone, MailingAddress, email FROM Contacts) FROM Account" --prefix demo-data --outputdir FixedData --plan

and then run import

sfdx force:data:tree:import -u [email protected]  [email protected] -p FixedData/demo-data-Account-Contact-plan.json                                  

It works for the one-level deep, if multiple levels required, two exports can be done and then the plan files should be merger into one, where the intermediate object should have both properties "saveRefs": true and "resolveRefs": true set to true

0

So to solve my problem ( i did it before A. Kaspar answer ) i made a code that change my filed downloaded with SFDX in to compoisteAPI files and I push them with regular API requests.

I will check Kaspar answer today and will try to give you guys info on which options is easier to perform.

To those who do not know what compositeAPI is: https://tddprojects.atlassian.net/wiki/spaces/SF/pages/451477505/Composite+APIs

You must log in to answer this question.

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