What is Salesforce DX Project?
Salesforce DX introduces a new project structure for your org’s metadata (code and configuration), your org templates, your sample data, and all your team’s tests. Store these items in a version control system (VCS) to bring consistency to your team’s development processes. Retrieve the contents of your team’s repository when you’re ready to develop a new feature.
A Salesforce DX project has a specific structure and a configuration file that identifies the directory as a Salesforce DX project.
What are the different options to create a Salesforce DX project?
|Use the Sample Repository on GitHub||Explore the features of Salesforce DX using one of the sample repos and your own VCS and toolset.|
|Create a Salesforce DX Project from Existing Source||Start with an existing Salesforce app to create a Salesforce DX project.|
|Create a Salesforce DX Project||Create an app on the Lightning Platform using a Salesforce DX project.|
Sample Repository on GitHub
If you want to check out Salesforce DX features quickly, start with the sfdx-simple GitHub repo. It contains an example of the project configuration file (sfdx-project.json), a simple Salesforce app, and Apex tests.
Cloning this repo creates the directory sfdx-simple. Assuming that you’ve already set up Git, use the git clone command to clone the master branch of the repo from the command line.
To use SSH:- git clone email@example.com:forcedotcom/sfdx-simple.git
To use HTTPS: git clone https://github.com/forcedotcom/sfdx-simple.git
Note:- In the above command, forcedotcom is the username of GitHub account and sfdx-simple.git is the repository name.
Another simple way to do it is to download a .zip file of the repository’s source using Clone or download on the GitHub website. Unpack the source anywhere on your local file system.
Assuming that you’ve already set up Git, Visual Studio Code and Salesforce CLI, another way of doing it is:-
- Open your visual studio code and Press ctrl+shift+p. It will open the command palette.
- type Git: Clone
- It will ask the Repository URL, Once you provide the Repository URL, press enter.
- A window will pop up to select the path directory. Select a folder and enter
- The command will get execute and once it is finished, it will give you an option saying:-
Once You select Open Repository, You editor will look like this:-
Create a Salesforce DX Project from Existing Source
If you are already a Salesforce developer or ISV, you likely have an existing source in a managed package in your packaging org or some application source in your sandbox or production org. Before you begin using Salesforce DX, retrieve the existing source and convert it to the source format.
- Create a Salesforce DX project.
- Create a directory for the metadata retrieve. You can create this directory anywhere. Use mkdir mdapipkg.
- Retrieve your metadata source.
The format of Current Source How to Retrieve Your Source for Conversion You are a partner who has your source already defined as a managed package in your packaging org. Retrieve Source from an Existing Managed Package You have a package.xml file that defines your unpackaged source. Retrieve Unpackaged Source Defined in a package.xml File
Retrieve Source from an Existing Managed Package
- In the project, create a folder to store what’s retrieved from your org, for example, Use mkdir mdapipkg.
- Retrieve the metadata.
sfdx force:mdapi:retrieve -s -r ./mdapipkg -u <username> -p <package name>
The username can be a username or alias for the target org (such as a packaging org) from which you’re pulling metadata. The -s parameter indicates that you’re retrieving a single package. If your package name contains a space, enclose the name in single quotes.
-p ‘Test Package’
The convert command ignores all files that start with a “dot,” such as .DS_Store. To exclude more files from the convert process, add a .forceignore file.
- To indicate which package directory is the default, update the sfdx-project.json file.
- Convert metadata format to source format.
sfdx force:mdapi:convert –rootdir <retrieve dir name>
The –rootdir parameter is the name of the directory that contains the metadata source, that is, one of the package directories or subdirectories defined in the sfdx-project.json file.
If you don’t indicate an output directory with the –outputdir parameter, the converted source is stored in the default package directory indicated in the sfdx-project.json file. If the output directory is located outside of the project, you can indicate its location using an absolute path.
Retrieve Unpackaged Source Defined in a package.xml File
But what if you don’t have a package.xml file already created? See Sample package.xml Manifest Files in the Metadata API Developer Guide.
- In the project, create a folder to store what’s retrieved from your org, for example, mdapipkg.
- Retrieve the metadata.
sfdx force:mdapi:retrieve -r ./mdapipkg -u <username> -k ./package.xmlThe username can be the scratch org username or an alias. The -k parameter indicates the path to the package.xml file, which is the unpackaged manifest of components to retrieve.
Refer below image to create a project with Manifest which consists package.xml file:-
Authorize an org:-
select the URL to login:-
Give an alias to your org to use it later:-
After authorization, open terminal in the VS Code and write below command:-
sfdx force:source:retrieve –manifest C:\Users\HP\Desktop\retrieveMetadataFromPackage\manifest\package.xml
Note:- Change the path directory accordingly. The path would be where your package.xml exist.
Refresh the folder and You will see the metadata in default/main folder.
Create a Salesforce DX Project
- Use the force:project:create command to create a skeleton project structure for your Salesforce DX project. If you don’t indicate an output directory, the project directory is created in the current location. You can also specify the default package directory to target when syncing source to and from the scratch org. If you don’t indicate a default package directory, this command creates a default package directory, force-app.
The force:project:create command generates these samples configuration files to get you started:
sfdx force:project:create --projectname mywork
sfdx force:project:create --projectname mywork --defaultpackagedir myapp
- Press Ctrl+shift+p and select sfdx: create project
- Give a project name and press enter. A popup will appear to select the destination of the project. Once selected, your project is ready.
Also, Take a look at some stuff on Lightning web Component here:- https://salesforcediaries.com/category/lightning-web-component/