Release - November 22
Search…
⌃K

Introduction to Scratch Orgs

Learning Objectives

  • What is Scratch Org?
  • How it is different from normal Salesforce Sandboxes / Developer Orgs
  • How to create Scratch Org
Time to Complete: 45 Minutes

Steps

Create dreamhouse community

  • Enable Experience Cloud on Playground 1
    • Go to setup --> search digital experiences -> select settings in quick find --> Enable Digital Experience --> save
  • Enable experience bundle on Playground 1
    • Go to setup --> search digital experiences -> select settings in quick find
      --> Enable ExperienceBundle Metadata API --> save
  • Create community on Playground 1
    • Go to setup --> search all sites in quick find --> New --> Customer Account Portal --> Get started --> name = dreamhouseportal --> Create --> In workspaces --> Administration --> Activate

Retrieve the community from the org

Add the below package.xml to your project root
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<name>AuraDefinitionBundle</name>
<members>forgotPassword</members>
<members>loginForm</members>
<members>selfRegister</members>
<members>setExpId</members>
<members>setStartUrl</members>
</types>
<types>
<name>ApexClass</name>
<members>ChangePasswordController</members>
<members>ChangePasswordControllerTest</members>
<members>CommunitiesLandingController</members>
<members>CommunitiesLandingControllerTest</members>
<members>CommunitiesLoginController</members>
<members>CommunitiesLoginControllerTest</members>
<members>CommunitiesSelfRegConfirmController</members>
<members>CommunitiesSelfRegConfirmControllerTest</members>
<members>CommunitiesSelfRegController</members>
<members>CommunitiesSelfRegControllerTest</members>
<members>ForgotPasswordController</members>
<members>ForgotPasswordControllerTest</members>
<members>LightningForgotPasswordController</members>
<members>LightningForgotPasswordControllerTest</members>
<members>LightningLoginFormController</members>
<members>LightningLoginFormControllerTest</members>
<members>LightningSelfRegisterController</members>
<members>LightningSelfRegisterControllerTest</members>
<members>MyProfilePageController</members>
<members>MyProfilePageControllerTest</members>
<members>SiteLoginController</members>
<members>SiteLoginControllerTest</members>
<members>SiteRegisterController</members>
<members>SiteRegisterControllerTest</members>
</types>
<types>
<name>ApexComponent</name>
<members>SiteFooter</members>
<members>SiteHeader</members>
<members>SiteLogin</members>
<members>SitePoweredBy</members>
</types>
<types>
<name>ApexPage</name>
<members>AnswersHome</members>
<members>BandwidthExceeded</members>
<members>ChangePassword</members>
<members>CommunitiesLanding</members>
<members>CommunitiesLogin</members>
<members>CommunitiesSelfReg</members>
<members>CommunitiesSelfRegConfirm</members>
<members>CommunitiesTemplate</members>
<members>Exception</members>
<members>FileNotFound</members>
<members>ForgotPassword</members>
<members>ForgotPasswordConfirm</members>
<members>IdeasHome</members>
<members>InMaintenance</members>
<members>MyProfilePage</members>
<members>SiteLogin</members>
<members>SiteRegister</members>
<members>SiteRegisterConfirm</members>
<members>SiteTemplate</members>
<members>StdExceptionTemplate</members>
<members>Unauthorized</members>
<members>UnderConstruction</members>
</types>
<types>
<name>NavigationMenu</name>
<members>Default_Navigation</members>
</types>
<types>
<name>Network</name>
<members>*</members>
</types>
<types>
<name>ExperienceBundle</name>
<members>*</members>
</types>
<types>
<name>CustomSite</name>
<members>*</members>
</types>
<types>
<name>StaticResource</name>
<members>SiteSamples</members>
</types>
<version>55.0</version>
</Package>
Execute the sfdx force:source:retrieve command to retrieve the related files
sfdx force:source:retrieve -x package.xml -u <playground-1>

Enable contacts to multiple accounts setting

  • Go to setup --> search account settings in quick find --> Allow users to relate a contact to multiple accounts --> save
  • Update the ContactController class
    • Go to developer console --> file --> open --> Classes --> ContactController --> replace with below snippet --> save
    • Retrieve the ContactController from the org using sfdx force:source:retrieve command
public class ContactController {
@AuraEnabled
public static List<contact> getContacts(string searchKey){
string key = '%'+ searchKey +'%' ;
return [SELECT Id,Name FROM Contact WHERE Name LIKE :key ORDER BY Name LIMIT 50];
}
/*
* Get Account Contact Relation
*/
public static List<AccountContactRelation> getContactAcrs(string contactId){
return [SELECT Id,AccountId,Roles,IsActive,IsDirect,StartDate,EndDate FROM AccountContactRelation WHERE ContactId =: ContactId];
}
}

Enable DevHub

  • Open your playground 1
  • Setup --> Search Dev Hub in Quick find--> Enable Dev Hub
  • Run the below command to see scratch orgs available for you
sfdx force:limits:api:display -u <playground-1>
Developer Edition/Trailhead playground provides 5 active scratch orgs at any given point in time. Create up to 6 scratch orgs per day by deleting the used ones. Create multiple playgrounds as you need to get more scratch orgs

Create a new scratch org

Use the sfdx force:org:create command to create a new scratch org
sfdx force:org:create --definitionfile config\project-scratch-def.json --setdefaultusername --setalias <scratchorgaliasyouwishtouse> --durationdays <1-30> -v <playgroundaliasfromwhichyouwanttocreate>
Scratch Orgs adopt source tracking API which helps track the changes in both the local project and your scratch org. No need to track the list of files you modified.

Install your dependencies to your scratch org

Use the sfdx forceinstall command to install dependencies.
sfdx force:package:install -p 04t6F000001ZL7yQAG -w 30 --apexcompile package --securitytype AdminsOnly --noprompt -u <so-alias>
sfdx force:package:install -p 04t5I000001ecttQAA -w 30 --apexcompile package --securitytype AdminsOnly --noprompt -u <so-alias>
sfdx force:package:install -p 04t6F000001D6LOQA0 -w 30 --apexcompile package --securitytype AdminsOnly --noprompt -u <so-alias>

Check the source status

Use sfdx force:source:status to check the status
sfdx force:source:status

Create community on Scratch Org

Navigage to Setup Menu
Search for "All Sites" in the Quick Find
Click on New > Customer Account Portal > Get started > name = dreamhouseportal
Create

Deploy your local source project to the scratch org

Use the sfdx force:source:push command to deploy
sfdx force:source:push --forceoverwrite
Did that fail?... Yes, we expected it to fail! Let's tackle that in our next challenge.

Delete the scratch org

Use the force:org:delete command to delete your created scratch org
sfdx force:org:delete --targetusername <so-alias>
Commit your changes to the repo.

Recap

Scratch _**_Orgs are an important tool in Salesforce DX. It enables the quick provisioning and revoking of an org. You will really see the power when it is combined with source tracking.