Salesforce DX - How to create DevHub and Scratch org



What is Salesforce DX?
Salesforce DX is an opportunity to shift how you manage the source of truth and app development lifecycle for your org.
But Salesforce DX isn’t a magic bullet that will fix every challenge your team has with organizing change and managing releases. In their current form, the tools related to Salesforce DX will be most accessible and useful for members of your team that can work with command line interfaces (CLIs) or integrated development environments (IDEs). Additionally, adopting Salesforce DX isn’t an all-or-nothing process. Because Salesforce DX is both a new set of tools and new features, your team might find some of the tools and features useful right now, and want to wait to investigate others.
Salesforce DX includes a “powerful command line interface and open APIs” which could allow developers to write their own scripts and automations, even if they aren’t deeply familiar with deployment and packaging.The new tooling will support more metadata types and offer a higher level interface, making deployment flows that currently need interaction with specific APIs more accessible to the community.
Salesforce DX adds a new class of orgs called Scratch Orgs, a lightweight environment for testing and development that can be launched on demand from the editor and command line. Different from traditional Sandboxes which mirror the metadata of their parent org, Scratch Orgs do not inherit any content and instead start up blank. An initial upload deploys metadata and data fixtures from files stored in source control before the org is ready to use. This descriptive mechanism guarantees that the new environment will be built from managed source code. Configurations that are not tracked in source control will not be present in a Scratch Org. With DX it will be possible to spin up clean environments in little to no time at all, so you can use a fresh Scratch Org for feature development, review or Continuous Integration .

Version Control and “Source Driven Development”

Perhaps the biggest shift in the Salesforce DX workflow is the movement towards “source driven development”. Using a source control tool is common in just about every software development ecosystem. Traditionally, Salesforce has been the exception here. With DX, this will change and the source of truth will be source code rather than a Salesforce Org.
Source control (or version control) is a mechanism for keeping track of your code. It’s important for several reasons: it allows you to collaborate with colleagues without overwriting each other’s work, it serves as a backup in case you need to rollback to previous versions of your software, and it allows you to release your code safely. (It does a lot of other things too, but those are the basics.) Most developers today are moving towards Git to handle their version control for Salesforce. In a source-driven development workflow, the source code can be kept in a version control system such as GitHub,  TFS,  SVN, or any other equivalent source control system.

Steps to setup Salesforce DX: 

1.     Install Salesforce CLI
2.     After successful installation check if it installed properly or not by using ‘sfdx’  command in command prompt.


 3.     We need to know what all the commands available before starting the actual configuration. Use this command to check list of available CLI commands : sfdx force:doc:command:list

      Note: Please find all the Salesforce command line interface commands here


 

4.     Verify your Salesforce CLI installation and plug-in versions.
5.     Run this command to verify the Salesforce CLI version:
Sfdx –version

You will see the below details in CMD.

sfdx-cli/6.0.10-3713d7b alpha (darwin-x64) node-v8.6.0

6.     Run this command to verify the Salesforce CLI plug-in version:
Sfdx plugins –core

You will see the CLI plugin version in CMD

salesforcedx 41.2.0 (core)

7.     This command returns a list of the other plug-ins installed in the CLI:
Sfdx plugins


Next Step is How to create Dev Hub:

Create Dev Hub org in order to work with Salesforce CLI. You can create the developer hub org for free (30 days validity) here

As soon as you creates developer hub org you will see that dev hub feature is enabled by default. Before moving ahead you can check and confirm yourself if it’s enabled .
Setup–> Developement–>Dev Hub Observe that Dev Hub is already enabled here. But if you have business organization (ISV partner or salesforce customer) where you want to work with salesforce cli and scratch orgs then you must enable Dev Hub manually.



Next step is to connect  the CLI to Salesforce. The CLI provides commands to connect using web-based OAuth flow, JWT token-based flow, and an sfdx auth URL.

Execute below command in command prompt to authorize the Dev hub with web login flow
sfdx force:auth:web:login -r https://login.salesforce.com

Once you execute the above command it will route you to the browser where you can enter your previously created dev hub login detail. On successful login yo will see the below message in you command prompt.

Successfully authorized sfdx.astro@****.com with org ID 00D6F000**********
You may now close the browser.

In order to check which are all orgs connected , you can execute below command
sfdx force:org:list


 Now your Dev Hub is connected with CLI. In the above screen we could see Alias name is blank.

In this step will set the alias name to the Dev Hub. Run the below command in your command prompt.
sfdx force:alias:set devbox=sfdx.astro@****.com


Next step we need to make this Dev Hub as default org for this CLI. Run the below command in CMD


sfdx force:config:set defaultdevhubusername=devbox –g

Here -g denotes global and hence, by default, for all the DX projects, the same Dev Hub username will be used to create a scratch Org for other DX projects

Finally we have successfully created Dev Hub and connected to CLI and make it as default org.


Next Step We will learn how to create Scratch orgs and How to Create Project:

Scratch orgs are nothing but temporary orgs where developer can use to develop the code and test the application. Scratch orgs life default life time is 7 days and max is 30 days.

Run the below command to create project.

sfdx force:project:create --projectname DXProject --outputdir C:\Users\mallareddy.reddy\Desktop\SFDXLearning\myWork

Nmae of the project folder here is DXproject. After you creates the project you can go to the path where you actually creates the folder. It will looks like below.




By default, scratch orgs are empty. They don’t contain much of the sample metadata that you get when you sign up for an org, such as a Developer Edition org, the traditional way. Some of the things not included in a scratch org are:
  • Custom objects, fields, indexes, tabs, and entity definitions
  • Sample data
  • Sample Chatter feeds
  • Dashboards and reports
  • Workflows
  • Picklists
  • Profiles and permission sets
  • Apex classes, triggers, and pages
Setting up different scratch org definition files allows you to easily create scratch orgs with different shapes for testing. For example, you can turn Chatter on or off in a scratch org by setting the ChatterEnabled org preference in the definition file. If you want a scratch org with sample data and metadata like you’re used to, we have an option for that: hasSampleData.


From above folder Click on Config --> Project-scratch-def.json where you can define the structure of scratch org that you are going to create.

{
"orgName": "Demo Company",
"edition": "Enterprise",
"orgPreferences": {
"enabled": ["S1DesktopEnabled"]
}
}

You can find more about scratch org definition here

The above structure only have the mandatory fields to create scratch org.

We can modify the definition of the scratch or as per your requirement. Here I am adding few definations to the project-scratch-def.json file as below.

{
"orgName": "Salesforce Developer",
"country": "US",
"username":"sfdc.bare1@*****.com",
"adminEmail":"reddy.********@gmail.com",
"edition": "Enterprise",
"hasSampleData": "true",
"features": ["MultiCurrency"],
"orgPreferences": {
"enabled": ["S1DesktopEnabled", "ChatterEnabled"]
}
}
 
orgName: Name of Scratch org
Username :New user for your scratch org
adminEmail : Email address of the Dev Hub user making the scratch org creation request
Edition : Valid entries are Developer, Enterprise, Group, or Professional
hasampleData : Valid values are true and false. False is the default, which creates an org without sample data.
 
Run the below command to create scratch org.

C:\Users\mallareddy.reddy\Desktop\SFDXLearning\myWork\DXProject\config>sfdx force:org:create -f project-scratch-def.json -a myscratchOrgNew -d 30
 
 
Make necessary changes to the command according to your folder location.
Here what is the significance of types in command?
sfdx force:org:create
[-a SETALIAS]
[-d DURATIONDAYS]
[-f DEFINITIONFILE]

You are done with the scratch org creation and duration of the org is 30 days, alias name is myscratchOrgNew

Now you check the list of connected orgs by executing the below command.

Sfdx force:org:list



Now you are all set to open your Scratch org. Run the below command from your command prompt to open your scratch org in browser.

sfdx force:org:open -- If we made default org then this command is useful
OR
Sfdx force:org:open –u myscratchOrg




Use any of the below command to generate the password for your scratch orgs.

sfdx force:user:password:generate -u myscratchOrgNew
sfdx force:user:password:generate --targetusername=myscratchOrgNew

Runs any of the below commands to check your scratch org user details including password and access token.

sfdx force:user:display u myscratchOrg
sfdx force:user:display --targetusername=myscratchOrg

Here we learn below points.
1.     How to create Dev Hub
2.     How to connect dev Hub to CLI
3.     How to create Project
4.     How to create Scratch org
5.     How to generate password for Scratch org

I will explain how to install package and uninstall  And Data Commands (Create, update,delete,export)

Note: Please refer to this salesforce official documentation for more information.


 

Post a Comment

0 Comments