2

Trying to run the following command on my org using sfdx (Upserting csv into my scratch org) :

'sfdx force:data:bulk:upsert -s Product2 -i ExternalId__c -f data/srv-allproducts.csv -u MyORG '

This works fine and do upsert products based on my ExternalId.

=> BUT I have a custom lookup on product which points to My custom Object and I want to populate it with its own ExternalId__c field.

Seems like with this command in DX I only have a way to set ExternalId for the object I'm upserting but don't have a way to map certain lookups to their external unique keys? Can I add some extra params somehow?

Getting MALFORMED_ID Error when trying.

5

I think you're misunderstanding the purpose of the -i parameter in the Salesforce CLI force:data:bulk:upsert command.

In your use case, you can use -i to identify which field in the Product2 object the upsert operation will use to determine whether a row of your CSV file will be treated as insert or update. And if update, which record will be updated. In some cases this can be an external ID.

But as you astutely have brought up, external ID has a secondary purpose, which is to perform record matching across relationships and ensure that when a record is inserted the relationship to another object is created.

This is not the purpose of the -i flag.

While mapping is widely done and understood, it does not appear to be implemented in the Salesforce CLI currently.

For this reason, all header rows field names must match the Salesforce API name of fields in the object. For upsert with relationships and external Ids, you must use the header row of your CSV to indicate the related external ID field that a value corresponds to. Let's say the value you have is stored in My_Custom_Object__c.External_Id__c. Your CSV might look like this:

Name, MyCustom_Object__r.External_Id__c, Other_Field__c
Product 1, 12345, Rutabega
Product 2, 98765, Beetroot

There are more detailed rules for prepping CSVs in the bulk API developer guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.