0% found this document useful (0 votes)
63 views18 pages

DEX450 CheatSheet

This document provides a cheat sheet for using the VS Code IDE and Salesforce CLI to develop with Apex. It outlines commands for executing anonymous Apex, getting debug logs, running tests, and creating various component types like classes, triggers, and Visualforce pages. The Org Browser in VS Code allows viewing org metadata without a package.xml, and anonymous blocks allow executing Apex code without storing it in metadata. Debug logs record system processes and errors, and can be viewed in VS Code or through CLI commands like tailing the logs.

Uploaded by

David rodrig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views18 pages

DEX450 CheatSheet

This document provides a cheat sheet for using the VS Code IDE and Salesforce CLI to develop with Apex. It outlines commands for executing anonymous Apex, getting debug logs, running tests, and creating various component types like classes, triggers, and Visualforce pages. The Org Browser in VS Code allows viewing org metadata without a package.xml, and anonymous blocks allow executing Apex code without storing it in metadata. Debug logs record system processes and errors, and can be viewed in VS Code or through CLI commands like tailing the logs.

Uploaded by

David rodrig
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

VS Code and Salesforce CLI Cheat Sheet

Table of Contents

Use the Command Palette in VS Code........................................................................................................... 3

Find files in VS Code ....................................................................................................................................... 3

Use the Context Menu in VS Code Explorer .................................................................................................. 4

Use the Org Browser in VS Code.................................................................................................................... 5

Learn About Anonymous Blocks .................................................................................................................... 6

Execute Anonymous Apex with Editor Contents in VS Code ......................................................................... 6

Execute Anonymous Apex with Selected Text in VS Code ............................................................................ 6

Execute Anonymous Apex from the CLI ........................................................................................................ 6

Learn About Debug Logs ................................................................................................................................ 7

Get Apex Logs in VS Code .............................................................................................................................. 7

Get Apex Logs from the CLI ........................................................................................................................... 7

Tail the Debug Logs from the CLI ................................................................................................................... 8

Learn About Replay Debugger ....................................................................................................................... 9

Enable Replay Debugger Logs ........................................................................................................................ 9

Disable Replay Debugger Logs ....................................................................................................................... 9

Set a Breakpoint for Replay Debugger in VS Code ......................................................................................10

Set a Checkpoint for Replay Debugger in VS Code ......................................................................................10

Run Apex Tests in VS Code ..........................................................................................................................11

Toggle Code Coverage in VS Code ...............................................................................................................12

Run Apex Tests from the CLI ........................................................................................................................12

Create an Apex Class in VS Code .................................................................................................................13


Exercise Guide
Programmatic Development Using Apex and Visualforce

Create an Apex Class from the CLI ...............................................................................................................13

Create an Apex Trigger in VS Code ..............................................................................................................14

Create an Apex Trigger from the CLI ...........................................................................................................14

Create an Aura Component in VS Code .......................................................................................................15

Create an Aura Component from the CLI ....................................................................................................15

Create a Lightning Web Component in VS Code .........................................................................................16

Create a Lightning Web Component from the CLI ......................................................................................16

Create a Visualforce Page in VS Code ..........................................................................................................17

Create a Visualforce Page from the CLI .......................................................................................................17

Open a Visualforce Page from the CLI .........................................................................................................17

Work with Prettier in your Project ..............................................................................................................18

©Copyright 2020 salesforce.com, inc. All rights reserved. 2


Exercise Guide
Programmatic Development Using Apex and Visualforce

Use the Command Palette in VS Code

Open the VS Code Command Palette using either of the following methods:

Use your mouse to select View | Command Palette.


Use your keyboard to type one of the following shortcuts:

i. Windows: Ctrl + Shift + P


ii. Mac: Cmd + Shift + P

A dialogue will open up, starting with a caret >.


Type to filter the list, use your mouse or keyboard to select a command.

Find files in VS Code

If you remove the leading > from the Command Palette, it turns into a file finder. This can also be
accessed using Ctrl + P or Cmd + P

©Copyright 2020 salesforce.com, inc. All rights reserved. 3


Exercise Guide
Programmatic Development Using Apex and Visualforce

Use the Context Menu in VS Code Explorer

Right-clicking files and directories in the Explorer view makes various context-sensitive options available.
The Salesforce Extensions Pack for VS Code adds options that are specific to Salesforce.

In the screenshot below, the user has clicked the force-app/main/default/classes directory
and the option SFDX: Create Apex Class is available among others. Different directories give different
options. For instance, the lwc directory allows you to create a Lightning web component. You can also
right-click individual files for their own options.

©Copyright 2020 salesforce.com, inc. All rights reserved. 4


Exercise Guide
Programmatic Development Using Apex and Visualforce

Use the Org Browser in VS Code

Reference: Org Browser

The Org Browser view allows you to retrieve individual components or all components for a given
metadata type without needing a package.xml. The org browser is only available for orgs that do not
have source tracking.

©Copyright 2020 salesforce.com, inc. All rights reserved. 5


Exercise Guide
Programmatic Development Using Apex and Visualforce

Learn About Anonymous Blocks

Reference: Anonymous Blocks

An anonymous block is Apex code that does not get stored in the metadata, but that can be compiled
and executed.

Execute Anonymous Apex with Editor Contents in VS Code

Execute the entire contents of your editor as an anonymous block.

Run the following from the Command Palette:


> SFDX: Execute Anonymous Apex with Editor Contents

Execute Anonymous Apex with Selected Text in VS Code

Execute only the highlighted text as an anonymous block.

Highlight the Apex Code that you wish to execute.


Run the following from the Command Palette:
> Execute Anonymous Apex with Currently Selected Text

Execute Anonymous Apex from the CLI

Reference: force:apex:execute

Use the force:apex:execute command to execute Apex code entered on the command line or
Apex code that exists in a local file.

©Copyright 2020 salesforce.com, inc. All rights reserved. 6


Exercise Guide
Programmatic Development Using Apex and Visualforce

Learn About Debug Logs

Reference: Debug Log

A debug log can record database operations, system processes, and errors that occur when executing a
transaction or running unit tests. Learn more at the reference link above.

Get Apex Logs in VS Code

Fetch the list of Apex debug logs using this command:


> SFDX: Get Apex Debug Logs...

Select which Apex log you would like to download. They are presented in order of most recent to
least recent.

Get Apex Logs from the CLI

References: View Apex Debug Logs, apex commands

Get the list of log files using force:apex:log:list


View a debug log by passing its ID to the force:apex:log:get command.

©Copyright 2020 salesforce.com, inc. All rights reserved. 7


Exercise Guide
Programmatic Development Using Apex and Visualforce

Tail the Debug Logs from the CLI

Reference: apex commands

The force:apex:log:tail command activates debug logging and displays logs in the terminal. It
will tail logs from your target org for 30 minutes. If a DEVELOPER_LOG trace flag does not exist, it
automatically creates one in the target org.

Use the -c flag to highlight important log lines: sfdx force:apex:log:tail -c

Use a pipe to feed the output into the appropriate utility for your shell - either findstr, Select-
String, or grep - if you wish to filter the log. In this example, we filter for lines that contain
USER_DEBUG.

Windows CMD: sfdx force:apex:log:tail | findstr USER_DEBUG


Powershell: sfdx force:apex:log:tail | Select-String USER_DEBUG
Mac: sfdx force:apex:log:tail | grep USER_DEBUG

©Copyright 2020 salesforce.com, inc. All rights reserved. 8


Exercise Guide
Programmatic Development Using Apex and Visualforce

Learn About Replay Debugger

Reference: Apex Replay Debugger


Practice on Trailhead: Find and Fix Bugs with Apex Replay Debugger

Replay debugger allows you to replay the steps of an Apex debug log and use traditional tools like
breakpoints and variable inspectors.

Enable Replay Debugger Logs

Enable the Replay Debugger by executing the following from the Command Palette:
> SFDX: Turn On Apex Debug Log for Replay Debugger

This will ensure that there is an active trace flag in your org with a log level of FINER or FINEST
for log category Visualforce and a log level of FINEST for log category Apex Code.
Confirm it worked by looking in the status bar (blue bar at bottom of VS Code) and verifying that it
says something like "Recording detailed logs until 10:02 PM".

Disable Replay Debugger Logs

Disable the Replay Debugger logs by executing the following from the Command Palette:
> SFDX: Turn Off Apex Debug Log for Replay Debugger

Confirm it worked by looking in the status bar (blue bar at bottom of VS Code) at observing that it
no longer mentions anything about detailed logs.

©Copyright 2020 salesforce.com, inc. All rights reserved. 9


Exercise Guide
Programmatic Development Using Apex and Visualforce

Set a Breakpoint for Replay Debugger in VS Code

Set a breakpoint on a line of code by doing one of the following:

Place your cursor on the line and press F9.


Place your cursor to the left of the line and click when you see a dark red dot.
Go to Run | Toggle Breakpoint.

Set a Checkpoint for Replay Debugger in VS Code

Set a checkpoint on a line of code by executing the following from the Command Palette:
> SFDX: Toggle Checkpoint

Alternatively, you can do the following:

Right-click the left of the line where you wish to add the checkpoint.
Select "Add Conditional Breakpoint…".
Enter checkpoint for the Expression.

After setting the checkpoint, or any time you modify your Apex code in the same file, run the
following command to sync your checkpoint with the org to make sure the heap dump occurs:
> SFDX: Update Checkpoints in Org

©Copyright 2020 salesforce.com, inc. All rights reserved. 10


Exercise Guide
Programmatic Development Using Apex and Visualforce

Run Apex Tests in VS Code

Open Workspace Settings from Command Palette:


> Preferences: Open Workspace Settings

Type coverage in the search box and enable Retrieve-test-code-coverage.


Open the Test View by running the following Command Palette command:
> Test: Focus on Apex Tests View

In the list of Apex Tests, mouse over a class or test to reveal a green arrow.
If a test is missing, click the Refresh Tests button (circular arrow).
Click the arrow on a class row (run all tests in class) or a method row (run single test).
You can also run all tests in your org by clicking the green arrow in the TEST: APEX TESTS header.

©Copyright 2020 salesforce.com, inc. All rights reserved. 11


Exercise Guide
Programmatic Development Using Apex and Visualforce

Toggle Code Coverage in VS Code

After running a test with the Retrieve-test-code-coverage option enabled in your VS Code settings, open
the corresponding Apex class file and press the button in the Status Bar to toggle code coverage on and
off.

Run Apex Tests from the CLI

Reference: apex commands


Use the force:apex:test:run command. By default, it runs all Apex tests in the org’s namespace.
Check the reference to see how to run specific tests classes, suites, or methods.

©Copyright 2020 salesforce.com, inc. All rights reserved. 12


Exercise Guide
Programmatic Development Using Apex and Visualforce

Create an Apex Class in VS Code

Either run the following from the Command Palette:


> SFDX: Create Apex Class

or right click the force-app/main/default/classes directory and select SFDX: Create Apex Class.
Enter a name for the class.
A list of directories will be presented. In most cases you will simply press Enter to accept the
default selection.

Create an Apex Class from the CLI

Reference: Create an Apex Class

Use the force:apex:class:create command to create a class from the CLI.

©Copyright 2020 salesforce.com, inc. All rights reserved. 13


Exercise Guide
Programmatic Development Using Apex and Visualforce

Create an Apex Trigger in VS Code

Either run the following from the Command Palette:


> SFDX: Create Apex Trigger

or right click the force-app/main/default/triggers directory and select SFDX: Create Apex Trigger.
Enter the name of the trigger that you would like to create.
A list of directories will be presented. In most cases you will simply press Enter to accept the
default selection.

Create an Apex Trigger from the CLI

Reference: Create an Apex Trigger

Use the force:apex:trigger:create command to create a trigger from the CLI.

©Copyright 2020 salesforce.com, inc. All rights reserved. 14


Exercise Guide
Programmatic Development Using Apex and Visualforce

Create an Aura Component in VS Code

Reference: Create Lightning Apps and Aura Components

Either run the following from the Command Palette:


> SFDX: Create Aura Component

or right click the force-app/main/default/aura directory and select SFDX: Create Aura Component.
Enter a name for the Aura component.
A list of directories will be presented. In most cases you will simply press Enter to accept the
default selection.
A directory will be created. This directory is known as the component bundle and will have every
possible file needed. You may wish to delete the files that you know you won't need.

Create an Aura Component from the CLI

Reference: Create Lightning Apps and Aura Components


Use the force:lightning:component:create command to create an Aura component from
the CLI.

©Copyright 2020 salesforce.com, inc. All rights reserved. 15


Exercise Guide
Programmatic Development Using Apex and Visualforce

Create a Lightning Web Component in VS Code

Either run the following from the Command Palette:


> SFDX: Create Lightning Web Component

or right click the force-app/main/default/lwc directory and select SFDX: Create Lightning Web
Component.
Enter a name for the Lightning web component.
A list of directories will be presented. In most cases you will simply press Enter to accept the
default selection.
A directory will be created with the template, controller, and metadata file.

Create a Lightning Web Component from the CLI

Reference: Create Lightning Web Components

Use the force:lightning:component:create command to create a Lightning web component


from the CLI.

©Copyright 2020 salesforce.com, inc. All rights reserved. 16


Exercise Guide
Programmatic Development Using Apex and Visualforce

Create a Visualforce Page in VS Code

Either run the following from the Command Palette:


> SFDX: Create Visualforce Page

or right click the force-app/main/default/pages directory and select SFDX: Create Visualforce Page.
Enter the name of the VF page that you would like to create.
A list of directories will be presented. In most cases you will simply press Enter to accept the
default selection.

Create a Visualforce Page from the CLI

Reference: visualforce Commands

Use the force:visualforce:page:create command to create a Visualforce page from the CLI.

Open a Visualforce Page from the CLI

Run the following CLI command, omitting the leading "/" if you are using Powershell:
sfdx force:org:open -p "/apex/yourPageName"

©Copyright 2020 salesforce.com, inc. All rights reserved. 17


Exercise Guide
Programmatic Development Using Apex and Visualforce

Work with Prettier in your Project

Prettier is a popular code formatter. It is recommended but not required for class.

Requirements

Using Prettier requires Node.JS and NPM to be installed.


It is implemented as a node module that can be installed locally or globally.
It includes native support for many popular languages including HTML and JavaScript, but you
must install the prettier-plugin-apex node module if you wish to format Apex code.
It can be configured using a .prettierrc file.

Helpful links:

Node.JS and NPM installation instructions.


Prettier section of the Salesforce Extensions for VS Code User Guide.
Apex Plugin README on GitHub.
About .prettierrc configuration files.

Notes specific to the DEX450 project:

In DEX450 Exercise 1-2, there is an optional step for setting up Prettier to work with Apex. Your
project already has an appropriately configured package.json. Prettier is set up in the
devDependencies and an entry has been created in the scripts block. When you run
npm install as advised in the instructions, you are installing both the prettier and prettier-
plugin-apex modules locally.
We provided a sample .prettierrc in the project. If you decide to change it, you can
format all files in your project at once by running the following command from the command
line at the root of your project.
npm run prettier -- --write "**/*.{trigger,cls,js,html}"

If you elect to install the prettier Apex plugin globally, you will need to enable the Prettier:
Resolve Global Modules setting in VS Code, but you will see that VS Code warns of a possible
performance impact.

©Copyright 2020 salesforce.com, inc. All rights reserved. 18

You might also like