Resolution of Managed Packages - Salesforce DX Developer Guide
Working with Salesforce DX for the resolution of managed packages:
- First, we create an org and enable the DX version in it that is DEVHUB.
- Enabling DEV HUB ensures that we can use Salesforce DX features in that Org.
- We also need to install CLI for working with DX tools in the terminal window.
- Through Salesforce DX, we can create what are called scratch org which are temporary orgs and can be used to make changes in the managed package codebase having a namespace. In this way, multiple developers can work on the same project without conflicting with one another and after the separate development, the code changes in the scratch org can be merged with the developer org having the managed package.
- NOTE - Scratch orgs can only be made from the org having DEV HUB enabled which can be done only in those orgs where the package is not being made.
Don't forget to check out: Salesforce DX – The X Factor For Salesforce Developers
Following are the steps to work with the managed package codebase in the Scratch org:
Enable DEV HUB in a Salesforce org and link namespace with the developer org having managed packages.
The Domain Name should be defined in that Org. Following are the steps:
- From the App Launcher menu, select Namespace Registries.
- Click Link Namespace.
- Make sure that your browser allows pop-ups from Dev Hub organizations.
- Log in to the Developer Edition organization where the namespace is registered using the organization's system administrator credentials.
- To view all namespaces linked to the namespace registry, select the "All Namespace Registry" view.
Now we open the terminal window and log in to the DEV HUB enabled org using the command: sfdx force:auth:web:login -d -a DevHub
This command opens the org and sets it to default and defines the alias name ‘DevHub’ to it.
The next step is to set up the project on the local machine. These are the steps:
- First, we create a folder by the command: sfdx force:project:create -n ‘Folder Name’
This command not only creates a folder with the specified name but also creates some files which are necessary for creating scratch orgs. (like sfdx-project.json) - To retrieve the contents from developer org having the managed package, we write the command in the terminal : sfdx force:mdapi:retrieve -s -r ./mdapipkg -u <username> -p <package name>
- This is a zip file so we need to unzip it with the command: unzip unpackaged.zip -d. On unzipping, the code is unzipped to metadata format.
- Delete the unpackage.zip file: rm unpackaged.zip
Check out another amazing blog by Anuj here: How to Setup Salesforce on VS Code - Developer Guide
Now we move to the folder where the project is created and in the terminal, we convert the metadata format to the source format which is required for the code to be pushed in the scratch org.
The command is: sfdx force:mdapi:convert -r mdapipkg/
- Now we create a new SCRATCH Org the command: sfdx force:org:create -s -f config/project-scratch-def.json -a <Alias Name>
- We can open the newly created Scratch org by the command: sfdx force:org:open
- Before moving the code to the scratch org, we specify the namespace of the managed package in the sfdx-project.json file
- After specifying the namespace, we push the codebase to the scratch org using the command: sfdx force:source:push
- All the code base of the developer org has now been moved to the scratch org along with the namespace.
- Now we can make the changes to the scratch org.
- After making all the changes, now we move the code from the scratch org to the local machine using the command: sfdx force:source:pull
The code changes are moved to the local machine in the same place from where they were pushed (i.e. the ‘force-app’ folder) . - Now we convert it back to the metadata format using the command: sfdx force:source:convert -r force-app -d deploy
-r denotes the folder directory where the code to be converted is present.
-d denotes the folder where the converted code is saved. - Now the last step is to deploy the code to the main org for which the command is: sfdx force:mdapi:deploy -d deploy -u ‘username of the dev org’
- To generate a password for a Scratch Org, we use the command: sfdx force:user:password:generate -u ‘Alias Name’
- By default, scratch orgs are active only for a period of 7 days, but we can extend it to a maximum of 30 days.
Reference: salesforce.stackexchange