Frequently utilized SFDX commands

   



        The following are frequently used Salesforce DX (SFDX) commands:


Retrieve Specific Metadata from Salesforce Org

1. Retrieve all specified metadata types:  

         sfdx force:source:retrieve -m CustomObject,ApexClass,ApexTrigger,ApexPage,CustomLabels

2. Retrieve specific metadata types:    

  * Apex class

        sfdx force:source:retrieve -m ApexClass  // All ApexClass

        sfdx force:source:retrieve -m ApexClass:MyApexClass // To get specific ApexClass

       

         Aura Component

        sfdx force:source:retrieve -m AuraDefinitionBundle // All Aura Component

sfdx force:source:retrieve -m AuraDefinitionBundle:AuraComponentName   // To get specific AuraDefinitionBundle

  * Custom Tab

        sfdx force:source:retrieve -m CustomTab

        

       * Custom Object

        sfdx force:source:retrieve -m "CustomObject"


  * LWC

        sfdx force:source:retrieve -m LightningComponentBundle

        sfdx force:source:retrieve -m LightningComponentBundle:LWCComponentName    // To get specific LWC

  * Custom Application

        sfdx force:source:retrieve -m CustomApplication

  * Page Layout

        sfdx force:source:retrieve -m "Layout:Account-Retail Account"

    Object Name: Account

            Layout Name: Retail Account


      Retrieve flow with Version

        sfdx force:source:retrieve -m Flow:flowAPINAme-versonNumber

    Example: sfdx force:source:retrieve -m Flow:Send_Expiry_Email-9


      3.  List of all existing Orgs in VS code


       * sfdx force:org:list // to check all connected org in VS code

       * sfdx force:org:list --all // to check all connected org in VS code including Scratch Org

       * sfdx auth:logout -u OrgUserName  // Disconnect particular Org from VS code

       * sfdx force:org:list --clean // To cleanup all orgs which are expired or deleted


     4. Export the Data from Org

      sfdx force:data:tree:export -q "SELECT Name, Id FROM Account" -d ./data  // Export         some  sample data from the org


     5. Import the Data to Org

     * sfdx force:data:tree:import --sobjecttreefiles data/Account.json  // Import sample data to the current associated/default org

   Note: Specify which data you want to import like we are importing Account.json   


     6. Open Salesforce Org from sfdx

     * sfdx force:org:open

     * sfdx force:org:open -u org_alias  // org_alias = Org Alias Name or Org's username


     7. Retrieve all metadata from salesforce org


       Define Package.xml


     Note: Just mention only those components in package.xml that you want to retrieve from         Salesforce Org.


      * Create a "package.xml" folder in VS code and paste the package.xml from above       mentioned link

      * sfdx force:mdapi:retrieve --retrievetargetdir force-app  -k package.xml 

           // This will Retrieve all the metadata which is mentioned in Org under main folder in salesforce  VS code folder. 


  // This command can only be used on orgs that have source tracking enabled, such as sandboxes and scratch orgs.

     sfdx force:source:pull 

     * sfdx force:source:pull -u org_alias

 

     8. Scratch org (Dev Hub should be enabled)


     * sfdx force:org:create -v OrgUserName -s -f config/project-scratch-def.json -d 30 -a ScratchOrgName // Create Scratch org i.e. 

    * sfdx force:org:delete -u orgusername // Delete the Scratch org from VS code

    * sfdx force:user:password:generate // Generate the password for scratch Orgs

    * sfdx force:user:display -u ScratchOrgUserName // You can see the password again for Scratch org by running this.

   

    * sfdx force:source:push -u MyScratchOrg // Push metadata to ScratchOrg

    * sfdx force:source:push -u MyScratchOrg --ignorewarnings --forceoverwrite  // To push changes ignoring errors


    9. Set an alias to Org

     * sfdx force:alias:set newAliasName=OrgUserName


   10. Set a default Org

    * sfdx force:config:set defaultusername=OrgAliasName/OrgUserName // for scratch Org OR Normal Org

    * sfdx force:config:set defaultdevhubusername=OrgAliasName/OrgUserName  // for normal Org


   11. Deploy code to Org

   By using creating a package

    sfdx force:source:convert -d packageName  // packageName, it is the name of package

    sfdx force:mdapi:deploy -d packageName -u usernameofOrg // usernameofOrg, specify where you want to deploy metadata