DEV Community

Cover image for SFDX Commands in Action
Gaurav Yadav
Gaurav Yadav

Posted on

SFDX Commands in Action

The Salesforce CLI is a powerful command line interface that simplifies development and build automation when working with your Salesforce org.
Over the past few years I am using multiple sfdx commands from deploying metadata to running code snippet using sfdx command.

> Here is the consolidated list of all commands which are needed by developer during complete development life cycle


1. Retrieve code/metadata from org

How to retrieve source from an org?

sfdx commands are very flexible different types of metadata can be retrieved together.

//Example 1: Retrieve single file
sfdx force:source:retrieve -p force-app\main\default\triggers\AccountTrigger.trigger

//Example 2: Retrieve multiple file
sfdx force:source:retrieve -p force-app\main\default\triggers\AccountTrigger.trigger,force-app\main\default\classes\TriggerHandler.cls

Enter fullscreen mode Exit fullscreen mode

2. Deploy code/metadata to org

How to deploy source to an org?

sfdx commands are very flexible different types of metadata can be deployed together.

//Example 1: Deploy single file
sfdx force:source:deploy -p force-app\main\default\triggers\AccountTrigger.trigger

//Example 2: Deploy multiple file
sfdx force:source:deploy -p force-app\main\default\triggers\AccountTrigger.trigger,force-app\main\default\classes\TriggerHandler.cls

Enter fullscreen mode Exit fullscreen mode

3. Run Test class

Can we run test class or test method using sfdx commands?

Yes, here are few useful options using sfdx commands

//Running complete test class
sfdx force:apex:test:run -n "TestClass1,TestClass2"

//Another Way
sfdx force:apex:test:run -t "TestClass1,TestClass2"

//Run only specified test methods
sfdx force:apex:test:run -t "TestClass1.method1,TestClass2.method2"
Enter fullscreen mode Exit fullscreen mode

4. Execute code snippet

Definitely I do need to open developer console for executing code snippets?

No, sfdx command is here for executing code snippets directly from terminal. 😀

sfdx force:apex:execute
Enter fullscreen mode Exit fullscreen mode

5. Check logs using sfdx command

How can I see logs stream in VSCode terminal?

Here is the sfdx command…

sfdx force:apex:log:tail
Enter fullscreen mode Exit fullscreen mode

6. Login to org directly from cli

Did you forgot the password of sandbox? But you don’t want to reset it, since this sandbox is already setup and authorised by couple of other 3rd party applications like vscode,xl connector, data loader, metazoa, etc.

You are lazy like me to update password in all of these applications. Here is the sfdx command to rescue us.

sfdx force:org:open -u orgAlias
Enter fullscreen mode Exit fullscreen mode

Happy Learning !!!

Top comments (0)