Are you ready to move to Salesforce DX, but your source code is currently in a Developer Edition (DE) or Sandbox org? In this blog post, I describe the easy steps to convert existing source code to a Salesforce DX project.
I recently converted the latest version of the DreamHouse sample application to a Salesforce DX project, and I thought it would be helpful to document the process so you can use the same steps to convert your own projects to Salesforce DX. The end result for the the DreamHouse app is available in this repository, but the steps below are applicable to any project.
sfdx force:auth:web:login -d -a myhuborg
sfdx force:project:create -n myproject cd myproject
sfdx force:auth:web:login -a mydevorg
mkdir temp sfdx force:mdapi:retrieve -s -r ./temp -u mydevorg -p mypackage
“mypackage” is the name of the unmanaged package you created in step 1. Afer executing this command, a file named unpackaged.zip is created in the temp directory.
unzip ./temp/unpackaged.zip -d ./temp/
sfdx force:mdapi:convert -r ./temp
You can delete the temp directory at this point.
sfdx force:org:create -s -f config/project-scratch-def.json -a myscratchorg
If you get an expired access/refresh token message, authenticate with your hub org again (step 2 above).
sfdx force:source:push
sfdx force:org:open
For example, here are the permissions I added for the DreamHouse app:
sfdx force:source:pull
The Salesforce DX CLI also makes it easy to export sample data from your developer org and import it in your scratch orgs. It even preserves master-details relationships between records. As an example, here is how I did it for the DreamHouse app:
To export the data from my DE org, I ran the following command from the root folder of my Salesforce DX project:
sfdx force:data:tree:export -q "\ SELECT Id, \ Name, \ Title__c, \ Phone__c, \ Mobile_Phone__c, \ Email__c, Picture__c, \ (SELECT Id, \ Name, \ Address__c, \ City__c, \ State__c, \ Zip__c, \ Price__c, \ Title__c, \ Beds__c, \ Baths__c, \ Location__Longitude__s, \ Location__Latitude__s, \ Picture__c, \ Thumbnail__c, \ Description__c \ FROM Properties__r) \ FROM Broker__c" \ -u mydevorg --outputdir ./data --plan
After executing this command, the sample data files (Broker__cs.json, Property__cs.json, and Broker__c-Property__c-plan.json) are available in the data subfolder of my project which means that they will be part of the project when I put it under version control (step 3 below).
To import the data in my scratch org, I run the following command:
sfdx force:data:tree:import -u myscratchorg \ --plan ./data/Broker__c-Property__c-plan.json
We are done converting the project and transferring data. It’s time to put the project under version control. For example, using Github:
git init git add . -A git commit -m 'first commit' git remote add origin https://github.com/yourname/your-repo.git git push origin master
That’s it. It’s now easier than ever for other developers to work with you on the project. Using the migrated DreamHouse project as an example, all they have to do is:
git clone https://github.com/dreamhouseapp/dreamhouse-sfdx cd dreamhouse-sfdx
sfdx force:org:create -s -f config/project-scratch-def.json -a myscratchorg
sfdx force:source:push
sfdx force:user:permset:assign -n dreamhouse
sfdx force:data:tree:import -u myscratchorg \ --plan ./data/Broker__c-Property__c-plan.json
sfdx force:org:open
Salesforce DX enables modern development workflows on the Salesforce platform. It makes team development, version control, and continuous integration easier than ever. Migrate your existing projects to Salesforce DX today using the easy steps described in this post and watch the pull requests come in!