top of page
Writer's pictureM. Gupta

New Salesforce CLI Commands for Better Deployment Control

Updated: 7 days ago



Mastering deployment management in Salesforce has never been easier! With the introduction of new SF CLI commands, you now have more precision and control over your deployment workflows. Whether you're authorizing an org, setting an alias, or configuring your default Dev Hub, these updated commands simplify the process for both developers and administrators.

These new commands eliminate complexity, enhance efficiency, and give you unparalleled control over your deployment pipeline. Dive in and see how the modern Salesforce CLI can transform your deployment strategies with just a few streamlined commands!


Warning:


Some of the SFDX command is deprecated and will be removed by November 2024. Use the SF command instead.


The major commands that you will need to focus on are below:

The functionality of these six deprecated sfdx-style commands.

  • sfdx force:source:push

  • sfdx force:source:pull

  • sfdx force:source:deploy

  • sfdx force:source:retrieve

  • sfdx force:mdapi:deploy

  • sfdx force:mdapi:retrieve


These sf-style commands support source tracking.

  • sf project deploy start

  • sf project delete source

  • sf project retrieve start


  1. Authorize an Org, Set an Alias, and Make it the Default Dev Hub


Authorizing a Salesforce org and setting it as the default Dev Hub is a foundational step in managing Salesforce development and deployment. Here's how to accomplish this using both the SFDX CLI and the new SF CLI.


  • SFDX CLI:

sfdx force:auth:web:login -a DevHub -d

  • SF CLI:


  1. Run the command with no flags to open the default Salesforce login page (https://login.salesforce.com):

sf org login web

  1. Log in to your Dev Hub, set it as your default Dev Hub, and set an alias that you reference later when you create a scratch org:

sf org login web --set-default-dev-hub --alias DevHub11
  1. List of all orgs we have created or authenticated to VSCode


Listing all orgs you've created or authenticated to is an essential step for managing your Salesforce environments. Here's how to accomplish this using both the SFDX CLI and the new SF CLI.


  • SFDX CLI:

sfdx force:config:list

  • SF CLI:


  1. Run the command with no flags to open the default Salesforce login page (https://login.salesforce.com):

sf org login web

  1. Log in to your Dev Hub, set it as your default Dev Hub, and set an alias that you reference later when you create a scratch org:

sf org login web --set-default-dev-hub --alias DevHub11

  1. List orgs and remove local org authorization info about non-active scratch orgs:

sf org list --clean

  1. How to Create a Scratch org


Creating a scratch org is simple and flexible. You can either use a definition file with all the settings or specify the edition directly with the --edition flag. Here’s how you can customize your scratch org:


Remember: You’ll need a Dev Hub org to create a scratch org. Set it as your default or use the --target-dev-hub flag to specify it.


  • SFDX CLI:


The --durationdays flag lets you decide how long the scratch org will exist, anywhere between 1 and 30 days. The --description option allows you to add a short note about the purpose of the scratch org, which can be helpful for keeping things organized. The --edition flag specifies the type of org you want to create, like Developer or Enterprise, depending on the features you need 

sfdx force:org:create -s -f config/project-scratch-def.json -a TestScratchOrg1 --durationdays 7

Warning:


  • SF CLI:


  1. Create a Developer edition scratch org using your default Dev Hub and give the scratch org an alias:

sf org create scratch --edition developer --alias TestScratchOrg2UsingSF

  1. Create a scratch org with a definition file. Specify the Dev Hub using its alias, set the scratch org as your default, and specify that it expires in 30 days:

sf org create scratch --edition developer --alias TestScratchOrg3UsingSF --definition-file config/project-scratch-def.json --set-default --duration-days 30 

  1. How to Delete a Scratch org


Deleting a scratch org helps keep your environment clean and organized. You can use the SFDX CLI(deprecated) or SF CLI to quickly remove any scratch org you no longer need.


  • SFDX CLI:

sfdx force:org:delete -u TestScratchOrg3UsingSF -p

Warning:

  • SF CLI:

sf org delete scratch --target-org <Scratch Org Alias>

  1. Generate a Random password


By default, new scratch orgs contain one admin user with no password. Use this command to generate or change a password for this admin user. After it's set, you can’t unset a password, you can only change it.


  • SFDX CLI:


The -u flag specifies the alias or username of the scratch org.

sfdx force:user:password:generate -u TestScratchOrg4UsingSF

Warning:


  • SF CLI:

Generate a password for the original admin user of the scratch org with alias "TestScratchOrg5UsingSF".

sf org generate password --target-org TestScratchOrg5UsingSF

  1. How to Authorize a Scratch Org


Logging into an org allows the CLI to run commands that interact with that org, like deploying or retrieving metadata. You can log into various types of orgs, including sandboxes, Dev Hubs, Environment Hubs, production orgs, and scratch orgs.


  • SFDX CLI:


The  -d  flag sets the org as the default for the current workspace, while the -a <alias_name> flag assigns an alias to the org for easy reference.

sfdx force:auth:web:login -d -a TestScratchOrg5UsingSF

sf org login web

  1. Log in to your Dev Hub, set it as your default Dev Hub, and set an alias that you reference later when you create a scratch org:

sf org login web --set-default-dev-hub --alias DevHub 
  1. Log in to a sandbox and set it as your default org:

sf org login web --instance-url https://MyDomainName--SandboxName.sandbox.my.salesforce.com --set-default
  1. Log in to your Dev Hub, set it as your default Dev Hub, and set an alias that you reference later when you create a scratch org:

sf org login web --instance-url https://MyDomainName--SandboxName.sandbox.my.salesforce.com --set-default --browser chrome
  1. How to Open an Org using SFDX OR SF command


Open your default scratch org, or another specified org, in a browser. Here's how to accomplish this using both the SFDX CLI and the new SF CLI.


  • SFDX CLI:


Open your default scratch org, or another specified org, in a browser.

sfdx force:org:open

  • SF CLI:


  1. Open your default org in your default browser:

sf org open
  1. Open your default org in an incognito window of your default browser:

sf org open --private
  1. Open the org with alias “TestScratchOrg5UsingSF” in the Firefox browser or Chrome(use chrome):

sf org open --target-org TestScratchOrg5UsingSF --browser firefox
  1. How to Create an Apex Class or Apex Trigger for Default Org


Creating an Apex class or Apex Trigger in your default org is easy with the SFDX or SF CLI. These commands allow you to quickly generate a new class in your Salesforce environment, enabling you to add logic and functionality immediately.

Note: Use trigger instead of class to generate apex trigger.


  • SFDX CLI:


The  -d flag sets the org as the default for the current workspace, while the -a <alias_name> flag assigns an alias to the org for easy reference.

sfdx force:apex:class:create -n SampleClass -d force-app/main/default/classes
  • SF CLI:


  1. Generate two metadata files associated with the MyClass Apex class (MyClass.cls and MyClass.cls-meta.xml) in the current directory:

sf apex generate class --name SampleClassUsingSF

  1. Similar to previous example, but generates the files in the "force-app/main/default/classes" directory

sf apex generate class --name SampleClassUsingSF --output-dir force-app/main/default/classes

  1. How to Create an Aura Component or LWC Component for Default Org


Creating an Aura component or Lightning Web Component (LWC) for your default org is simple using the SFDX or SF CLI. These commands allow you to generate the required component files, so you can start building your UI or logic.


  • SFDX CLI:


  1. Use the following SFDX command to create an Aura component:

sfdx force:lightning:component:create --type aura --name SampleAuraComponent -d force-app/main/default/aura

  1. Use the following SFDX command to create an LWC component:

sfdx force:lightning:component:create --type lwc --name SampleLWC -d force-app/main/default/lwc

  • SF CLI:


  1. Generates the bundle in the specified directory or the current working directory. The bundle consists of multiple files in a directory with the designated name. Lightning web components are contained in the directory with name "lwc", Aura components in "aura".


Note:  To generate a Lightning web component, pass --type lwc to the 

command. If we don’t specify --type, Salesforce CLI generates an Aura component by default.

sf lightning generate component --name SampleAuraUsingSF -d force-app/main/default/aura

  1. Generate a Lightning web component bundle in the "force-app/main/default/lwc" directory:

sf lightning generate component --name SampleLwcUsingSF --type lwc --output-dir force-app/main/default/lwc

  1. How to Deploy an Apex Class, Apex Trigger, Aura Component, LWC Component to the Default Org


Deploying an Apex class, Apex Trigger, Aura Component, LWC Component to our default org is straightforward with both SFDX and SF CLI. Here's how we can do it:


  • SFDX CLI:

sfdx force:source:deploy -p force-app/main/default/classes/SampleClass.cls

Warning:


  • SF CLI:


  1. To deploy an Apex class using SF CLI, use this command:

sf deploy metadata -m ApexClass:SampleClassUsingSF
  1. To deploy an Apex Trigger using SF CLI, use this command:

sf deploy metadata -m ApexTrigger:<TriggerName>

  1. To deploy an Aura Component using SF CLI, use this command:

sf deploy metadata -m AuraDefinitionBundle:<ComponentName>

  1. To deploy an LWC Component using SF CLI, use this command:

sf deploy metadata -m LightningComponentBundle:<ComponentName>


Conclusion:

Mastering Salesforce deployments just got a whole lot easier! With the new and improved Salesforce CLI (SF CLI) commands, you have the power to streamline your development and deployment processes like never before. Say goodbye to the complexity of outdated methods and hello to precision, control, and efficiency.

From authorizing and managing your orgs to cleaning up inactive scratch orgs with a simple command, the SF CLI empowers developers and admins to work smarter, not harder. As the SFDX CLI retires in November 2024, now is the perfect time to embrace the modern and enhanced capabilities of the SF CLI.

Take charge of your Salesforce development journey today and elevate your deployment strategies to the next level. The future of seamless deployment control is here—don't miss out!


References:



Blog Credit:

M. Gupta

D. Khare

 Salesforce Developer

   Avenoir Technologies Pvt. Ltd.

  Reach us: team@avenoir.ai


Recent Posts

See All

Commenti


bottom of page