/platform-apex-anonymous-run
Use this skill to run anonymous Apex against the connected Salesforce org — from a .apex file or a pasted snippet — capturing the debug log, surfacing compile and runtime errors, and summarizing results. Trigger on phrases like \"run this anonymous apex\", \"execute this script
$ npx -y skills add forcedotcom/sf-skills --skill platform-apex-anonymous-run --agent claude-codeHow it fires
How this skill gets triggered: by you, by Claude, or both.
- Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
- You can call itInvoke it directly when you want it.
- Slash command
/platform-apex-anonymous-run
Context preview
The summary Claude sees to decide when to auto-load this skill.
Use this skill to run anonymous Apex against the connected Salesforce org — from a .apex file or a pasted snippet — capturing the debug log, surfacing compile and runtime errors, and summarizing results. Trigger on phrases like \"run this anonymous apex\", \"execute this script
SKILL.md
platform-apex-anonymous-run.SKILL.mdname: platform-apex-anonymous-run
description: "Use this skill to run anonymous Apex against the connected Salesforce org — from a .apex file or a pasted snippet — capturing the debug log, surfacing compile and runtime errors, and summarizing results. Trigger on phrases like \"run this anonymous apex\", \"execute this script against my org\", \"run this snippet of Apex\", \"what does this code return\", or \"execute scripts/foo.apex\". Wraps verification-style scripts in a savepoint and rollback so org state is untouched, and warns before running against production. DO NOT TRIGGER for authoring .cls or .trigger files (use platform-apex-generate), running Apex unit tests (use platform-apex-test-run), or deep debug-log analysis (use platform-apex-logs-debug)."
metadata:
version: "1.0"
relatedSkills:
- "platform-apex-generate"
- "platform-apex-test-run"
- "platform-apex-logs-debug"
cliTools:
- tool: ["sf"]
semver: ">=2.0.0"platform-apex-anonymous-run
Run anonymous Apex against the connected Salesforce org via `sf apex run --file`, capture the debug log, and narrate compile-time and runtime outcomes back to the developer.
This is the agent-side equivalent of VS Code's *Execute Anonymous Apex* (document and selection) commands.
This skill is **runtime, not generation** — for authoring `.cls` / `.trigger` files use `platform-apex-generate`; for running Apex unit tests use `platform-apex-test-run`; for deep debug-log analysis (governor breakdowns, SOQL-in-loop detection) hand off to `platform-apex-logs-debug`.
---
Tool Restrictions
**Use ONLY the Bash tool** to execute `sf apex run`, and the `Write` tool to stage snippet temp files. Do NOT use MCP tools for execution.
---
Anonymous Apex is NOT read-only
Anonymous Apex executes with the running user's permissions and **can perform DML, callouts, and platform events**. Treat every invocation as a write unless the developer has stated otherwise.
- **Verification-style scripts (preferred for "test this"):** wrap the body in a savepoint + rollback so org state is untouched:
Savepoint sp = Database.setSavepoint();
try {
// ... code under test ...
} finally {
Database.rollback(sp);
}- **Production org heads-up:** if the resolved `<alias>` points at a production org (no scratch/sandbox markers in `sf org display --json`), surface a clear warning before running. This is informational only — there is no automated block. Always wait for an explicit "yes, run it" before executing destructive scripts in prod.
- **Never run anonymous Apex you did not generate or have not been shown** — if the developer pastes a snippet, echo it back and confirm before executing.
---
Workflow
Step 1 — Identify the target org
Resolve the active org alias from configuration. If `target-org` is set, the `--target-org` flag may be omitted from the command, but always log which alias was used in the report.
sf config get target-org --json
Throughout this skill, `<alias>` is the resolved alias or username. If no `target-org` is set, ask the developer; do not silently default. If the org is not authenticated, re-authenticate with `sf org login web` or switch orgs with the `dx-org-switch` skill.
Step 2 — Resolve the input mode
| Mode | When | Action | |---|---|---| | **File mode** | Developer points at an existing path ending in `.apex` (or any path they specify) | Run `sf apex run --file <path>` directly | | **Snippet mode** | Developer pastes Apex code into the conversation | Write to `.sfdx/tmp/anon-<unix-ts>.apex` first, then run `sf apex run --file <tmp-path>` |
**Why a temp file for snippets, instead of an inline flag?** The current `sf apex run` CLI only supports `--file` (and interactive stdin). It does **not** expose an `--apex-code` flag. Even where inline code is supported by other tooling, multi-line Apex passed inline runs into shell-escaping pitfalls (single quotes in string literals, backslashes, embedded `$`). Writing to a temp file is the only reliable path for arbitrary snippets.
Verify CLI flags before deviating:
sf apex run --help
Supported flags (as of writing): `--file/-f`, `--target-org/-o`, `--api-version`, `--json`, `--flags-dir`. **Do not invent flags** — if the task asks for something not listed, surface that to the developer rather than guessing.
Step 3 — Set up trace flags (for useful logs)
`sf apex run` returns a debug log only if a `TraceFlag` is active for the running user (or a streaming tail is attached). Recommended path — let the developer tail logs in another terminal:
sf apex tail log --target-org <alias> --color
This auto-creates a short-lived TraceFlag for the running user and streams logs as anonymous Apex executes. Mention this in the report so the developer can copy/paste it.
If no trace flag is set up, `sf apex run` will still execute the code and return compile/runtime status — only the *debug log body* will be missing or sparse.
Step 4 — Snippet mode: write the temp file
Only applies when the input is a pasted snippet:
mkdir -p .sfdx/tmp
TS=$(date +%s)
# write the snippet content to .sfdx/tmp/anon-${TS}.apex via the Write tool, NOT via shell heredocUse the agent's `Write` tool (not a heredoc) so the snippet is preserved verbatim — heredocs subject the content to additional shell expansion. Echo the resolved temp path to the developer in the report. Do not auto-clean the temp file after execution — leave it under `.sfdx/tmp/` for inspection. The `.sfdx/` directory is conventionally gitignored.
Step 5 — Execute
sf apex run --file <path> --target-org <alias> --json
- Always pass `--json`. Human-format output conflates compile vs runtime errors.
- If `target-org` is already configured, `--target-org` may be omitted, but log the alias used.
- The command exits non-zero on compile errors. Capture both stdout and the parsed JSON.
Step 6 — P
Read more
name: platform-apex-anonymous-run
description: "Use this skill to run anonymous Apex against the connected Salesforce org — from a .apex file or a pasted snippet — capturing the debug log, surfacing compile and runtime errors, and summarizing results. Trigger on phrases like \"run this anonymous apex\", \"execute this script against my org\", \"run this snippet of Apex\", \"what does this code return\", or \"execute scripts/foo.apex\". Wraps verification-style scripts in a savepoint and rollback so org state is untouched, and warns before running against production. DO NOT TRIGGER for authoring .cls or .trigger files (use platform-apex-generate), running Apex unit tests (use platform-apex-test-run), or deep debug-log analysis (use platform-apex-logs-debug)."
metadata:
version: "1.0"
relatedSkills:
- "platform-apex-generate"
- "platform-apex-test-run"
- "platform-apex-logs-debug"
cliTools:
- tool: ["sf"]
semver: ">=2.0.0"platform-apex-anonymous-run
Run anonymous Apex against the connected Salesforce org via `sf apex run --file`, capture the debug log, and narrate compile-time and runtime outcomes back to the developer.
This is the agent-side equivalent of VS Code's *Execute Anonymous Apex* (document and selection) commands.
This skill is **runtime, not generation** — for authoring `.cls` / `.trigger` files use `platform-apex-generate`; for running Apex unit tests use `platform-apex-test-run`; for deep debug-log analysis (governor breakdowns, SOQL-in-loop detection) hand off to `platform-apex-logs-debug`.
---
Tool Restrictions
**Use ONLY the Bash tool** to execute `sf apex run`, and the `Write` tool to stage snippet temp files. Do NOT use MCP tools for execution.
---
Anonymous Apex is NOT read-only
Anonymous Apex executes with the running user's permissions and **can perform DML, callouts, and platform events**. Treat every invocation as a write unless the developer has stated otherwise.
- **Verification-style scripts (preferred for "test this"):** wrap the body in a savepoint + rollback so org state is untouched:
Savepoint sp = Database.setSavepoint();
try {
// ... code under test ...
} finally {
Database.rollback(sp);
}- **Production org heads-up:** if the resolved `<alias>` points at a production org (no scratch/sandbox markers in `sf org display --json`), surface a clear warning before running. This is informational only — there is no automated block. Always wait for an explicit "yes, run it" before executing destructive scripts in prod.
- **Never run anonymous Apex you did not generate or have not been shown** — if the developer pastes a snippet, echo it back and confirm before executing.
---
Workflow
Step 1 — Identify the target org
Resolve the active org alias from configuration. If `target-org` is set, the `--target-org` flag may be omitted from the command, but always log which alias was used in the report.
sf config get target-org --json
Throughout this skill, `<alias>` is the resolved alias or username. If no `target-org` is set, ask the developer; do not silently default. If the org is not authenticated, re-authenticate with `sf org login web` or switch orgs with the `dx-org-switch` skill.
Step 2 — Resolve the input mode
| Mode | When | Action | |---|---|---| | **File mode** | Developer points at an existing path ending in `.apex` (or any path they specify) | Run `sf apex run --file <path>` directly | | **Snippet mode** | Developer pastes Apex code into the conversation | Write to `.sfdx/tmp/anon-<unix-ts>.apex` first, then run `sf apex run --file <tmp-path>` |
**Why a temp file for snippets, instead of an inline flag?** The current `sf apex run` CLI only supports `--file` (and interactive stdin). It does **not** expose an `--apex-code` flag. Even where inline code is supported by other tooling, multi-line Apex passed inline runs into shell-escaping pitfalls (single quotes in string literals, backslashes, embedded `$`). Writing to a temp file is the only reliable path for arbitrary snippets.
Verify CLI flags before deviating:
sf apex run --help
Supported flags (as of writing): `--file/-f`, `--target-org/-o`, `--api-version`, `--json`, `--flags-dir`. **Do not invent flags** — if the task asks for something not listed, surface that to the developer rather than guessing.
Step 3 — Set up trace flags (for useful logs)
`sf apex run` returns a debug log only if a `TraceFlag` is active for the running user (or a streaming tail is attached). Recommended path — let the developer tail logs in another terminal:
sf apex tail log --target-org <alias> --color
This auto-creates a short-lived TraceFlag for the running user and streams logs as anonymous Apex executes. Mention this in the report so the developer can copy/paste it.
If no trace flag is set up, `sf apex run` will still execute the code and return compile/runtime status — only the *debug log body* will be missing or sparse.
Step 4 — Snippet mode: write the temp file
Only applies when the input is a pasted snippet:
mkdir -p .sfdx/tmp
TS=$(date +%s)
# write the snippet content to .sfdx/tmp/anon-${TS}.apex via the Write tool, NOT via shell heredocUse the agent's `Write` tool (not a heredoc) so the snippet is preserved verbatim — heredocs subject the content to additional shell expansion. Echo the resolved temp path to the developer in the report. Do not auto-clean the temp file after execution — leave it under `.sfdx/tmp/` for inspection. The `.sfdx/` directory is conventionally gitignored.
Step 5 — Execute
sf apex run --file <path> --target-org <alias> --json
- Always pass `--json`. Human-format output conflates compile vs runtime errors.
- If `target-org` is already configured, `--target-org` may be omitted, but log the alias used.
- The command exits non-zero on compile errors. Capture both stdout and the parsed JSON.
Step 6 — P
This repository provides a curated collection of Salesforce agent skills for building applications.
Repo: forcedotcom/sf-skills
Other skills on sf-skills.
- /agentforce-generate
Build, modify, optimize, debug, and deploy agents with Agentforce Agent Script. TRIGGER when: user creates, modifies, optimizes, or asks about .agent files or aiAuthoringBundle metadata; changes agent behavior, responses, or conversation logic; designs agent actions, tools,
Open skill - /agentforce-observe
Analyze production Agentforce agent behavior using session traces and Data Cloud. TRIGGER when: user queries STDM session data or Data Cloud trace records; investigates production agent failures, regressions, or performance issues; asks about session traces, conversation logs,
Open skill - /agentforce-test
Write, run, and analyze structured test suites for Agentforce agents — functional AND security. TRIGGER when: user writes or modifies test spec YAML (AiEvaluationDefinition); runs sf agent test create, run, run-eval, or results commands; asks about test coverage strategy, metric
Open skill - /automation-flow-generate
Generate Salesforce Flows using the MCP tool execute_metadata_action. Use when the user asks to create, build, or generate a flow — including Screen, Autolaunched, Record-Triggered (before/after-save), Scheduled. Also trigger for flow-like requests such as \"when a record is
Open skill - /dx-code-analyzer-configure
Set up, configure, and troubleshoot Salesforce Code Analyzer for any project. Handles installation, prerequisite checks, diagnosing broken setups, creating and editing code-analyzer.yml overrides, engine-specific settings, ignore patterns, severity overrides, and CI/CD pipeline
Open skill - /dx-code-analyzer-custom-rule-create
Create custom Code Analyzer rules for Regex (pattern matching), PMD (XPath/AST for Apex and metadata XML), and ESLint (LWC/JavaScript/TypeScript). Use when users want to enforce coding standards, ban patterns, detect hardcoded values, govern metadata, or add rules not in the
Open skill

