A lot of our projects are in existing orgs, so spinning up a scratch org and starting from scratch isn't really an option.
There is an option to migrate existing sandboxes to Salesforce DX using an unmanaged package, but holy moly. When you add a few fields to the package, it brings in a TON of dependencies! I wanted the ability to cherry pick the dependencies I wanted to store in source control. Reason being? I only wanted the work that our team is in charge of developing instead of the client's complete metadata.
Enter change sets.
Turns out you can generate a package.xml through a change set (similar to what you would do through Eclipse or other IDEs).
Here's the basic steps I took to convert a change set to a DX project:
- Create a change set in a sandbox.
- Populate the change set with any necessary metadata.
- Validate the change set against, say, production.
- Rinse lather repeat until you're able to validate the change set successfully.
- In workbench (workbench.developerforce.com), click on the Migration tab, then click Retrieve.
- In "Package Names" specify the name of your change set. You might have to delete / remove old change sets, otherwise, you'll get an error that multiple packages exist.
- When the retrieve is successful, then click "Download zip file"
- This zip file will have a root directory of "unpackaged", inside of which the package.xml file is stored.
- Create a directory for DX project, and add the sfdx-project.json file, along with the config/project-scratch-def.json files, etc. If you're connecting to a sandbox environment, you'll want to make sure project-scratch-def.json points to test.salesforce.com instead of login.salesforce.com
- If you haven't already, authenticate with your sandbox (this will pop up an oauth screen in your web browser and ask for permission so that Salesforce DX can connect to it):
sfdx force:auth:web:login -a HeyThisIsMySandboxAliasAndYouCanChangeIt
- Copy the package.xml from step 7/8 into the directory.
- Issue the following command (It will retrieve the source from your sandbox, using the package.xml and generate a zip file in the root level of your project directory):
sfdx force:mdapi:retrieve --retrievetargetdir ./ -k package.xml -u HeyThisIsMySandboxAliasAndYouCanChangeIt
- Make a temp directory for the contents of the zip file:
- Unzip the zip file into a temp directory:
unzip unpackaged.zip -d ./temp/
- Change directories into temp. If you see a folder called "unpackaged", rename this to "src". Because.. reasons?
cd temp | mv unpackaged src
- Now, convert the source from the "metadata api format" to the "dx format"
sfdx force:mdapi:convert -r ./temp
Boom, now you can either work with it in a scratch org, and/or commit it to source control.