Sub topic 5
Running your first scan
This chapter assumes you have the Salesforce CLI (sf) installed, a Salesforce DX project with a force-app folder, and the code-analyzer plugin from the previous chapters. You should also know basic Apex.
What a scan does
sf code-analyzer run reads your source files, applies the rules you choose, and reports each violation. A violation is a place where code breaks a rule. The command needs two decisions from you: which files to look at and which rules to apply.
Create a file with a problem
Add this class to force-app/main/default/classes/AccountUpdater.cls. It runs a DML statement (update) inside a loop. Salesforce limits how many DML statements one transaction can run, so this pattern can fail on large data sets.
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
update acc;
}
}
}Run the scan
Run this from the root of your project:
sf code-analyzer run --workspace force-app --rule-selector Recommended --view tableThe flags do the following:
--workspace force-appsets the folder that holds the code to scan.--rule-selector Recommendedapplies the Recommended set, a curated group of rules that suits most projects.--view tableprints the violations as a table in the terminal.
Read the output
Each row describes one violation. The columns that matter most at first are these:
| Column | Meaning |
|---|---|
| Severity | How serious the finding is, from 1 (Critical) to 5 (Info). |
| Rule | The rule that fired, prefixed by its engine, such as `pmd:OperationWithLimitsInLoop`. |
| Location | The file, line, and column of the problem. |
| Message | A short description of what is wrong. |
For AccountUpdater.cls, expect a finding on the update acc; line. The fix is to collect the changed records in a list and run one update after the loop:
public class AccountUpdater {
public static void renameAll(List<Account> accounts) {
for (Account acc : accounts) {
acc.Name = acc.Name + ' Inc.';
}
update accounts;
}
}Run the same command again. The loop finding should be gone.
Narrow a scan
You can restrict a scan to one file with --target, and to one engine's tagged rules with a more specific selector. This command checks one file against PMD rules tagged Security and writes an HTML report:
sf code-analyzer run --workspace force-app --target force-app/main/default/classes/AccountUpdater.cls --rule-selector pmd:Security --output-file results.htmlThe next chapter covers workspace and target in detail.
Common errors
- No workspace found or no files scanned. The path after
--workspaceis wrong or relative to the wrong folder. Run the command from the project root, or pass an absolute path. - A rule selector that matches nothing. Selector names are case sensitive in some positions. List valid names with
sf code-analyzer rules --rule-selector Recommendedand copy from the output. - Expecting a failed command. A scan with violations still exits with status 0 unless you set
--severity-threshold. Do not rely on the exit code in scripts until you set one.
Exercise
You run sf code-analyzer run --workspace force-app --rule-selector Recommended and it prints violations, but echo $? shows 0. Why, and what flag changes this?
Show answer
A scan only returns a nonzero exit status when you ask it to. Add --severity-threshold with a number, for example --severity-threshold 2, and the command exits nonzero when any violation is severity 2 or more severe.
Exercise
Why does moving update acc; out of the loop fix the finding?
Show answer
The original code runs one DML statement per record, so a large list can exceed the governor limit on DML statements per transaction. Collecting records in a list and calling update accounts; once uses a single DML statement regardless of list size.