How to create a scratch org with namespaces in Salesforce DX

|
| By Webner

Salesforce DX is a platform that allows us to create multiple scratch Orgs with namespaces equivalent to the packaging organization. In order to achieve this, firstly link your Dev Org with namespaces to the Developer Hub Org. This process is also known as registering namespaces.

To register a namespace in your Developer Hub Org (if you do not have one, signup using https://developer.Salesforce.com/promotions/Orgs/dx-signup), use the Namespace Register tab and click on Link Namespace, as shown in the following screenshot:

sf1

Once you click on Link Namespace, a login for the Salesforce instance pops up. These will be the credentials of the Org where you have namespaced and created your managed packages.
Upon registering a namespace, you will notice a Namespace Registry record, as shown in the following screenshot:

sf2

Once the Namespace is registered in your dev hub you can spin up a new scratch org using the IDE.

Below are the steps for creating a scratch org with namespace using the VS Code IDE:

  1. Open your project in VS Code.
  2. Find “sfdx-project.json” file in your project.
  3. Add the “namespace” attribute in the file and provide your dev org’s
    namespace as shown below:
    {
    "packageDirectories" : [
    { "path": "force-app", "default": true}
    ],
    "namespace": "apple",
    "sfdcLoginUrl" : "https://login.salesforce.com",
    "sourceApiVersion": "44.0"
    }
  4. Now save the file and create a scratch org using the terminal.
  5. Run sfdx force:org:create -d <number of days you want your scratch org to be live ranging from 1-30> -f <path of you project scratch def file like .config\peoject-scratch-def.json> -v <Your dev hub name>
  6. If you see an error, then go and check if the namespace is registered in your dev hub org.
  7. If you didn’t get the above error you can check the scratch org is created with a namespace.

You can also create multiple scratch orgs with the same namespace.

Leave a Reply

Your email address will not be published. Required fields are marked *