Get Ready to Create an App
Learning Objectives
- Explain the Salesforce DX project structure.
- Describe how to use Salesforce CLI to create a project.
- Describe how to use Salesforce CLI to import sample data.
Create a Salesforce DX Project
Before you can build your first app, create a project and connect it to your source control repository.
A Salesforce DX project is a local copy of your package metadata, a group of related code and customizations. It also contains the core assets required to synchronize local project source and metadata with your scratch orgs. Create the project on the same machine where you installed Salesforce CLI, then sync this project with your VCS repository.
So let’s get started. Create a project called geolocation
.
- In a terminal or command window, navigate to where you want your project located.
- Create the project:
sf project generate --name geolocation
This command creates a folder called geolocation, and scaffolds a new project with all assets in the proper folder structure. Here’s an example of a local project. You can see how a project evolves once you begin to add source code, Lightning components, and classes, just to name a few.
Asset Purpose sfdx-project.json
This file indicates that the directory is a Salesforce DX project. It contains project information and facilitates the authentication of orgs. It also tells the Salesforce CLI where to put files when syncing between the project and org. Within this file you specify: - The paths to your source code, classes, and metadata, as located in one or more package directories, in this case,
force-app
. - The namespace, if needed.
- The API version of your source.
config/project-scratch-def.json
Determines the configuration of a scratch org, including which features and settings define its org shape. You can create a configuration file that your whole dev team can share. force-app
The folder that contains the source for your project. - The paths to your source code, classes, and metadata, as located in one or more package directories, in this case,
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 Salesforce mobile web caching on or off by setting the enables1EncryptedStoragePref2
org preference. During development, disabling caching saves you from repeatedly refreshing the page waiting for your component code changes to take effect.
Optionally update the contents of config/project-scratch-def.json
to make it more personal, such as changing the orgName
property.
{ "orgName": "Account Geolocation App", "edition": "Developer", "features": ["EnableSetPasswordInApi"], "settings": { "lightningExperienceSettings": { "enableS1DesktopEnabled": true }, "mobileSettings": { "enableS1EncryptedStoragePref2": false } } }
Create a Scratch Org
Now that you understand the power of scratch orgs, here’s the basic workflow of when you use them in the Package Development Model.
- Push your local source and metadata to a scratch org.
- Pull any changes you make in the scratch org back to your local project.
- Sync this project with your source control repo.
But before we can play around with the workflow, we must first create a scratch org. Let’s do that now.
- In the command window, change to the
geolocation
project directory.cd geolocation
- Create a scratch org with the alias GeoAppScratch.
sf org create scratch --set-default --definition-file config/project-scratch-def.json --alias GeoAppScratch
Sometimes this process takes a few moments. We used these flags when launching the command:
- The
--set-default
flag indicates that you want this scratch org to be the default org for this project when running Salesforce CLI commands. To use a different org on a per command basis, you can specify the--target-org
flag and specify another alias. - The
--definition-file
flag is the path to the project scratch org configuration file. - Remember our friend, the
--alias
flag, from the previous unit? It lets you refer to the org using its alias, GeoAppScratch, in future commands that accept the--target-org
flag.
Create Sample Data
Scratch orgs come with some standard data based on the edition you choose. However, it’s important to add some sample data that’s more relevant to the app or package you’re building. So let’s use Salesforce CLI to create some sample accounts to test with our geolocation app.
- Create the Marriott Marquis account.
sf data create record --sobject Account --values "Name='Marriott Marquis' BillingStreet='780 Mission St' BillingCity='San Francisco' BillingState='CA' BillingPostalCode='94103' Phone='(415) 896-1600' Website='www.marriott.com'"
- Create the Hilton Union Square account.
sf data create record --sobject Account --values "Name='Hilton Union Square' BillingStreet='333 O Farrell St' BillingCity='San Francisco' BillingState='CA' BillingPostalCode='94102' Phone='(415) 771-1400' Website='www.hilton.com'"
- Create the Hyatt account.
sf data create record --sobject Account --values "Name='Hyatt' BillingStreet='5 Embarcadero Center' BillingCity='San Francisco' BillingState='CA' BillingPostalCode='94111' Phone='(415) 788-1234' Website='www.hyatt.com'"
To continue to rock your developer world, we provide commands to easily grab data from your scratch org and pull it into your project. You can then commit that data to your source control repository, so you can reload it if you, or another developer, spins up a new scratch org.
Whatever source control system you use, we recommend that you configure it to exclude the.sf
folder from being added to the repository. This folder holds temporary information for your scratch orgs, so you don’t have to save it for posterity . In Git, you would add it to the.gitignore
file.
- In your Salesforce DX geolocation project, create a directory called
data
.mkdir data
- Export some sample data.
sf data export tree --query "SELECT Name, BillingStreet, BillingCity, BillingState, BillingPostalCode, Phone, Website FROM Account WHERE BillingStreet != NULL AND BillingCity != NULL and BillingState != NULL" --output-dir ./data
You now have sample data that you can import in the future with this command. But don't do it now, we'll import the data into a different scratch org later.sf data import tree --files data/Account.json
So far we’ve synchronized regular data. Now we’re going to turn to the really fun part—the code.