SFDC Stop - Always the latest about Salesforce


Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.


Telegram logo   Join our Telegram Channel

Saturday 30 May 2020

Getting Started with Scratch Orgs | DevHub | Link VSCode project to a New Scratch Org

Hello Trailblazers,

In this post we're going to learn how we can enable devhub to create a scratch org and we'll also create a new VSCode project and link it with scratch org. Let's begin.

Enabling DevHub

In order to enable DevHub in your developer edition / trial / production org / business org, follow the below steps:-

1. Login as a System Administrator

2. Go to Setup and search for DevHub

3. You'll see a screen as shown below:-

Enable DevHub

Toggle the button which says Enable Dev Hub to enable the dev hub (Please note that you cannot disable a DevHub once you've enabled it in your org).

Once you've enabled the devhub, you'll have some objects automatically visible in your org like:- Active Scratch Orgs, Scratch Org Infos etc.

Active Scratch Org object basically consist of information about all the scratch orgs that are currently Active.

Scratch Org Info object consist of information about all scratch orgs that you created using this DevHub no matter whether they're currently active or expired. It basically have the metadata of your scratch orgs like:- Edition, Created Date, Expiration Date, Status etc.

When you've enabled DevHub, you can just refresh the page once and search for Active Scratch Orgs in the app launcher. You can open Active Scratch Orgs as shown below:-

Active Scratch Orgs

It is currently empty because we haven't created any scratch org yet. Let's create a new scratch org, but before that, we need to create a new VSCode Project.

Create a new VSCode Project

Go to your VSCode and create a new project like we did in our How to setup Visual Studio Code for Salesforce tutorial. This time you can select Create Project instead of Create Project with Manifest opton as we don't need a manifest while working with Scratch Orgs as shown below:-


You can use the standard template itself


and add a project name you like to create a new project. I am using ScratchOrg1 here.


Once you've created a new project. The next step is to Authorize Our DevHub. So, let's see how we can do that.

Authorize DevHub

In order to authorize DevHub, open a terminal in VSCode and enter the below command:-

sfdx force:auth:web:login -d -a <Alias of your DevHub>

In the above command,

-d is used to specify Default Dev Hub. We're setting this DevHub as the default devhub for scratch org creation in future

-a is used to specify the Alias of your DevHub. An alias is nothing but friendly name for your devhub. Like in the below screenshot you can see that I am setting my devhub alias as CuriousFoxDevHub so that in future I can simply refer to this org using the alias I have given.


Once you press Enter, It'll automatically open your browser where you can login and it'll ask you for permissions after logging in as shown below:-


Click on Allow. You've now successfully authorized your DevHub and you'll see the below message in your CLI:-

  

Create a New Scratch Org

Now we need to create a new scratch org. As we've already created a new VSCode project. Inside that project only, open the command palette and type Create a Default Scratch Org as shown below:-


When you choose Create a Default Scratch Org, it automatically creates a scratch org for the current project you're working on. Choose this option and you'll see another option where you need to select the scratch org definition file as given below:-


Select the default scratch org definition file which is already created for you unless you want some extra features automatically enabled in your scratch org like:- MultiCurrency, Person Accounts etc. The default config file is stored in the config folder as shown below:-


You can add a number of features as shown above, to learn more about the scratch org definiton file, click here

Once you've selected a definition file, you need to enter an alias for your scratch org as shown below:-


SFDC Stop Tip:- Give the same alias as the name of your project so that you remember it easily. I am giving the same alias here i.e. ScratchOrg1

Next, you need to specify the number of days between 1-30 for which your scratch org is active as shown below:-


Press Enter and your scratch org will be created automatically using the default DevHub that we set before. You can open the sfdx-config.json file where you can see the defaultdevhubusername as well as the defaultusername of your org which is linked with the current project as shown below:-


Once your scratch org is created, you can open that by opening the command palette and writing the command:- Open Default Org and pressing Enter as shown below:-


This command will automatically open your newly created Scratch Org in your browser. You can also go to your DevHub where you can see a new record under the Active Scratch Orgs section as shown below:-


Congratulations..!! You've created a New Scratch Org Succesfully and linked it with VSCode.

Now it's your turn, create a new scratch org and try updating data in Scratch Org and pull it down in VSCode. Also, create a new Apex Class or any other stuff in VSCode and push it into your scratch org.

That's all for this tutorial everyone, if you liked it, make sure to share it in your network and let me know your feedback in the comments down below.

Tired of reading or just scrolled down ? Don't worry, you can watch the video too..!!


Happy Trailblazing..!!

No comments:

Post a Comment