Salesforce Developer Experience - SFDX

Most Frequently used SFDX Commands

Table of Content

If we already have Salesforce Metadata API, Force.com IDE and other tools then why do we need one more tool like Salesforce DX ?

In tools like Changeset, Metadata API, or Force.com IDE, the source of truth is Sandbox. Although we can set up process and continuous integration (CI) to use some source code management (SCM) like Git or SVN. However, this kind of setup takes time, expertise, and a lot of effort.

Salesforce DX not only solves the above problem but

  • Ability to consider SCM as a source of truth
  • Use any favorite IDE or any SCM
  • Powerful CLI to help minimize the complexity of setting up CI
  • Updated Force.com IDE to support Salesforce DX if you are not comfortable with CLI
  • and most important, spin-off Scratch Orgs within minutes through the script to quickly work on POC or package-based development

What are Scratch Orgs?

Salesforce DX can be enabled for any Salesforce instance and they are known as Developer Hub. One Developer Hub can have multiple Scratch Orgs. Scratch Orgs are temporary Salesforce org which can be created quickly and metadata can be deployed from SCM. These kind of Orgs can be used by developers to perform quick proof of concept or build and test packages. Once the package is built and saved back on SCM, scratch org can be destroyed easily.

If we already have developer orgs or sandboxes, why do we need scratch orgs ?

Because of compliance of most of the clients, we cannot move their code to developer org to perform some POC or package development, so developer org is out of questions anyway.

Now, let’s focus on Sandboxes. The use of sandboxes are mostly for end-to-end testing, integration testing, UAT or training. Sandboxes mostly represent either replica of production or future state of production which might be undergoing user acceptance testing (UAT). When we refresh Sandbox, we don’t get options on which metadata needs to be copied. Assume the situation, there is a defect on production and we need to go back in time and check how the system was behaving previously. Typical rollback scenario for sandboxes. We might have metadata stored somewhere, but we need to perform many iterations of destructive.xml to delete components from Sandbox. In this case, we can quickly spin off scratch org from source code and perform an analysis of historical code.

This is just one example, there are many scenarios and specifically for appexchange development companies.

I think we had lots of talks and are ready to roll now.

Setting up Salesforce DX

Install Heroku CLI and then run the below command

heroku plugins:install salesforcedx

Another way to install SalesforceDX during the pilot is to installer from here (Currently this URL is working however its location is not official). Once downloaded, install it. After installation makes sure the git command is working from the command line.

In Salesforce DX, the source of truth is source control. so we need a repository for the demo. For this blog post, let’s consider this repository. Run the below command to clone this repository

git clone https://github.com/forcedotcom/sfdx-simple cd sfdx-simple

Authorize Salesforce DX to login to Developer Hub Org

Below command will set org as default Developer Hub Org and will set its alias as my-devhub-org. It will open Salesforce login page in default browser for OAuth flow, where we need to login to Developer Hub org and authorize Salesforce DX.

sfdx force:auth:web:login --setdefaultdevhubusername --setalias my-devhub-org

Creating Scratch Org

To create a scratch org in Salesforce DX, we need to have workspace-scratch-def.json . Best place is to place it in config folder. This file already exists in Git repository we cloned initial however we would need to update it with our FirstName, lastName, Email and Org preferences. All supported preferences are listed here in Metadata documentation.

You can find official documentation on sample scratch org definition file and possible configuration values and features.

Below is a sample file

workspace-scratch-def.json

{ 
"Company": "Shivasoft", 
"Country": "US", 
"LastName": "Zaa", 
"Email": "jitendra.Zaa@shivasoft.in", 
"Edition": "Developer", 
"OrgPreferences" : { "S1DesktopEnabled" : true }
 } 

Edition of Org can be Professional, developer or enterprise.

Once workspace-scratch-def.json created in config folder, run below command to create a scratch org. New scratch would be named as jitendra2_scratch and it can be configured by passing parameter to –setalias

Note : At a time of writing this post, scratch orgs auto deleted after 7 days however it may change when product goes GA.

sfdx force:org:create --setdefaultusername -f config/workspace-scratch-def.json --setalias jitendra2_scratch

Moving code to Scratch Org

To move code, we need to define sfdx-workspace.json which contains path of source code needed to be pushed. Below is sample file

simple sfdx-workspace.json

{   
"PackageDirectories": 
  [     
    {       
      "Path": "force-app"     
    }   
  ],   
"Namespace": "",   
"SourceApiVersion": "39.0" 
}

sfdx-workspace.json with some more options

{ 
"Namespace" : "AcmeIncExample", 
"SfdcLoginUrl" : "https://login.salesforce.com", 
"SourceApiVersion": "39.0" ,
"PackageDirectories" : 
  [ 
    { 
      "Path" : "helloWorld", 
      "Default": true
     }, 
      { "Path" : "unpackaged" }, 
      { "Path" : "utils" } 
] 
}

Run below command to push metadata to default scratch org

sfdx force:source:push or sfdx force:source:push --targetusername my-scratch-org@bar.com

Get metadata changes from Scratch Org or Pull changes

We can also get metadata changes done in scratch Org either from UI or deployment. It will only pull if metadata changes, not the whole Org.

sfdx force:source:pull or sfdx force:source:pull -u org_alias

Getting list of all existing Orgs

Before creating scratch org, you may want to know about the existing orgs and their aliases. We can run below command

sfdx force:org:list

Open Salesforce Org from sfdx

Below command can be used to open Org from sfdx command line

sfdx force:org:open or sfdx force:org:open -u org_alias 

Running Test classes using Salesforce DX

To run test classes using Salesforce DX , use below command

sfdx force:apex:test:run

Above command will return job Id, use that ID and run next command for status

sfdx force:apex:test:report -i JOBID_FROM_ABOVE_COMMAND 

Setting up log Levels

Salesforce DX logs are generated at USER_Home_DIR/.sfdx/sfdx.log. We can set it either with each command or globally. By default only Error logs are recorded but we can change log level. To set log level with each command, we can use –loglevel DEBUG. To set log level globally we can use

//windows set SFDX_LOG_LEVEL=DEBUG //Or in unix Export SFDX_LOG_LEVEL=DEBUG

Currently below log levels are supported

  1. ERROR
  2. WARN
  3. INFO
  4. DEBUG
  5. TRACE

Creating skeleton workspace

We can automatically generate skeleton workspace using CLI which will create folder structure and json files with default value.

sfdx force:workspace:create --workspacename mywork OR sfdx force:workspace:create --workspacename mywork --defaultpackagedir myapp

Above command will create below folder structure

Salesforce DX Skeleton Workspace
Salesforce DX Skeleton Workspace

Password for scratch Orgs

When we create a scratch org using sfdx CLI, it does not display password and uses OAuth internally to communicate with scratch org. If we need to login to scratch org from non sfdx CLI, then we would need to generate a password. Below command can be used to generate password.

sfdx force:user:password:generate

Message :

Successfully set the password "26y271a" for user scratchorg1495650731268@shivasoft.in. You can see the password again by running "force:org:describe".

If we want to see password in future, then use below command

force:org:describe

Enable Person Account

Make sure below steps are completed in Salesforce Hub instance before running below sfdx script

  1. Create Record Type on Account
  2. Make sure OWD setting for contact is Controlled by Parent.

Below code in project-scratch-def.json file would create a Person Account

{     
"orgName": "Person Account Scratch Org",     
"edition": "Enterprise",     
"features": ["PersonAccounts"] 
}

Retrieve and Deploy code in Sandbox using SFDX – Old File Format

This blog post goes in detail, however, below is quick command

Retrieve Metadata from Sandbox

sfdx force:mdapi:retrieve -r ./mdAPIZip -u jzaa1 -k src/package.xml 

Deploy Metadata to Sandbox

-- deploy zip file 
sfdx force:mdapi:deploy   -f ../mdAPIZip/unpackaged.zip -u jzaa1 -w 10 
-- or deploy traditional file structure 
sfdx force:mdapi:deploy  -d ../mdAPIZip/unpackaged -u jzaa1 -w 10

Retrieve and Deploy code in Sandbox using SFDX – New Source Format

Deploy metadata to Sandbox or production using new source format

sfdx force:source:deploy -p force-app/main/default

Retrieve metadata from Sandbox of Production using new source format for existing components in default folder

sfdx force:source:retrieve -p force-app/main/default

Retrieve metadata from Sandbox / production on basis of package.xml in manifest folder

sfdx force:source:retrieve -x manifest/package.xml

Set Default Username or Default Devhub

Use below command to set default username for current project. For global use -g at end

$ sfdx force:config:set defaultusername=me@my.org defaultdevhubusername=me@myhub.org
$ sfdx force:config:set defaultdevhubusername=me@myhub.org -g

Convert Package.xml to SFDX format

sfdx force:mdapi:convert --rootdir "manifest"

In above command, –rootdir would be replaced by root path declared in sfdx-project.json

Sample Code Snippet to retrieve metadata from Developer or Sandbox Instance and convert to SFDX source format

echo "Retrieve Metadata from Developer instance"
echo "Command - sfdx force:mdapi:retrieve -r metadata -u pathtocode -k manifest/package.xml"
sfdx force:mdapi:retrieve -r tmp -u pathtocode -k manifest/package.xml
echo "Unzip results"
echo "Command - unzip -o tmp/unpackaged.zip -d manifest"
unzip -o tmp/unpackaged.zip -d manifest
echo "delete zipped result retrieved"
rm tmp/unpackaged.zip
echo "Move unzipped content to folder up"
mv manifest/unpackaged/* manifest 
rm -r manifest/unpackaged
echo "Convert Manifest to SFDX format Source"
sfdx force:mdapi:convert --rootdir "manifest"

Change default SFDX API

Be default SFDX points to latest API of plugin. If I have SFDX installed for prerelease instances, that means it will not work for my developer or sandboxes because they are still on older API. In these scenarios, we can override SFDX API either globally or current project using below command.

sfdx force:config:set apiVersion=44.0 --global
or
sfdx force:config:set apiVersion=44.0

Login to Salesforce using Certificate / JWT

Reference link

sfdx force:auth:jwt:grant 
-r "https://login.salesforce.com" 
-a orgalias 
-f "PathtoKey/server.key" 
-s -u username@salesforce.com -d 
-i "ConsumerKey" --json

Remove entry from org list or logout

sfdx force:auth:logout 
-u <usernamealias> 
-p 

You can also run below command to cleanup all orgs which are expired or deleted

sfdx force:org:list --clean

Install Package / Manage Package or Unlocked Package

sfdx force:package:install -p [packageId] -w 30 -u [scratchOrgName] -k [passwordIfAny]

You can find the list of a few Salesforce AppExchange here.

Create a new Unlocked Package

sfdx force:package:create -n <package_name> -d <Package Description> -t Unlocked -r force-app --json --wait 20 -e --codecoverage

Update Unlocked Package to new Version

sfdx force:package:version:create -p <package name> -d force-app -k <password> -w 10 -v <dev_hub_user_alias> -f config/project-scratch-def.json --releasenotesurl <url> --codecoverage

Example

sfdx force:package:version:create -a "Log framework 2.0 add on Package" -e "Nov 18 2020 release" -p log_framework -d force-app -k jit_opensource_pwd -w 80 -v Prod_unlocked -f config/project-scratch-def.json --releasenotesurl "https://jitendrazaa.com/blog/log_framework=%2FREADME.md&_a=preview" --codecoverage

Create Org Dependent Package

sfdx force:package:create -t Unlocked -r force-app -n <app name> --orgdependent

Update the package name or description

official document link

sfdx force:package:update --package "Expense App" --name "Expense Manager App" \
--description "New Description" --errornotificationusername me2@devhub.org

Promote Package for release

sfdx force:package:version:promote --package <package alias>

Example

sfdx force:package:version:promote --package "log_framework@0.9.0-4" -v Prod_unlocked

Governor Limit information

sfdx force:limits:api:display

Delete a Scratch Org

sfdx force:org:delete -u <org alias>

Posted

in

by


Related Posts

Comments

30 responses to “Most Frequently used SFDX Commands”

  1. Amjad khan Avatar
    Amjad khan

    I am getting the below error while creating scratch org:
    sfdx force:org:create –setdefaultusername -f config/workspace-scratch-def.json –setalias amjad_scratch
    ERROR running force:org:create: No such column ‘WorkspaceType’ on sobject of type SignupRequest.

    1. Jitendra Zaa Avatar

      Are you part of Pilot ? This feature not GA yet, you would need to have access to Developer hub to create scratch org.

      1. Somnath Paul Avatar
        Somnath Paul

        I am getting the below error while running the command “sfdx force:org:create –setdefaultusername -f config/workspace-scratch-def.json –setalias somnath1_scratch”

        You do not have access to the [SignupRequest] object
        In my developer org setup, i can not find environment hub.Is it enabled only for ISVpartners? How to get the access of Environment hub in my developer Org.
        Thanks
        Somnath

  2. Harshit Pandey Avatar

    Glad you lined this up. Long due DX, have been hearing this coming. Wondering, how DX solve problem of connected org and landscape, will that solve or does this add more complexity. Like QA lets say connected to Tableau and O365, so as dev instance linked with Dev(Tableau) and Dev(365) now spinning a template org, while offer new org id which in turn require relinking all integration and variables. What’s your thought on that ?

    1. Jitendra Zaa Avatar

      Not sure if in these scenarios, DX would be of any help or not. Its more on package based deployment. Also, currently it can only be used with scratch orgs and not with sandboxes. So time will tell that can we or not, set these integration related settings automatically.

  3. Abdul Salam Avatar
    Abdul Salam

    I am getting error message while running below command
    org:create –setdefaultusername -f config/workspace-scratch-def.json –setalias abdul_scratch20170614
    ERROR running force:org:create: You do not have access to the [SignupRequest] object.

  4. Vipul Goel Avatar
    Vipul Goel

    https://uploads.disquscdn.com/images/27f28c13abd5e02c378867739e93d26df793420c1364639fa54474fa72fc158b.png Can you please let me know the Source directory so that I can update it to BETA version. I’m stuck

  5. Abdul Salam Avatar
    Abdul Salam

    I am very exited to use this feature. Is there any release date?? In your demo, mentioned that it will be available soon.

  6. Venkat Avatar
    Venkat

    HI,
    i have small doubt. is it possible Creating recurring time-based actions in workflow or process builder. i want fire the my workflow rule every day. is it possible or not?

    Thanks,
    Venkat.

  7. saurabh Avatar
    saurabh

    Hi Jitendra,

    I am getting below error-

    ERROR: You do not have access to the [ScratchOrgInfo] object.

    1. Abhirup Sengupta Avatar
      Abhirup Sengupta

      Setup-> Quick fund-> Search ‘Dev Hub’ -> Enable

  8. Venkatesh R Avatar
    Venkatesh R

    Hi Jitendra,

    I have one query if i enable the Scratch Orgs in DevHub can i use the Developer Sandbox simultaneously or we cannot able to use Developer Sandbox?

    Regards,
    Venkatesh R

    1. Jitendra Avatar

      You can use simultaneously both – Scratch Org and Sandbox

  9. abhik sarkar Avatar

    Hi Jitendra,
    I have a question on the scratch orgs that are created using SFDX. If we need to reproduce an issue by going back to a certain point in time (by getting the code from any SCM tool), & deploying it to a scratch org, it will also need some data setup to be done before performing testing. So my question is is there a way copy data as well while creating the scratch orgs ?

    1. Jitendra Avatar

      You would first need to create a scratch Org and then run a command to load data using SFDX or CLI dataloader. https://www.jitendrazaa.com/blog/salesforce/import-and-export-records-using-salesforce-dx/

  10. Anil Avatar
    Anil

    Hello,

    I am facing an issue with record type migration to scratch org,what would be the best way to dit can you help me ?
    force-app\main\default\objects\Account\compactLayouts\C_CL_Entreprise.compactLayout-meta.xml In field: RecordTypeId – no CustomField named Account.RecordTypeId found

    any help would be greatly appreciated

    1. Adam Avatar
      Adam

      Anil, were you able to resolve this issue? am running into same issue.

      Cheers
      Adam

      1. Kumar Pamarthy Avatar
        Kumar Pamarthy

        I have the same issue still

  11. Asish S Avatar
    Asish S

    Hi Jitendra,

    I have a query related to sfdx:force push. When ever I try to push the code it always consider the whole code. Somehow its not able to identify the delta. Everytime, I need to create a new scratch Org to resolve this issue. Even after creating new scratch org, some times again it starts with same problem. Is there any I can fix this issue without creating a new scratch org.

    Thanks in advance.

    1. Anil Ghattamaneni Avatar
      Anil Ghattamaneni

      Even i need the same. Looks there is no way to send only delta changes to scratch org. pls share your thoughts too.

  12. Revathy Avatar
    Revathy

    Thanks Jitendra for the explanation.
    I am getting the error “‘sfdx’ is not recognized as an internal or external command, operable program or batch file.” after installing the Salesforce CLI and setting the path environment variable.
    Any idea why or how can I resolve this?

    Thanks,
    Revathy

  13. Beatrix Avatar
    Beatrix

    Hi Jitendra,

    I have a question regarding person accounts.
    I activated the features and person accounts are available in my scratch org.
    But the company field is still required on the Lead object.
    In my production org the field is not required and determines if a lead is converted to a person account or a business account.
    Is there any way to activate this functionality in scratch orgs as well?

    Thank you,
    Beatrix

  14. GAURAV SETH Avatar
    GAURAV SETH

    We need to start the development with VS-Code with dev org using version control (Gitlab).

    We have number of developers who will be working to complete the tasks.
    Which approach should we take, scratch or non-scratch org ? or any other preference.
    Please suggest which approach to take.

  15. Kumar Pamarthy Avatar
    Kumar Pamarthy

    Hi Jithendra, just played around this DX today. when the scratch org is created, i don’t see the complete meta from source system is coming up. Is it designed in the same fashion? My understanding towards scratch org is, we will get only standard sf meta data into the scratch org but nothing custom meta data we built. If we need some missing meta data in scratch org for our feature implementation in scratch org, then we need to retrieve the required meta data from source org and push it to scratch org. is my understanding correct? I am really excited to use scratch orgs using command based component development . Thank you

  16. Mahesh Avatar
    Mahesh

    Hello sir, i have a sandbox with existing functional component added to it, I want to retrieve whole metadata of a org using sfdx retrieve command . For that I need to have a package.xml. Is there any way to generate the package.xml that consist the entries to fetch all the metadata from my sandbox.

  17. Prasanna Avatar
    Prasanna

    Hi Jeetendra, I am workin as a QA. Have a query, where those jason files should be created for creating the workspace?
    Thank you.

  18. […] You can refer this blog post for list of all frequently used SFDX commands. […]

  19. […] components while deploying code ? Out of 20+ components, its easy to miss few. In scratch Org, push & pull commands are life saver. It will auto track changes for you. If your code not working as expected or POC got pushed to next […]

  20. BIBHUPRASAD SAHOO Avatar
    BIBHUPRASAD SAHOO

    Could you please help me to get all the meta data which used a particular picklist value inside an org..

  21. Bala Kishore Avatar
    Bala Kishore

    Hai Jithendra,

    I am having the metadata – is there any command to create the manifest file for the respective metadata components ?

    i need the package.xml only for that metadata

Leave a Reply to Abdul SalamCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading