Salesforce DX Absolute Beginner ( Part 4 Of 7 )

In this fourth part of the salesforce DX series, I am going to show how to set up the Salesforce DX from the existing source base by creating the temporary package.  If you are already a Salesforce developer or ISV, you likely have the existing source in a managed package in your packaging org or some application source in your sandbox or production org. Before you begin using Salesforce DX, retrieve the existing source and convert it to the Salesforce DX project format.When you convert existing Metadata API source to Salesforce DX project format, we create an XML file for each bit. All files that contain XML markup now have an .xml extension. You can then look at your source files using an XML editor. To sync your local projects and scratch orgs, Salesforce DX projects use a particular directory structure for custom objects, custom object translations, Lightning components, and documents.

1. Retrieve the Metadata

Create an unmanaged package in your existing org, and include all the component you want to move to your Salesforce DX project. You can do it by login into the salesforce dev or sandbox.

 2. Login into Dev Hub from CLI 

Run the force:auth:web:login CLI command. If you are authorizing a Dev Hub org, use the –setdefaultdevhubusername parameter if you want the Dev Hub org to be the default for commands that accept the –targetdevhubusername parameter.

sfdx force:auth:web:login --setdefaultdevhubusername --setalias my-sfdchub-org

3. Create a DX Project

A Salesforce DX project has a specific structure and a configuration file that identifies the directory as a Salesforce DX project.Use the force:project:create command to create a skeleton project structure for your Salesforce DX project.

sfdx force:project:create --projectname DXMigration
cd DXMigration

4. Login into Salesforce Source Org 

Now you need to login into the salesforce source org from where you wanted to fetch metadata into the salesforce scratch org. Use below command to connect to the developer account or sandbox.

sfdx force:auth:web:login -a my-dev-org

5. Export The Package 

Export the unmanaged package metadata in a temporary directory. Type the following commands in the root folder of your Salesforce DX project:

mkDir temp 
sfdx force:mdapi:retrieve -s -r ./temp -u my-dev-org -p DXExport	

Unzip the file and optionally delete the file

 6. Convert Source code to Salesforce  DX 

Convert the source code to the Salesforce DX project structure by running the following commands.

cd ..
sfdx force:mdapi:convert -r ./temp


7. Create a Scratch Org for Development

       You can create scratch orgs for different functions, such as for feature development, for development of packages that contain a namespace, or for user acceptance testing.Run the following commands to create a scratch org .

sfdx force:org:create -f config/project-scratch-def.json --setdefaultusername --setalias my-dx-scratchorg
 8.Push Metadata into the scratch org
You can push the extracted metadata into the scratch org by running the below command.
sfdx force:source:push

9: Open scratch org

You can open scratch org by running following command.

sfdx force:org:open

10. Pull Changes from Scratch Org, if any needed 

sfdx force:source:pull

11. Implement the new features

Now if you wanted to make any changes you can do in the scratch org . In this example, you can create an apex class by running this command.

sfdx force:apex:class:create  --classname DemoClass --template DefaultApexClass --outputdir force-app/main/default/classes/
push changes to scratch org again after development by running the following command.
sfdx force:source:push

Quick Recap of part 4  going to part 5
  • Salesforce DX  Project setup
  • Converting metadata into DX specific format
  • Making changes and push to scratch org