Table of Content
- What are Scratch Orgs
- Setting up SalesforceDX
- Authorize Salesforce Instance to be used with SFDX
- Create Scratch Org
- Move code to and from Scratch Org
- List of existing Orgs in SFDX
- Open Salesforce Org from SFDX
- Run Test Classes
- Setup Log Level
- Create Skeleton Workspace
- Generate Password for Scratch Org
- Enable Person Account
- Deploy and Retrieve metadata from Sandbox – Old Source Format
- Deploy and Retrieve metadata from Sandbox – New Source Format
- Set default username or default dev hub
- package.xml to SFDX format
- Sample Bash script to retrieve source code from developer or sandbox and convert to SFDX format
- Change SFDX API version
If we already have Salesforce Metadata API, Force.com IDE and other tools then why do we need one more tool like Salesforce DX ?
In tools like Changeset, Metadata API or Force.com IDE, source of truth is Sandbox. Although we can setup process and continuous integration (CI) to use some source code management (SCM) like Git or SVN. However these kind of setup takes time, expertise and lot of effort.
Salesforce DX not only solves above problem but
- Ability to consider SCM as source of truth
- Use any favorite IDE or any SCM
- Powerful CLI to help minimizing complexity of setting up CI
- Updated Force.com IDE to support Salesforce DX if you are not comfortable with CLI
- and most important, spin off Scratch Orgs within minutes through script to quickly work on POC or package based development
What are scratch Orgs ?
Salesforce DX can be enabled for any Salesforce instance and they are known as Developer Hub. One Developer Hub can have multiple Scratch Orgs. Scratch Orgs are temporary Salesforce org which can be created quickly and metadata can be deployed from SCM. These kind of Orgs can be used by developers to perform quick proof of concept or build and test packages. Once package is build and saved back on SCM, scratch org can be destroyed easily.
If we already have developer orgs or sandboxes, why do we need scratch orgs ?
Because of compliance of most of clients, we cannot move their code to developer org to perform some POC or package development, so developer org is out of questions anyway.
Now, lets focus on Sandboxes. Use of sandboxes are mostly for end to end testing, integration testing, UAT or training. Sandboxes mostly represents either replica of production or future state of production which might be undergoing through User acceptance testing (UAT). When we refresh Sandbox, we don’t get options on which metadata needs to be copied. Assume situation, there is defect on production and we need to go back to time and check how system was behaving previously. Typical rollback scenario for sandboxes. We might have metadata stored somewhere, but we need to perform many iterations of destructive.xml to delete components from Sandbox. In this case, we can quickly spin off scratch org from source code and perform analysis of historical code.
This is just one example, there are many scenario and specifically for appexchange development companies.
I think, we had lots of talk and ready to roll now.
Setting up Salesforce DX
Install Heroku CLI and then run below command
heroku plugins:install salesforcedx
Other way to install SalesforceDX during pilot is to installer from here (Currently this URL is working however its location is not official). Once downloaded, install it. After installation make sure git command is working from command line.
In Salesforce DX, source of truth is source control. so we need a repository for demo. For this blog post, let’s consider this repository. Run below command to clone this repository
git clone https://github.com/forcedotcom/sfdx-simple cd sfdx-simple
Authorize Salesforce DX to login to Developer Hub Org
Below command will set org as default Developer Hub Org and will set its alias as my-devhub-org. It will open Salesforce login page in default browser for OAuth flow, where we need to login to Developer Hub org and authorize Salesforce DX.
sfdx force:auth:web:login --setdefaultdevhubusername --setalias my-devhub-org
Creating Scratch Org
To create a scratch org in Salesforce DX, we need to have workspace-scratch-def.json . Best place is to place it in config folder. This file already exists in Git repository we cloned initial however we would need to update it with our FirstName, lastName, Email and Org preferences. All supported preferences are listed here in Metadata documentation.
You can find official documentation on sample scratch org definition file and possible configuration values and features.
Below is sample of file
workspace-scratch-def.json
{ "Company": "Shivasoft", "Country": "US", "LastName": "Zaa", "Email": "jitendra.Zaa@shivasoft.in", "Edition": "Developer", "OrgPreferences" : { "S1DesktopEnabled" : true } }
Edition of Org can be Professional, developer or enterprise.
Once workspace-scratch-def.json created in config folder, run below command to create a scratch org. New scratch would be named as jitendra2_scratch and it can be configured by passing parameter to –setalias
Note : At a time of writing this post, scratch orgs auto deleted after 7 days however it may change when product goes GA.
sfdx force:org:create --setdefaultusername -f config/workspace-scratch-def.json --setalias jitendra2_scratch
Moving code to Scratch Org
To move code, we need to define sfdx-workspace.json which contains path of source code needed to be pushed. Below is sample file
simple sfdx-workspace.json
{ "PackageDirectories": [ { "Path": "force-app" } ], "Namespace": "", "SourceApiVersion": "39.0" }
sfdx-workspace.json with some more options
{ "Namespace" : "AcmeIncExample", "SfdcLoginUrl" : "https://login.salesforce.com", "SourceApiVersion": "39.0" , "PackageDirectories" : [ { "Path" : "helloWorld", "Default": true }, { "Path" : "unpackaged" }, { "Path" : "utils" } ] }
Run below command to push metadata to default scratch org
sfdx force:source:push or sfdx force:source:push --targetusername my-scratch-org@bar.com
Get metadata changes from Scratch Org or Pull changes
We can also get metadata changes done in scratch Org either from UI or deployment. It will only pull if metadata changes, not the whole Org.
sfdx force:source:pull or sfdx force:source:pull -u org_alias
Getting list of all existing Orgs
Before creating scratch org, you may want to know about the existing orgs and their aliases. We can run below command
sfdx force:org:list
Open Salesforce Org from sfdx
Below command can be used to open Org from sfdx command line
sfdx force:org:open or sfdx force:org:open -u org_alias
Running Test classes using Salesforce DX
To run test classes using Salesforce DX , use below command
sfdx force:apex:test:run
Above command will return job Id, use that ID and run next command for status
sfdx force:apex:test:report -i JOBID_FROM_ABOVE_COMMAND
Setting up log Levels
Salesforce DX logs are generated at USER_Home_DIR/.sfdx/sfdx.log. We can set it either with each command or globally. By default only Error logs are recorded but we can change log level. To set log level with each command, we can use –loglevel DEBUG. To set log level globally we can use
//windows set SFDX_LOG_LEVEL=DEBUG //Or in unix Export SFDX_LOG_LEVEL=DEBUG
Currently below log levels are supported
- ERROR
- WARN
- INFO
- DEBUG
- TRACE
Creating skeleton workspace
We can automatically generate skeleton workspace using CLI which will create folder structure and json files with default value.
sfdx force:workspace:create --workspacename mywork OR sfdx force:workspace:create --workspacename mywork --defaultpackagedir myapp
Above command will create below folder structure

Password for scratch Orgs
When we create a scratch org using sfdx CLI, it does not display password and uses OAuth internally to communicate with scratch org. If we need to login to scratch org from non sfdx CLI, then we would need to generate a password. Below command can be used to generate password.
sfdx force:user:password:generate
Message :
Successfully set the password "26y271a" for user scratchorg1495650731268@shivasoft.in. You can see the password again by running "force:org:describe".
If we want to see password in future, then use below command
force:org:describe
Enable Person Account
Make sure below steps are completed in Salesforce Hub instance before running below sfdx script
- Create Record Type on Account
- Make sure OWD setting for contact is Controlled by Parent.
Below code in project-scratch-def.json file would create a Person Account
{ "orgName": "Person Account Scratch Org", "edition": "Enterprise", "features": ["PersonAccounts"] }
Retrieve and Deploy code in Sandbox using SFDX – Old File Format
This blog post goes in detail, however, below is quick command
Retrieve Metadata from Sandbox
sfdx force:mdapi:retrieve -r ./mdAPIZip -u jzaa1 -k src/package.xml
Deploy Metadata to Sandbox
-- deploy zip file sfdx force:mdapi:deploy -f ../mdAPIZip/unpackaged.zip -u jzaa1 -w 10 -- or deploy traditional file structure sfdx force:mdapi:deploy -d ../mdAPIZip/unpackaged -u jzaa1 -w 10
Retrieve and Deploy code in Sandbox using SFDX – New Source Format
Deploy metadata to Sandbox or production using new source format
sfdx force:source:deploy -p force-app/main/default
Retrieve metadata from Sandbox of Production using new source format for existing components in default folder
sfdx force:source:retrieve -p force-app/main/default
Retrieve metadata from Sandbox / production on basis of package.xml in manifest folder
sfdx force:source:retrieve -x manifest/package.xml
Set Default Username or Default Devhub
Use below command to set default username for current project. For global use -g at end
$ sfdx force:config:set defaultusername=me@my.org defaultdevhubusername=me@myhub.org
$ sfdx force:config:set defaultdevhubusername=me@myhub.org -g
Convert Package.xml to SFDX format
sfdx force:mdapi:convert --rootdir "manifest"
In above command, –rootdir would be replaced by root path declared in sfdx-project.json
Sample Code Snippet to retrieve metadata from Developer or Sandbox Instance and convert to SFDX source format
echo "Retrieve Metadata from Developer instance" echo "Command - sfdx force:mdapi:retrieve -r metadata -u pathtocode -k manifest/package.xml" sfdx force:mdapi:retrieve -r tmp -u pathtocode -k manifest/package.xml echo "Unzip results" echo "Command - unzip -o tmp/unpackaged.zip -d manifest" unzip -o tmp/unpackaged.zip -d manifest echo "delete zipped result retrieved" rm tmp/unpackaged.zip echo "Move unzipped content to folder up" mv manifest/unpackaged/* manifest rm -r manifest/unpackaged echo "Convert Manifest to SFDX format Source" sfdx force:mdapi:convert --rootdir "manifest"
Change default SFDX API
Be default SFDX points to latest API of plugin. If I have SFDX installed for prerelease instances, that means it will not work for my developer or sandboxes because they are still on older API. In these scenarios, we can override SFDX API either globally or current project using below command.
sfdx force:config:set apiVersion=44.0 --global or sfdx force:config:set apiVersion=44.0
Login to Salesforce using Certificate / JWT
sfdx force:auth:jwt:grant -r "https://login.salesforce.com" -a orgalias -f "PathtoKey/server.key" -s -u username@salesforce.com -d -i "ConsumerKey" --json
Remove entry from org list or logout
sfdx force:auth:logout -u <usernamealias> -p
You can also run below command to cleanup all orgs which are expired or deleted
sfdx force:org:list --clean
Install Package / Manage Package or Unlocked Package
sfdx force:package:install -p [packageId] -w 30 -u [scratchOrgName] -k [passwordIfAny]
You can find list of few Salesforce AppExchange here
I am getting the below error while creating scratch org:
sfdx force:org:create –setdefaultusername -f config/workspace-scratch-def.json –setalias amjad_scratch
ERROR running force:org:create: No such column ‘WorkspaceType’ on sobject of type SignupRequest.
Are you part of Pilot ? This feature not GA yet, you would need to have access to Developer hub to create scratch org.
I am getting the below error while running the command “sfdx force:org:create –setdefaultusername -f config/workspace-scratch-def.json –setalias somnath1_scratch”
You do not have access to the [SignupRequest] object
In my developer org setup, i can not find environment hub.Is it enabled only for ISVpartners? How to get the access of Environment hub in my developer Org.
Thanks
Somnath
Glad you lined this up. Long due DX, have been hearing this coming. Wondering, how DX solve problem of connected org and landscape, will that solve or does this add more complexity. Like QA lets say connected to Tableau and O365, so as dev instance linked with Dev(Tableau) and Dev(365) now spinning a template org, while offer new org id which in turn require relinking all integration and variables. What’s your thought on that ?
Not sure if in these scenarios, DX would be of any help or not. Its more on package based deployment. Also, currently it can only be used with scratch orgs and not with sandboxes. So time will tell that can we or not, set these integration related settings automatically.
I am getting error message while running below command
org:create –setdefaultusername -f config/workspace-scratch-def.json –setalias abdul_scratch20170614
ERROR running force:org:create: You do not have access to the [SignupRequest] object.
https://uploads.disquscdn.com/images/27f28c13abd5e02c378867739e93d26df793420c1364639fa54474fa72fc158b.png Can you please let me know the Source directory so that I can update it to BETA version. I’m stuck
I am very exited to use this feature. Is there any release date?? In your demo, mentioned that it will be available soon.
HI,
i have small doubt. is it possible Creating recurring time-based actions in workflow or process builder. i want fire the my workflow rule every day. is it possible or not?
Thanks,
Venkat.
Hi Jitendra,
I am getting below error-
ERROR: You do not have access to the [ScratchOrgInfo] object.
Setup-> Quick fund-> Search ‘Dev Hub’ -> Enable
Hi Jitendra,
I have one query if i enable the Scratch Orgs in DevHub can i use the Developer Sandbox simultaneously or we cannot able to use Developer Sandbox?
Regards,
Venkatesh R
You can use simultaneously both – Scratch Org and Sandbox
Hi Jitendra,
I have a question on the scratch orgs that are created using SFDX. If we need to reproduce an issue by going back to a certain point in time (by getting the code from any SCM tool), & deploying it to a scratch org, it will also need some data setup to be done before performing testing. So my question is is there a way copy data as well while creating the scratch orgs ?
You would first need to create a scratch Org and then run a command to load data using SFDX or CLI dataloader. https://www.jitendrazaa.com/blog/salesforce/import-and-export-records-using-salesforce-dx/
Hello,
I am facing an issue with record type migration to scratch org,what would be the best way to dit can you help me ?
force-app\main\default\objects\Account\compactLayouts\C_CL_Entreprise.compactLayout-meta.xml In field: RecordTypeId – no CustomField named Account.RecordTypeId found
any help would be greatly appreciated
Anil, were you able to resolve this issue? am running into same issue.
Cheers
Adam
I have the same issue still
Hi Jitendra,
I have a query related to sfdx:force push. When ever I try to push the code it always consider the whole code. Somehow its not able to identify the delta. Everytime, I need to create a new scratch Org to resolve this issue. Even after creating new scratch org, some times again it starts with same problem. Is there any I can fix this issue without creating a new scratch org.
Thanks in advance.
Even i need the same. Looks there is no way to send only delta changes to scratch org. pls share your thoughts too.
Thanks Jitendra for the explanation.
I am getting the error “‘sfdx’ is not recognized as an internal or external command, operable program or batch file.” after installing the Salesforce CLI and setting the path environment variable.
Any idea why or how can I resolve this?
Thanks,
Revathy
Hi Jitendra,
I have a question regarding person accounts.
I activated the features and person accounts are available in my scratch org.
But the company field is still required on the Lead object.
In my production org the field is not required and determines if a lead is converted to a person account or a business account.
Is there any way to activate this functionality in scratch orgs as well?
Thank you,
Beatrix
We need to start the development with VS-Code with dev org using version control (Gitlab).
We have number of developers who will be working to complete the tasks.
Which approach should we take, scratch or non-scratch org ? or any other preference.
Please suggest which approach to take.
Hi Jithendra, just played around this DX today. when the scratch org is created, i don’t see the complete meta from source system is coming up. Is it designed in the same fashion? My understanding towards scratch org is, we will get only standard sf meta data into the scratch org but nothing custom meta data we built. If we need some missing meta data in scratch org for our feature implementation in scratch org, then we need to retrieve the required meta data from source org and push it to scratch org. is my understanding correct? I am really excited to use scratch orgs using command based component development . Thank you
Hello sir, i have a sandbox with existing functional component added to it, I want to retrieve whole metadata of a org using sfdx retrieve command . For that I need to have a package.xml. Is there any way to generate the package.xml that consist the entries to fetch all the metadata from my sandbox.
Hi Jeetendra, I am workin as a QA. Have a query, where those jason files should be created for creating the workspace?
Thank you.