Yes, Salesforce’s native code scanning tools help you find security flaws and compliance risks in your Apex, Visualforce, and Lightning Web Component code—without needing third-party plugins. These tools catch issues like SQL injection, hardcoded secrets, and outdated cryptography, all while integrating seamlessly into your dev workflow. Unlike manual audits or external tools, Salesforce Code Security Scan and Static Application Security Testing (SAST) run automatically during deployments, catching problems early. You’ll need access to Salesforce Developer Editors (org), an installed IDE (like VS Code with the Salesforce Extension), or Salesforce Lightning Web Components (LWC) support in a recent release. No advanced DevOps skills required, but familiarity with Salesforce’s metadata model will be helpful.

What You'll Need

  • Salesforce Developer Edition org (or a sandbox with access to Code Security Scan)
  • Salesforce Extension for VS Code (or the Salesforce CLI) if working locally
  • Salesforce IDE plugin (for Eclipse or IntelliJ)
  • Code containing metadata (Apex classes, triggers, LWC components, or Visualforce pages)
  • Internet connection to check Salesforce’s static analysis servers
  • Salesforce Release Notes—some features require org version 58.0+
*Download links are provided in the troubleshooting section. These tools require specific setup; see the "Troubleshooting" section if they don’t install.

Step-by-Step

  1. Enable Code Security Scan in your org
    1. Go to Setup > Security Controls > Code Security.
    2. Toggle on Enable static application security testing (SAST).
    3. Save settings. The scan runs automatically during deployment—no manual trigger needed.
    4. If using a sandbox, ensure it’s updated to the latest release (check Setup > About for version).

    Labels may vary slightly by region: In some orgs, the path is Setup > Security > Code Security.

  2. Write or modify code with security best practices
    1. Use parameterized queries in Apex (avoid raw SQL)
    2. Encrypt sensitive data with Salesforce’s built-in encryption (e.g., `String.encrypt()`)
    3. Never hardcode secrets (API keys, passwords) in code—use Platform Encryption or a secrets manager
    4. For Lightning Web Components (LWC), avoid direct DOM manipulation with unsafe methods like outerHTML or innerHTML.
  3. Run a manual scan via VS Code (if configured)
    1. Open the Salesforce Extension in VS Code and select your org.
    2. Create or edit a metadata file (e.g., an Apex class) in the Salesforce project folder.
    3. In the VS Code sidebar, click Run Code Security Scan (a blue "scan" icon).
    4. The tool highlights issues in the editor with severity levels (high/medium/low).
    5. Click Fix Issues to auto-generate fixes (e.g., parameterized queries).

    If VS Code doesn’t show the scan button, ensure the Salesforce CLI is installed and the extension is properly licensed. See "Troubleshooting" for setup issues.

  4. Deploy code to Production or a Test org
    1. Push your changes to a deployed org using the Salesforce CLI or VS Code > Deploy.
    2. Wait for the deployment to complete. Salesforce’s built-in scan will flag issues in deployed code.
    3. Review the Deployment Summary in the org (Setup > Deploy > Recent Deployments) for warnings.
    4. For LWC, ensure the scan runs in the LWC Developer Sandbox (if enabled in project settings).

    Deploys to Production trigger the full scan. Sandboxes may require manual triggering via the CLI or API.

  5. Review and resolve findings
    1. Navigate to Setup > Developer Console > Code Security Issues to see scan results.
    2. Click on each issue to see details, such as:
      • Severity level (e.g., High for SQL injection)
      • Recommended fix (e.g., "Replace raw SQL with Database.runQuery")
      • Example code snippets for fixes.
    3. Apply fixes immediately—some issues can lead to data breaches or compliance violations.

If That Did Not Work

If the scan fails to trigger or shows no issues, check these steps:
  1. Verify Code Security is enabled (Setup > Security Controls > Code Security).
  2. Ensure your org is on a recent release (58.0+). Check Setup > About for version.
  3. If using VS Code, restart the extension and ensure the org is selected.
  4. Run a manual CLI scan:
    1. Install Salesforce CLI via npm install -g @salesforce/cli.
    2. Authenticate: sfdx force:auth:web:login.
    3. List components: sfdx force:mdapi:decompile -u [orgName].
    4. Scan package: sfdx force:mdapi:scan -u [orgName] --force.
  5. For LWC components, ensure the LWC Developer Sandbox is enabled in the component’s manifest.json.

Common Mistakes

  • Skipping manual scans: The built-in scan misses dynamic code changes or LWC logic. Always run VS Code scans for changes.
  • Ignoring low-severity issues: Medium/High issues can lead to breaches; fix all warnings immediately.
  • Using outdated code: Salesforce’s scan flags deprecated methods (e.g., SOQLQuery replaced with Database.query).
  • Misconfiguring CLI tools: If the CLI scan fails, verify the org is connected and the CLI is updated: sfdx plugins:install @salesforce/cli-codegen

Tips

  • Integrate with CI/CD: Trigger scans in a Jenkins or GitHub Actions pipeline to catch issues before deployment.
  • Use the Salesforce CLI regularly: CLI scans are faster than the browser-based interface and handle large orgs better.
  • Review LWC-specific issues: The scan looks for unsafe JS methods (e.g., document.write) in LWC. Use renderedHTML instead.
  • Enable "Code Coverage" for critical components to ensure logic is tested alongside security.
  • Test in a sandbox first: Deploy fixes to a sandbox before going to Production to avoid breaking changes.
  • Check Salesforce Release Notes for new scan features. Some fixes only appear in newer org versions.

Troubleshooting

  1. Scan shows no issues
    1. Run a manual CLI scan again: sfdx force:mdapi:scan -u [orgName] --force
    2. Check if Code Security is enabled (Setup > Security Controls > Code Security).
    3. If using VS Code, ensure the Salesforce Extension is updated to the latest version (check for updates in the extension menu).
    4. For LWC, verify the component is deployed to a sandbox with LWC support enabled.
  2. VS Code extension not installing
    1. Download the Salesforce Extension for VS Code manually.
    2. Restart VS Code after installing.
    3. If blocked by antivirus: Whitelist VS Code and the extension files.
  3. Deployment fails after fixes
    1. Re-deploy with sfdx force:source:deploy -u [orgName] --targetOrg [targetOrg].
    2. Check the deployment logs for errors (Setup > Monitor > Deployments).
    3. If the issue persists, reset the component to a clean version using: sfdx force:source:convert -u [orgName] --path [componentPath]
  4. LWC scan errors
    1. Ensure the LWC is deployed to a sandbox with LWC support (check Setup > Developer Console > LWC Components).
    2. Add "developerSandbox": true to the component’s manifest.json.
    3. Run the scan again: sfdx force:mdapi:scan -u [orgName] --component [componentName]

Frequently Asked Questions

What if my org is too old to run Code Security Scan?

The scan requires Salesforce org version 58.0 or later. Upgrade to a recent release by creating a new sandbox (Setup > Create > Sandbox) and migrating critical components. For urgent issues, enable Code Security in an older org temporarily with Setup > Developer Settings > Enable Experimental Features (if available).

Does Code Security Scan work for Visualforce pages?

Yes, but Visualforce is less frequently scanned than Apex/LWC. For best results, wrap Visualforce in a framework like Lightning Components Controller** or use SOQL securely with Database.query. The scan flags deprecated Visualforce methods like pageReference parameters.

How do I fix a "Hardcoded Secret" warning?

Replace hardcoded secrets with:

  • Platform Encryption (for API keys): Use String.encrypt() or Salesforce’s Platform Encryption** service.
  • Secrets Manager** (for credentials): Use secrets.dynamicSecret or secrets.vault (requires org version 61.0+).
  • Environment Variables (for non-sensitive data): Store in System.setProperty() (not recommended for production).

For older orgs, use System.getUserInfo().getUserId() to derive tokens dynamically.

Can I integrate this into a GitHub Actions workflow?

Yes. Use the @salesforce/cli plugin and the sfdx-force-mdapi-scan command. Example workflow: steps: - name: Install CLI run: npm install -g @salesforce/cli - name: Authenticate run: sfdx force:auth:web:login - name: Scan run: sfdx force:mdapi:scan -u [orgName] --force

Add this to .github/workflows/scan.yml and trigger on PRs.

Why does my scan show "No issues found"?

Possible reasons:

  • The scan is for metadata only—dynamic code (e.g., JavaScript in LWC) may not be scanned unless configured.
  • You’re testing in a sandbox without LWC support or a recent release.
  • The scan is disabled (check Setup > Security Controls > Code Security).
  • You’re reviewing the Deployment Summary instead of the Code Security Issues page.

Force a scan with the CLI or check component deployment status.

How do I exclude specific code files from scanning?

You cannot exclude files directly via the UI, but you can:

  • Use sfdx force:mdapi:decompile -u [orgName] --exclude [filePattern] to filter metadata before scanning.
  • For LWC, manually review components in component.json for "exclude": true (not officially supported).
  • Deploy only critical components to Production and skip others in CI/CD.

For dynamic code, use LWC Debugger to inspect runtime issues.