To get started with SFDX, it is important to set up certain bases, in order to be able to use a Scratch Org. We can use thegraphic interface of VSCode or command lines.
You will need a devhub (a Salesforce organization that will allow you to create Scratch Org), if you don't have one you can create a development org (free) : https://developer.salesforce.com/signup
For activate the Dev Hub in an organization:
- Log into your Salesforce org
- In Setup, enter in the Quick Find box, then select "Dev Hub".
- To activate the Dev Hub, click Activate (Once the Dev Hub is activated, you cannot deactivate it.)
You'll need :
Objective of the article:
- Create an object and a field in a Scratch Org.
- Rretrieve the object and the field (metadata) locally on your machine.
- Create a control locally then deploy it on the Scratch Org
Here is an example of steps to be carried out with the CLI (command line):
0 / Connection to your DevHub
sfdx force: auth: web: login -r https://test.salesforce.com
(To check if you are well connected: sfdx force: org: list)
1 / Creation of the directory (of your project)
sfdx force: project: create –projectname nameOfYourProject –manifest
2 / Creation of a script file in order to avoid redoing the commands each time you create Scratch Org:
Example: setupScratchOrg.sh (under mac / linux)
Each command should be added to this file as your project progresses (creation of Scratch Org, assignment of set permissions, installation of packages, opening of scratch org ...)
To run it on mac or linux in a terminal:
You can do the same with a Windows, you will have to change the extension and run it in a command prompt:
3 / The command line allowing you to create your scratch org:
sfdx force: org: create -f config / project-scratch-def.json -a nameDeLaScratchOrg -s -w 20
(To check if the scratch org has been created: sfdx force: org: list)
4 / Open the scratch org with the command line
sfdx force: org: open
5 / Creation of the object from the interface and local recovery:
For my example, I created a new Technology__c object (at the bottom of the article a link to the creation of objects in Salesforce), a tab linked to this object and an application to display the tab.
sfdx force: source: pull
Let's move on to creating a field on the object you created and once again do a sweater
6 / Creation of a control from the room and sending to the Scratch Org
You can copy and paste the file recovered previously in the same directory then rename it with the name you want, in my example I need a new field Archive__c: Boolean.
You will have to modify the inside of the file to match your needs:
sfdx force: source: push
Your new control must be present in your Scratch Org, be careful, you may have to do manual actions in order to give visibility rights to the field.
If you don't have a lot of fields to create, I advise you to go through the Salesforce interface and retrieve via a sweater.
The last word:
Here, the basics of SFDX have been interviewed, very quickly I grant you! There is so much to say, it will be an opportunity to write other articles.
Example, now that we have this code locally and that we can create, develop in Scratch Org, how do we deploy everything to a sandbox / production ?!
When you are in doubt about a command or want to see its available parameters, you can add a "-h" at the end of the command for detailed help.
sfdx force: org: create -f config / project-scratch-def.json -a nameDeLaScratchOrg -s -w 20 -h
Site to learn about the Salesforce ecosystem:
Learn how to create an object:
Automated Scratch Org Creation File:
echo ### Creation of the Scratch Org ### sfdx force: org: create -f config / project-scratch-def.json -a CartoTechno -s -w 20 sfdx force: data: record: update -s User -w "Name = 'User User'" -v "LanguageLocaleKey = en_US " echo ### Push the code to the Scratch Org ### sfdx force: source: push -u CartoTechno echo ### Open the Scratch Org ### sfdx force: org: open -u CartoTechno # It is possible to add as many commands as desired in order to save time when creating the Scratch Org.