SFDX

SFDX - Create a Scratch Org - Pull - Push

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

Connection to the org created previously (DevHub)

(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

Files automatically create

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 ...)

Example of my autocreation file

To run it on mac or linux in a terminal:

sh setupScratchOrg.sh

You can do the same with a Windows, you will have to change the extension and run it in a command prompt:

setupScratchOrg.ps1

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

Creation of the Scratch Org

(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

Retrieving the "pull" command for the object

Let's move on to creating a field on the object you created and once again do a sweater

Retrieving the control created from the scratch org

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.

Field created directly in the local directory

You will have to modify the inside of the file to match your needs:

Code to create a checkbox (boolean) type field locally

sfdx force: source: push

Sending the new control to the Scratch Org

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 ?!

Advice:

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.

Example:

sfdx force: org: create -f config / project-scratch-def.json -a nameDeLaScratchOrg -s -w 20 -h

Help for the scratch org creation command

Site to learn about the Salesforce ecosystem:

https://trailhead.salesforce.com/

Learn how to create an object:

https://trailhead.salesforce.com/fr/content/learn/modules/data_modeling/objects_intro

Automated Scratch Org Creation File:

setupScratchOrg.sh

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.

en_USEnglish