2. Introduction
Bohdan Dovhan
Senior Salesforce Developer
Salesforce Development Team Lead
Salesforce Certified Platform Developer I
Salesforce Certified Platform Developer II
Salesforce Certified Platform App Builder
8 years of Development experience
5 years of Development on Salesforce platform
3. What is SFDX?
SFDX is both a new set of tools and new features affecting development lifecycle.
SFDX helps to shift source of truth from production org to VCS repository.
SFDX is an opportunity to shift the source of truth management and org
development lifecycle [1]
SFDX is most useful for team members using CLI or IDE
4. Development Styles
Org based development vs. source driven development.
In a traditional SF Dev Lifecycle, application builders use sandboxes to create and
test changes. Source of truth is either production or any sandbox containing
most recent version of code and customization.
With Salesforce DX, you might use source driven development using latest
versions from a centralized source control system like GIT or SVN.
5. SFDX concepts
SFDX CLI – command line interface tools, for executing commands like
> sfdx force:doc:commands:list
Unlocked packages are intended to provide a repeatable, scriptable and
trackable vehicle for introducing and managing change in your orgs.
Scratch Org is a temporary organization created from DevHub
DevHub feature can be enabled on production or tried in SFDX Trial Org [2]
9. SFDX CLI is SF CLI
Using SFDX CLI you can use both DX features and standard features.
For example, you can run a SOQL query using CLI
> sfdx force:data:soql:query –q “select id, name from account limit 10” –u production
However, force:source:pull and force:source:push work only with scratch
orgs.
You can create a scratch org by force:org:create command only from DevHub
org – production or trial organization with DevHub feature turned on
Read Release Notes [3] !
They have added ability to write custom plugins if you know Node.js
10. Authentication
To authenticate into a non-scratch org execute command
> sfdx force:auth:web:login -a alias
This would open a new browser tab where you can login, OAuth credentials will be
stored on local machine.
Find list of custom object on org
> sfdx force:schema:sobject:list -c custom -u alias
Create a record
> sfdx force:data:record:create -s Account -v "Name=Test" –u alias
You can also run Apex tests, retrieve metadata, deploy metadata, create users,
assign permission set etc.
CLI uses Tooling API and Metadata API under hood
11. DX Project creation
To create a folder with SFDX project
> sfdx force:project:create -n MyNewProject
This would create MyNewProject subfolder in the current folder with SFDX project
structure.
Common files and folder of SFDX project include
sfdx-project.json – contains default project settings
config/project-scratch-def.json – controls shape of scratch org creation
.forceignore – controls source files to omit for push, pull and status commands [4]
force-app/main/default/ - contains source code and configuration metadata
12. Project settings file
Default content of project settings file sfdx-project.json contains [4]
{
"packageDirectories": [ //source element folder
{
"path": "force-app",
"default": true
}
],
"namespace": "", //possible configuration of namespace
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "42.0“ // default version of created classes, components etc.
}
13. Scratch org config
Default content of scratch org definition configuration file config/project-scratch-def.json
contains [6]
{
"orgName": "rwinkelmeyer Company",
"edition": "Developer",
"orgPreferences" : {
"enabled": ["S1DesktopEnabled"]
}
}
To create a scratch org with given settings
> sfdx force:org:create -f config/project-scratch-def.json -a MyScratchOrg.
14. Scratch org config
Disable Lightning session cache in scratch org
{
"orgName": "rwinkelmeyer Company",
"edition": "Developer",
"orgPreferences" : {
"enabled": ["S1DesktopEnabled"],
"disabled": ["S1EncryptedStoragePref2"]
}
}
To create a scratch org with given settings
> sfdx force:org:create -f config/project-scratch-def.json -a MyScratchOrg.
15. Global or local config
Find configuration setting by using command
> sfdx force:config:list
Set local setting by command
> sfdx force:config:set
Set global setting by command
> sfdx force:config:set -g
Global settings are stored in “.sfdx” folder under your user account, while local
settings are stored in “.sfdx” folder in the project root folder.
Global settings apply to all projects, local settings apply to project where they are
defined.
It is commonly suggested to add “.sfdx” folder to git ignore
16. Config list output
Output example of command sfdx force:config:list
=== Config
NAME VALUE LOCATION
───────────────────── ───────────── ────────
defaultdevhubusername DevHub Global
defaultusername GeoAppScratch Local
17. SFDX Source Control
Three commands to work with sources, similar to git commands
To push local source files to scratch org, use force:source:push command
To pull source files from scratch org to local folder, use force:source:pull
To determine source status, use force:source:status command
However, force:source:pull and force:source:push work only with scratch
orgs.
Once you run force:source:status command, it’ll detect which files have changed
(from a Salesforce DX perspective) since your last pull or push. This is
extremely helpful for detecting remotely changed files, especially when you
have changed the same file locally. The pull and push commands provide a —
forceoverwrite flag which you can then use to enforce (as the name says) an
overwrite for a changed file.
18. Development Cycle
1. Create a new scratch org and push existing code to it (or start with a new
project from scratch).
2. Develop live in the scratch org and test changes.
3. Pull the changes down to the local machine.
4. Change some components locally.
5. Push the components and validate the work in the org.
6. Commit the changes to git (never forget that scratch orgs expire, so make sure
your code is in version control).
19. CI with DX Config
1. Install Salesforce CLI on the CI host.
2. Obtain or create a digital certificate to secure your connection to the Salesforce
org. (self-signed certificates work OK)
3. Create and configure a Connected App in your Dev Hub org.
4. Authorize your Salesforce CI user to access this Connected App.
5. Configure a Salesforce JWT (JSON Web Tokens) authentication flow. This
allows the CI host to securely perform headless (without human interactivity)
operations on your Dev Hub org.
Trailhead module to try CI with DX using Travis [7]
20. CI with DX
What can or should be done with DX CI?
1. Create a fresh scratch org for test run
2. Run Apex Tests sfdx force:apex:test:run -w 10 -c -r human
3. Run Lightning Lint sfdx force:lightning:lint
./path/to/lightning/components/
4. Run Lightning Testing Service [8]
a) Install LTS: sfdx force:lightning:test:install
b) Run: sfdx force:lightning:test:run -a jasmineTests.app
5. Clean up test environment
21. CI with DX
What can or should be done with DX CI?
> sfdx force:lightning:lint force-app/main/default/aura —verbose —exit && /
> sfdx force:org:create -w 10 -s -f config/project-scratch-def.json -a ciorg && /
> sfdx force:source:push && /
> sfdx force:apex:test:run -c -r human -w 10
> sfdx force:org:delete -u ciorg -p
22. CD with DX
DX Continuous delivery options
1. Unlocked packages (second generation packages)
2. Managed packages
3. Metadata API
Create unlocked package version
Deprecated sfdx force:package2:version:create
Use: sfdx force:package:version:create
Update unlocked package version
Deprecated sfdx force:package2:version:update
Use: sfdx force:package:version:update
Install sfdx force:package:install
24. Data Tree Migration
Export data tree data
sfdx force:data:tree:export -q "SELECT Name, Location__Latitude__s,
Location__Longitude__s FROM Account WHERE Location__Latitude__s != NULL AND
Location__Longitude__s != NULL" -d ./data
Import data tree data
sfdx force:data:tree:import --plan data/sample-data-plan.json
SFDX allows to migrate both metadata and data
However, not too much of data, it is not complete replacement for
DataLoader [9]
25. Apex class creation
SFDX CLI has commands to create empty apex class, trigger, visualforce page,
component and lightning component.
sfdx force:apex:class:create -n DebugClass -d classes
sfdx force:apex:class:create -n CustomException -d classes -t ApexException
sfdx force:apex:class:create -n TestDebug -d classes -t ApexUnitTest
sfdx force:apex:class:create -n EmailService -d classes -t InboundEmailService
sfdx force:apex:trigger:create -n AccTrigger -s Account -e 'before insert, after update'
sfdx force:visualforce:page:create -n Page -l Label -d pages
sfdx force:visualforce:component:create -n comp -l compLabel -d components
sfdx force:lightning:app:create create a Lightning app
sfdx force:lightning:component:create create a Lightning component
sfdx force:lightning:event:create create a Lightning event
sfdx force:lightning:interface:create create a Lightning interface
sfdx force:lightning:test:create create a Lightning test
27. Pecularities
SFDX CLI has commands to create empty apex class, trigger, visualforce page,
component and lightning component.
However, all other components like SObjects, Fields, Tabs should be created
manually and then pulled from scratch organization
Each custom object field is a separate file. Each element in zipped static resource
is a separate file.
No destructiveChanges.xml is needed, just delete the source file, push and it will
be deleted from scratch organization
28. Limits
How many activetotal scratch orgs can I have in my edition?
EE – 40, UE, PerfE – 100, Trial – 20 active scratch orgs.
EE – 80, UE, PerfE – 200, Trial – 40 daily scratch orgs allocations.
Edition Active Scratch
Org Allocation
Daily Scratch
Org Allocation
Enterprise
Edition
40 80
Unlimited
Edition
100 200
Performance
Edition
100 200
Dev Hub trial 20 40
29. Limits
How many active scratch orgs do I
currently have?
Execute command
sfdx force:limits:api:display
-u DevHub
30. Source conversion
How do I convert my existing source to DX?
First, you need to create a project
sfdx force:project:create -n super_awesome_dx_project
Place a folder with old source in project folder, then execute commands
sfdx force:mdapi:convert -r mdapipackage/
I have a DX project but I want to use ANT Migration Tool. How do I convert DX project
back to ANT migration Tool format?
Easy. Just execute commands
mkdir mdapioutput
sfdx force:source:convert -d mdapioutput/
31. ANT Tasks in DX
Can I deploy ant project with DX?
Yes. Definitely, just run a command
sfdx force:mdapi:deploy -d mdapioutput/ -w 100
You might want to specify alias for target deployment organization with -u flag.
How do I perform ANT Retrieve task with DX?
Retrieve package
sfdx force:mdapi:retrieve -s -r ./mdapipackage -p DreamInvest -u org -w 10
Retrieve unpackaged source
sfdx force:mdapi:retrieve -k package.xml -r testRetrieve -u org
32. Licenses support DX
After you’ve enabled Dev Hub capabilities in an org, you’ll need to create user
records for any members of your team who you want to allow to use the Dev
Hub functionality, if they aren’t already Salesforce users. Three types of
licenses work for Salesforce DX users:
•
Salesforce,
•
Salesforce Platform
•
and the new Salesforce Limited Access license.
33. Permissions needed
To give full access to the Dev Hub org, the permission set must contain these
permissions.
•
Object Settings > Scratch Org Info > Read, Create, and Delete
•
Object Settings > Active Scratch Org > Read and Delete
•
Object Settings > Namespace Registry > Read, Create, and Delete
To work with second-generation packages in the Dev Hub org, the permission set
must also contain:
•
System Permissions > Create and Update Second-Generation Packages