Sunday, 28 March 2021

How to create an unlocked package?

 Hi ,

Here we are going to learn how we can create an unlocked package with simple "sfdx" commands.

Enable Devhub and Unlocked Package, Second Generation package in Developer edition or Production.
Authorize dev hub from Command line or vs code terminal
        sfdx force:auth:web:login -d -a DevHub
It opens a login page then give credentials then you are successfully authorized

Now clone the project from the repository or create a project with all the components in vs code.

Add the following snippet in "sfdx-project.json" file if it is already not there

{

   "packageDirectories": [

      {

         "path": "force-app",

         "default": true

      }

   ],

   "namespace": "",

   "sfdcLoginUrl": "https://login.salesforce.com",

   "sourceApiVersion": "50.0"

}


Now create a package with the help of following command:

sfdx force:package:create --name TestPackage  --description "TestPackage Desc" --packagetype Unlocked --path force-app --nonamespace --targetdevhubusername DevHub

Here DevHub is the alias of our DevHub configured while authorizing the DevHub

 After the creation of the package, it includes different items in sfdx-project.json   file as shown below.

{

    "packageDirectories": [

        {

            "path": "force-app",

            "default": true,

            "includeProfileUserLicenses": true,

            "package": "TestPackage ",

            "versionName": "ver 0.1",

            "versionNumber": "0.1.0.NEXT"

        }

    ],

    "namespace": "",

    "sfdcLoginUrl": "https://login.salesforce.com",

    "sourceApiVersion": "50.0",

    "packageAliases": {

        "TestPackage": "0HoXXXXXXXX"

    }

}

We can change the version number here manually if needed:

{

    "packageDirectories": [

        {

            "path": "force-app",

            "default": true,

            "includeProfileUserLicenses": true,

            "package": "TestPackage ",

            "versionName": "Version 1.0",

            "versionNumber": "1.0.0.NEXT"

        }

    ],

    "namespace": "",

    "sfdcLoginUrl": "https://login.salesforce.com",

    "sourceApiVersion": "50.0",

    "packageAliases": {

        "TestPackage": "0HoXXXXXXXX"

    }

}


Now its time to create a package version with the help of the following command:

sfdx force:package:version:create -p TestPackage -d force-app -k test1234 --wait 10   -v DevHub

or 

if we want to promote this package version then we have add "--codecoverage"  in the command.

sfdx force:package:version:create -p TestPackage -d force-app -k test1234 --wait 10  --codecoverage  -v DevHub

Here -k represents a key or password. If we set up this here then while installation of this package we have to give this password.

--codecoverage:

Calculate and store the code coverage percentage by running the Apex tests included in this package version. 

Before you can promote and release a managed or unlocked package version, the Apex code must meet a minimum 75% code coverage requirement. 

Salesforce doesn’t calculate code coverage for org-dependent unlocked packages or for package versions that specify --skipvalidation.

once the package version is created successfully it generates the package URL.

https://login.salesforce.com/packaging/installPackage.apexp?p0=04txxxxxxxxxxxxxx

we can install this package in our salesforce instance before promoting with the help of the above URL or command but it will beta version.


It also adds a package alias with package id in sfdx-project.json as shown below.

{

    "packageDirectories": [

        {

            "path": "force-app",

            "default": true,

            "includeProfileUserLicenses": true,

            "package": "TestPackage ",

            "versionName": "Version 1.0",

            "versionNumber": "1.0.0.NEXT"

        }

    ],

    "namespace": "",

    "sfdcLoginUrl": "https://login.salesforce.com",

    "sourceApiVersion": "50.0",

    "packageAliases": {

        "TestPackage": "0HoXXXXXXXX",

        "TestPackage@1.0.0-1": "04txxxxxxxxxxx"        

    }

} 


Now promote the package:

sfdx force:package:version:promote -p TestPackage@1.0.0-1 -v DevHub 

When you promote the package then it becomes a release version.

Now install with the help of installation URL above or sfdx command

sfdx force:package:install --wait 10 --publishwait 10 --package TestPackage@1.0.0-1 -k test1234 -r -u targetOrgalias

With this, we have successfully created an unlocked package and promote then install it into our target salesforce org where we want to install it.

Note:

Post-install scripts and uninstallation scripts are not supported in unlocked packages.


Reference:

https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_unlocked_pkg_intro.htm

https://trailhead.salesforce.com/content/learn/modules/unlocked-packages-for-customers


No comments:

Post a Comment

What’s the difference between Einstein Article Recommendations and Suggested Articles?

How Does Einstein Article Recommendations Work? Einstein Article Recommendations helps support agents resolve customer cases efficiently by ...