n the next couple of posts we will create a REST API with Salesforce DX. First we’ll create a custom object and then expose that object through a REST API. When it’s complete we will have an API for a custom object that is similar to the built in Salesforce REST API for sObjects.
You can download the code for this project from GitHub.
In this first post we’re going to take care of the preliminaries:
- Create a new Salesforce DX project
- Create a new scratch org
- Create our custom object
- Pull the code for the custom object into our DX project
Prerequisites
This tutorial assumes that you have the following:
- A developer edition org with dev hub enabled
- The Salesforce CLI
- Visual Studio Code setup for Salesforce DX development
Let’s get started!
Create a new Salesforce DX Project
- In VS Code type Ctrl+Shft+P on Windows or Cmd+Shft+P on Mac to open the command palette
- Select SFDX: Create Project
- Type the name of your project. I’m calling mine house-api.
- Select the folder where you want to save the project.
Create a default scratch org
- Open the command palette again.
- Type or select SFDX: Create a default scratch org…
- Hit Enter to accept the default name for the scratch org config file: project-scratch-def.json
- Enter a name for your scratch org
- Enter the duration of your scratch org
- After the scratch org is created you should see the name of your default scratch org in the VS Code status bar.
Create the custom object in scratch org
- Go to the command palette again
- Select SFDX: Open Default Org. This will open your scratch org in a browser.
- In Object Manager create a new object called House
Add the following fields:
- Address (Text)
- City (Text)
- State (Text)
- Zip Code (Text)
- Square Feet (Number)
- Bedrooms (Number)
- Bathrooms (Number)
Pull the House custom object into your DX project
- Go to the command palette
- Select SFDX: Pull Source from Default Scratch Org
You should now have \force-app\main\default\objects\House__c\House__c.object-meta.xml added to your project as well as meta.xml files for each of the custom fields you added.
Sometimes I have to run the command to set my scratch org as the default before I can pull or push code for the first time. If the SFDX: Pull Source from Default Scratch Org command isn’t available in your command palette you will need to do this:
- Click on the name of your scratch org in the status bar at the bottom of VS Code. This will bring up a list of scratch orgs in your command palette.
- Click on the name of your scratch org where you created the House object. This will set it as the default and make the Push/Pull commands available.
Conclusion
In this post we:
- Created a new Salesforce DX project
- Created a default scratch org
- Created a custom House object in the Salesforce UI
- Pulled the meta data for the custom House object into our Salesforce DX project
Now we’re ready to start building the API to perform CRUD operations on our House object. We’ll do that in Part 2.