Erfassen Sie Ihre Fortschritte
Trailhead-Startseite
Trailhead-Startseite

Create a Salesforce DX Project

In this step you play the role of Chan Ming Lewis, a Salesforce developer at DreamHouse Realty, who uses the Salesforce CLI to create a new project folder and populate it with the DreamHouse app’s metadata in preparation for developing new features.

At the end of this step your project folder should look something like this:

Salesforce DX project structure

Create a Salesforce DX Project

A Salesforce DX project has a specific structure and a configuration file that identifies the directory as a Salesforce DX project.

  1. Use the Salesforce CLI to create a skeleton project structure named sfdx-chan.
    sfdx force:project:create --projectname sfdx-chan
  2. Navigate to the sfdx-chan project directory you just created.
    cd sfdx-chan
Note

Note

In practice you would name the project something that is more descriptive of its purpose, like sfdx-sales-app or sfdx-expense-tracker-app, but because you are simulating multiple developers on the same machine, we thought naming the folders after the person can help keep things organized.

Configure a Scratch Org Definition File

Scratch org definition files allow you to easily create scratch orgs with different features or preferences for testing. For example, you can turn Lightning Experience caching on or off by setting the enableS1EncryptedStoragePref2 org preference. During development, disabling caching saves you from repeatedly refreshing the page waiting for your Lightning component code changes to take effect.

  1. Replace the contents of sfdx-chan/config/project-scratch-def.json with the below scratch org definition that disables Lightning Experience caching.
    {
      "orgName": "DreamHouse Realty",
      "edition": "Developer",
      "settings": {
        "mobileSettings": {
          "enableS1EncryptedStoragePref2": false
        }
      }
    }
Note

Note

Disabling secure and persistent browser caching has a significant negative performance impact on Lightning Experience. Always enable the setting in production orgs.

Ignore Non-Metadata Files

When syncing metadata between your local file system and a scratch org, or the other way around, you might have source files or metadata that you want to exclude. Sometimes, you make a change directly in a scratch org but you don’t want to pull that change into your local DX project. Sometimes, you have files in your local DX project that you don't want to deploy - or can't deploy - to your scratch org. In these situations, create a file named .forceignore to specify the files or directories you want to exclude.

  1. In your sfdx-chan project folder, create if doesn't exist a file named .forceignore.
  2. Ensure the file excludes the following file patterns. If not, save the below contents into the file.
    package.xml
    **profile
    # LWC configuration files
    **/jsconfig.json
    **/.eslintrc.json
    # LWC Jest
    **/__tests__/**

Retrieve Existing Metadata

Before Chan and Maria begin using Salesforce DX they need to retrieve the existing metadata for DreamHouse Realty’s app and convert it to the source format for syncing with scratch orgs. Since the metadata does not exist in source control (yet), then it must be retrieved from the Dev Hub org using a package.xml manifest file.

  1. In your sfdx-chan project folder, create a sub-folder named assets.
    mkdir assets
  2. In your sfdx-chan/assets folder, create a file named package.xml and save the below manifest contents into the file.
    <?xml version="1.0" encoding="UTF-8"?>
    <Package xmlns="http://soap.sforce.com/2006/04/metadata">
      <types>
        <members>DreamHouseSampleDataController</members>
        <members>DreamHouseSampleDataControllerTest</members>
        <name>ApexClass</name>
      </types>
      <types>
        <members>DreamHouseSampleData</members>
        <name>ApexPage</name>
      </types>
      <types>
        <members>Broker__c.Broker_Compact</members>
        <members>Property__c.Property_Compact_Layout</members>
        <name>CompactLayout</name>
      </types>
      <types>
        <members>DreamHouseLogo</members>
        <name>ContentAsset</name>
      </types>
      <types>
        <members>DreamHouse</members>
        <name>CustomApplication</name>
      </types>
      <types>
        <members>Broker__c</members>
        <members>Property__c</members>
        <name>CustomObject</name>
      </types>
      <types>
        <members>Broker__c</members>
        <members>Property__c</members>
        <members>Sample_Data_Import</members>
        <name>CustomTab</name>
      </types>
      <types>
        <members>Property_Record_Page</members>
        <name>FlexiPage</name>
      </types>
      <types>
        <members>Broker__c-Broker Layout</members>
        <members>Property__c-Property Layout</members>
        <name>Layout</name>
      </types>
      <types>
        <members>Broker__c.All</members>
        <members>Property__c.All</members>
        <name>ListView</name>
      </types>
      <types>
        <members>DreamHouse</members>
        <name>PermissionSet</name>
      </types>
      <version>47.0</version>
    </Package>
  3. In your sfdx-chan working directory, retrieve the DreamHouse app’s metadata.
    sfdx force:source:retrieve --manifest assets/package.xml --targetusername DevHub --wait 10

Export Sample Data

Most developers don’t enjoy the tedious task of creating records to work with every time they create a new scratch org or sandbox, so let’s export some sample data from your Dev Hub org to save with the project.

  1. In your sfdx-chan working directory, export property and broker records and save them as a JSON file in the sub-folder assets/data.
    sfdx force:data:tree:export --targetusername DevHub --outputdir assets/data --query "SELECT Id, Name, Email__c, Phone__c, Mobile_Phone__c, Title__c, Picture__c, ( SELECT Id, Address__c, Assessed_Value__c, Baths__c, Beds__c, Broker__c, City__c, Date_Agreement__c, Date_Closed__c, Date_Contracted__c, Date_Listed__c, Date_Pre_Market__c, Description__c, Location__Longitude__s, Location__Latitude__s, Picture__c, Price__c, Name, State__c, Status__c, Tags__c, Thumbnail__c, Title__c, Zip__c FROM Properties__r ) FROM Broker__c"

Excellent—you now have the starting source code for the DreamHouse app and some sample data in your Salesforce DX project. You’re now ready to add this project to source control.

We won't check any of your setup. Click Verify Step to go to the next step in the project.

Resources