Salesforce DX is put together a set of tools, features and APIs to make it easier for Salesforce teams to adopt modern DevOps. see it like a toolkit to strengthen better adoption of devops.
It encourages developers to adopt best practices for software development stacks, like using version control, and automating more parts of the release process, scripts etc.
Remember in itself DX may not necessarily perform all the devops processes as standalone but it brings on table all the blocks you need to connect with other elements — jenkins for scheduling or automation, github or bitbucket for source control,
SFDX comprises of set of tools —
- SF CLI :(push or pull metadata to & from orgs) , metadata ?: Database Schema; user interface; automation (workflows, process builders and flows),reports, code
2. Vs code: A new eclipse based IDE
3. Scratch orgs : to facilitae your tests, & mimic functioanlity (as an alternate to Dev sandbox) designed for your day to day development
4. 2nd gen Packaging : Club your functionalities & components together and make it ready to deploy, more on this later. (this is one of favorites: see this as individually deployable functionality rather than seeing your deployment as just package.xml built with chunks of metadata — too old concept) plus brings other benefits when we do market (regional)rollouts.
Ingredients (Tools) you need (1,2,3, 4 and you are set!) :
- Enable Dev hub (it is home to all your scratch orgs, central org, one place to manage your sc. orgs)— login to your Salesforce development or test environment and search for Dev hub in the setup & switch ON the plug
- Next, Install the Command line CLI — win 64; https://sfdc.co/sfdx_cli_win64 Or another alternative is to download the Source Tree (once installed type sfdx to guarntee SFDx installation)
- Last element- Install VS code & once installed , go to extensions tab and add the Salesforce extension pack, https://code.visualstudio.com/Download
- Install Git from https://git-scm.com/downloads.
See how you would visualize this for developers
Way of working
Develop on Local source → Scratch Org →Source Control (e.g. github)→ Sandbox → production
1. Software developer works in local project repo , develops the functionality (code & config) and push to scratch org
2. In the Scratch or, it is tested & pulled back to local repo.
3. Next once tested, then finally the code goes to source Control repositry (this is the source of *TRUTH)
- Person A: starts his /her work on user story A — 1. create a new feature branch from your source control (bitbucket / github), 2. create new scratch org and 3. pull your metadata to your local repo (GIT clone ) this gives you all you need as a starting point.
- Develop his /her feature (functionality) in the local repositry for any customization; for any config. level change make it in the scratch org, pull (sync back)the metadata from scratch org to the local repo, finally what you have a is a local repo containing everthing.
- When the user story is complete, push your local repo to scratch org & test the funtionality.
- Lastly, merge your local repo to Parent (master / develop) branch and delete your scratch org.
Thumb rule to remember is this process:
Push your local source and metadata to a scratch org.
Pull any changes you make in the scratch org back to your local project.
Sync this project with your source control repo.
Lets kickstart with a project (A →:F)
A. First Create a SFDX project
- Go to your commandline (window + R= keyboard shortcut) and run this command → sfdx force:project:create — projectname sfdx-Mo
- cd sfdx-Mo (this is to change project directory)
- Create assets folder manually: create package.xml and use the command below to retrieve content from the org to assets folder → sfdx force:source:retrieve — manifest assets/package.xml — targetusername DevHub — wait 10
- Export sample data from org → sfdx force:data:tree:export — targetusername DevHub — outputdir assets/data — query “SELECT Id, Name, Email__c, Phone__c, Mobile_Phone__c, Title__c, Zip__c FROM Properties__r ) FROM Broker__c”
Once project is created we need to:
B. Add our Salesforce DX project to our GIT source Control, for this :
+++++++ Move on to the GITHUB side & configure your GIT identity :
- Create a GitHub repository at https://github.com/new, name it & next open cmd prompt type below
- git config — global user.name “EMEAMo…..”
- git config — global user.email “mohitchha….”
+++++++ Come back to your local project working repositry & Create a local git Repo, type this →
- git init
++++++++ Commit Your Files to Git (version history) →
git add .
git commit — message “Initial commit of myproject metadata” (this commits the added files as a snapshot to the project’s version history)
++++++++ Push Your Files to GitHub →
- git remote add origin https://github.com/EMEAMo../sfdx-project.git
- git push origin master
- If you need to update remote repositry use; → git remote set-url origin https://github.com/EMEAMo../sfdx-project.git
C. Another Colleague starts in & joins the project, Here comes dev expert 2 Maria (see how she collaborate)
- She starts by cloning the parent (master) branch to its local repositry
git clone https://github.com/EMEAMo../sfdx-project.git sfdx-maria
- Then She creates a new feature branch →git checkout -b feature-maria-util→ Then she creates some apex classes (in class folder)
- Create & Push metadata to scratch org → sfdx force:org:create — setdefaultusername — setalias sfdx-maria — definitionfile config/project-scratch-def.json
- Sfdx force:source:push (to deploy)
- Export some sample data from Devhub → sfdx force:data:tree:export — targetusername DevHub — outputdir assets/data — query “SELECT Id, Name, Email__c, Phone__c, Mobile_Phone__c, , Location__Longitude__s, Status__c, Tags__c, Thumbnail__c, Title__c, Zip__c FROM Properties__r ) FROM Broker__c”
- Assign the permission set to the dreamhouse app → sfdx force:user:permset:assign — permsetname DreamHouse
- Import sample data → sfdx force:data:tree:import -f assets/data/Broker__c-Property__c.json
- Run Tests to Validate Changes →sfdx force:apex:test:run — codecoverage — resultformat human — wait 2
- Pull Metadata from Scratch Org
- Lastly, add the changes to the source control
Add all modified files in the project to be tracked by Git
- git add .
- git commit — message “Added new apex class- util.class”
- git push -u origin feature-maria-util
Conclusion: with this her code changes goes to her new respective feature branch, Next she needs to open pull request to merge changes into the master branch.
D. Familiarize yourself with the SFDX Commands:
- sfdx force:auth:web:login -d -a DevHub (to authorize against the Devhub)
- sfdx force:org:open -u DevHub (to open your devhub)
- sfdx force:org:list (shows all the scratch orgs& org list)
- Create a Salesforce DX project: sfdx force:project:create -n MyFirstProject File elements woth checking are: sfdx-project.json; config/project-scratch-def.json & force-app; — cd MyFirstProject (change directory)
- sfdx force:org:create -s -f config/project-scratch-def.json -a MyScratchOrg( this is to create a scratch org)
- Create data in scratch org: sfdx force:data:record:create -s Account -v “Name=’Marriott Marquis’ BillingStreet=’780 Mission St’ BillingCity=’San Francisco’ BillingState=’CA’ BillingPostalCode=’94103' Phone=’(415) 896–1600' Website=’www.marriott.com’”
- Export data from Salesforce: sfdx force:data:tree:export -q “SELECT Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, Phone, Website FROM Account WHERE BillingStreet != NULL AND BillingCity != NULL and BillingState != NULL” -d ./data
- Import .json file to any org: sfdx force:data:tree:import — sobjecttreefiles data/Account.json
- Open the scratch org: sfdx force:org:open -u test-au9akadu7o6z@exam.. OR -u MyScratchOrg (sc. org alias)
- Create an apex class: sfdx force:apex:class:create -n AccountSearchController -d force-app/main/default/classes
- sfdx force:source:push -u MyScratchOrg (against the scratch org)
- sfdx force:source:pull
- sfdx force:org:delete -u MyScratchOrg
- To Convert Source to metadata api → This converts sfdx source format to metadata format which creates also package.xml
sfdx force:source:convert -d metadata
- To Convert metadata from metadata api format to source format, SFDX one: → sfdx force:mdapi:deploy
E. Here are some of the powerful tasks that can be used directly through the command line:
- Export (SOQL) / Import (CSV) data
- Bulk API Delete/Upserts
- Full CRUD on individual records
- Run Anonymous Apex and Apex Tests
- Deploy/Retrieve/List Metadata with Metadata API
- List and Open in a browser a connected Salesforce instance
- Create/Delete scratch orgs
- Create/Version/Install packages
- Create/Display Users and Assign permission sets to them
- Convert between SFDX Component Structure and Metadata API Structure
F. Tools available today for moving metadata (deployment)
- Change sets
- Ant migration tool
- SFDX and Source related commands
Please Note: For few elements in the article, references are taken from online articles & docs, incase you find similarities , please dont be surprised :)
For more learning, please refer to trailheads- SFDX & GIT :
https://trailhead.salesforce.com/content/learn/modules/sfdx_app_dev/sfdx_app_dev_deploy && https://trailhead.salesforce.com/content/learn/projects/develop-app-with-salesforce-cli-and-source-control/set-up-your-environment?trail_id=move-to-a-continuous-integration-development &&
A useful utlity to see what DX supports and comparsion with others: https://mdcoverage.secure.force.com/docs/metadata-coverage/50