For all the CLI enthusiasts, you can now use sfdx commands to manage your custom metadata types.
Note: If you're not familiar with CLI, then this might interest you.
To summarize, using cmdt commands you can:
- Create a custom metadata type.
- Create fields within the metadata object.
- Migrate Custom Object records or Custom Settings to Custom Metadata types.
- Create a record for a specified custom metadata type
- Insert new custom metadata type records from a CSV file.
You can find more info related to the commands here.
What we’re really going to discuss here are some gotchas and considerations for inserting new custom metadata type records from a CSV file.
The command used to insert cmdt type records via CSV is sfdx force:cmdt:record:insert.
When using the force:cmdt:record:insert
command, the DeveloperName
identifier defaults to the column Name
and is a required column. If you want to map the DeveloperName to a different column in you CSV, you can use -n.
Example: force:cmdt:record:insert –./my.csv -t My_CMDT_Name -n Label,
where Label is your csv column.
Your other CSV column names should correspond to the API name of the fields on your CMDT object.
Till now, it’s fairly simple. But what if you want to explictly set the Label(MasterLabel) of the the CMDT type record?
Well, you can and cannot.
Note: You cannot specify both DeveloperName and Label at the same time.
Let’s first discuss the ‘ you can’ part.
If the Label values in your CSV are simple, for example:
– Single Space separated words,
– No consecutive underscores or hyphens, or other special characters,
– Doesn’t contain characters from other languages.
etc.(there can be other possible combinations)
Then you can set your Label column as the the –namecolumn because the command auto generates a valid DeveloperName.
For example, if label of your CMDT record is ‘Dummy Record Label’, then the autogenerated DeveloperName for your CMDT record will be: Dummy_Record_Label
And you’ll be able to succesfully deploy this CMDT record.
Now, let’s discuss when can you not.
If your Label value contains:
– Consecutive spaces, underscores or hyphens, or other special characters.
– Characters from other languages.
– Combinations like ‘- ‘(hyphen and space) or ‘ -‘.
etc.(there can be other possiblities)
In this case, if you set the Label as the –namecolumn, the command would still auto generate a DeveloperName but it will be invalid.
For example, if your label is ‘Dummy–Record- Label’, then the autogenerated DeveloperName for your CMDT record will be: Dummy_-Record_ Label
And if you try to deploy this CMDT record, you will get an error.
So you’ll have to manually rename the file name(DeveloperName) of the CMDT record and then deploy it.
It’s currently a limitation and hopefully we’ll see some updates in future.
So much hassle, no? That’s why I built this: https://forcepanda.wordpress.com/2021/02/23/flow-screen-component-create-custom-metadata-type-records-using-csv-and-flow/
I hope this is helpful! I’ll catch you in the next one! ✌
Thank you for being an awesome reader! Subscribe to ForcePanda for receiving all the latest updates straight to your inbox. 🙂
Hi Panda! This article was super helpful for me, thank you!
I’m working on uploading a bunch of metadata for my org and I’m having an issue, maybe you can help?
In your example, the command line auto-generated the underscores in your Label. From ‘Dummy Record Label’ to ‘Dummy_Record_Label’. Mine is only adding an underscore to the first space, like this ‘Dummy_Record Label’, which will fail when I try to deploy. Any ideas why this is happening and how I can fix it? Thank you!!
LikeLike
That’s weird. Normally I would expect it be ‘Dummy_Record_Label’. It could be some issue with SF CLI. You can try raising an issue on the Github for this.
LikeLike
Will do. Thank you!
LikeLike
Repo: https://github.com/forcedotcom/cli
LikeLike
Hi, I am getting this error
ERROR running force:cmdt:record:insert: Cannot read properties of undefined (reading ‘replace’)
Many forums say to update the version of SFDX but I already did it and the same problem continues.
LikeLike
Hi,
Is there a way to upload Metadata Relationship IDs as well where the metadata are related?
LikeLike