DX Complete Guide - Scratch Orgs

8 - Introduction to Scratch Orgs

  • AW: Scratch orgs are the “bread and butter” of Salesforce DX development.
    • Keeps your source of truth local
  • Sandboxes
    • Require time to spin up
    • Contain same configurations as production environment
    • All production users can have access
    • Higher data limits depending on sandbox type
  • Scratch Orgs
    • Can spin up within a few seconds
    • Contain no custom functionality - fresh instance to develop against every time
    • Only a few users can access
    • Limited data use
    • Source Tracking changes - no need for laborious tracking of changes to be added to change sets
      • Keeps repo in sync with scratch org and vice versa

9 - Create a Scratch Org

  • First step in working with Scratch Orgs is to open config folder in the DX Project
    • project-scract-def.json contains scratch org definition
    • Config folder can contain multiple of these files - must end with -scratch-def.json
  • There are many features that can be enabled in the scratch definition file
  • For example, to turn on Product Quantity Scheduling, enableQuantitySchedule:
{
  "orgName": "ryan company",
  "edition": "Developer",
  "features": ["EnableSetPasswordInApi"],
  "settings": {
    "productSettings": {
      "enableQuantitySchedule": true
    }
  }
}
  • The following is the default project scratch definition of my scratch org generated in April, 2021
    • enableS1DesktopEnabled - indicates Lightning Expertience is turned on
    • enableS1EncryptedStoragePref2 - Indicates whether the Salesforce mobile web uses secure and persistent browser caching to improve performance. Default is true.
{
  "orgName": "ryan company",
  "edition": "Developer",
  "features": ["EnableSetPasswordInApi"],
  "settings": {
    "lightningExperienceSettings": {
      "enableS1DesktopEnabled": true
    },
    "mobileSettings": {
      "enableS1EncryptedStoragePref2": false
    }
  }
}
  • Create a scratch org by:
    • Ctrl-Shift-P > SFDX: Create a Default Scratch Org…
    • Click the “No Default Org Set” button at the bottom left
    • Terminal: sfdx force:org:create -a courseScratchOrg -d 30 -f config/project-scratch-def.json
  • Running the terminal command returns the following:

Successfully created scratch org: 00D1100000C8mlrEAB, username: [email protected]

  • Next, set the scratch org to be the default org that all our commands run against. Do this with sfdx force:config:set
    • Terminal: sfdx force:config:set defaultusername=courseScratchOrg
    • This sets the default org in Visual Studio Code, in the bottom left
  • Then, open the scratch org:
    • Terminal: sfdx force:org:open
    • This opens the org in the browser and logs in the user

10 - Source Tracked Metadata

  • Following the configuration above, we are able to begin pushing and pulling metadata to and from the scratch org.
    • After making configurations with the declarative configuration tools of the scratch org, they become visible in the terminal with sfdx force:source:status, which returns the following for this example
=== Source Status
STATE       FULL NAME                                        TYPE          PROJECT PATH
──────────  ───────────────────────────────────────────────  ────────────  ────────────
Remote Add  Course_Test_Object__c.Test_Text_Field__c         CustomField
Remote Add  Admin                                            Profile
Remote Add  Custom: Sales Profile                            Profile
Remote Add  Custom: Marketing Profile                        Profile
Remote Add  Custom: Support Profile                          Profile
Remote Add  Course_Test_Object__c-Course Test Object Layout  Layout
Remote Add  Course_Test_Object__c                            CustomObject
  • Download the changes to the VS Code project with sfdx force:source:pull, which returned the following for this example
=== Pulled Source
STATE  FULL NAME                                        TYPE          PROJECT PATH
─────  ───────────────────────────────────────────────  ────────────  ──────────────────────────────────────────────────────────────────────────────────────────────
Add    Course_Test_Object__c                            CustomObject  force-app/main/default/objects/Course_Test_Object__c/Course_Test_Object__c.object-meta.xml
Add    Course_Test_Object__c.Test_Text_Field__c         CustomField   force-app/main/default/objects/Course_Test_Object__c/fields/Test_Text_Field__c.field-meta.xml
Add    Course_Test_Object__c-Course Test Object Layout  Layout        force-app/main/default/layouts/Course_Test_Object__c-Course Test Object Layout.layout-meta.xml
Add    Admin                                            Profile       force-app/main/default/profiles/Admin.profile-meta.xml
Add    Custom%3A Support Profile                        Profile       force-app/main/default/profiles/Custom%3A Support Profile.profile-meta.xml
Add    Custom%3A Marketing Profile                      Profile       force-app/main/default/profiles/Custom%3A Marketing Profile.profile-meta.xml
Add    Custom%3A Sales Profile                          Profile       force-app/main/default/profiles/Custom%3A Sales Profile.profile-meta.xml
  • The changes are added to the force-app directory within the VS Code project
  • Pushing the changes made to the source in VS Code is also possible. For example, we can delete the Custom Marketing, Custom Sales, and Custom Support default custom profiles, then push the changes back to the org with: sfdx force:source:push
    • This can also be used for any field configuration changes for example
=== Pushed Source
STATE    FULL NAME                    TYPE     PROJECT PATH
───────  ───────────────────────────  ───────  ────────────────────────────────────────────────────────────────────────────
Deleted  Custom%3A Support Profile    Profile  force-app/main/default/profiles/Custom%3A Support Profile.profile-meta.xml
Deleted  Custom%3A Sales Profile      Profile  force-app/main/default/profiles/Custom%3A Sales Profile.profile-meta.xml
Deleted  Custom%3A Marketing Profile  Profile  force-app/main/default/profiles/Custom%3A Marketing Profile.profile-meta.xml