[SalesForce] Usage of cli command – sfdx force:source:convert

I used force:source:retrieve to retrieve the given list of components (classes, workflows etc) and committed the files to git. Now I want to deploy these components to higher environments like QA and Prod from Git.

force:source:deploy – I can't use this since it doesn't have the options like validate only or run local tests etc.. So I am planning to use force:source:convert to convert into mdapi format but even this does not have an option to convert a list of metadata, it converts all the source code in the root folder.

What are the options to convert given list of metadata (package.xml/list of files) to mdapi format and then deploy to sandbox/prod from git.

Best Answer

You can use .forceignore file to specify the files which you want to exclude while converting to metadata format.

From How to Exclude Source When Syncing or Converting documentation:

The .forceignore file excludes files when running the source commands: force:source:convert, force:source:push, force:source:pull, and force:source:status.

Let's say you have a DX project structure as below:

myproject/main/default/classes/MyFirstClass.cls
myproject/main/default/classes/MyFirstClass.cls-meta.xml
myproject/main/default/classes/MySecondClass.cls
myproject/main/default/classes/MySecondClass.cls-meta.xml

And that you want to exclude MySecondClass.cls from being converted into a metadata format. You can then include this file in the .forceignore file so that the output directory does not contain this entry. The entry in the .forceignore file should be then as below:

myproject/main/default/classes/MySecondClass.cls
myproject/main/default/classes/MySecondClass.cls-meta.xml

When you run the force:source:convert, the generated output metadata format directory will not contain MySecondClass* files or any entry in the generated package.xml.

Related Topic