
Generating Apex
Generate, refactor, or review production-grade Salesforce Apex classes, triggers, and REST endpoints with consistent naming, sharing, and deployment defaults.
Install
npx skills add https://github.com/forcedotcom/sf-skills --skill generating-apexWhat is this skill?
- Covers service, selector, domain, batch, queueable, schedulable, invocable, trigger, DTO, utility, interface, abstract,
- Defaults: with sharing, public visibility, API version 66.0 minimum, runSpecifiedTests deployment posture
- Evidence-based review path for existing .cls and .trigger files, not only greenfield generation
- Maps required inputs: class type, SObjects, business goal, class naming table, refactor vs net-new, org/API constraints
Adoption & trust: 787 installs on skills.sh; 513 GitHub stars; 3/3 security scanners passed (skills.sh audits).
Recommended Skills
Journey fit
Apex authoring is core product backend work on Salesforce—canonical placement is build/backend where services, selectors, and async jobs live. Triggers, SOQL selectors, @RestResource APIs, and batch jobs are server-side logic, not storefront or marketing surfaces.
Common Questions / FAQ
Is Generating Apex safe to install?
skills.sh reports 3 of 3 security scanners passed. Review the Security Audits panel on this page before installing in production.
SKILL.md
READMESKILL.md - Generating Apex
# Generating Apex Use this skill for production-grade Apex: new classes, selectors, services, async jobs, invocable methods, and triggers; and for evidence-based review of existing `.cls` OR `.trigger`. ## Required Inputs Gather or infer before authoring: - Class type (service, selector, domain, batch, queueable, schedulable, invocable, trigger, trigger action, DTO, utility, interface, abstract, exception, REST resource) - Target object(s) and business goal - Class name (derive using the naming table below) - Net-new vs refactor/fix; any org/API constraints - Deployment targets (default to runSpecifiedTests and use generated tests where applicable) Defaults unless specified: - Sharing: `with sharing` (see sharing rules per type below) - Access: `public` (use `global` only when required by managed packages or `@RestResource`) - API version: `66.0` (minimum version) - ApexDoc comments: yes If the user provides a clear, complete request, generate immediately without unnecessary back-and-forth. --- ## Workflow All steps are sequential. Do not skip, merge, or reorder. If blocked, stop and ask for missing context. If not applicable, mark `N/A` with a one-line justification in the report. ### Phase 1 — Author 1. **Discover project conventions** - Service-Selector-Domain layering, logging utilities - Existing classes/triggers and current trigger framework or handler pattern - Whether Trigger Actions Framework (TAF) is already in use 2. **Choose the smallest correct pattern** (see Type-Specific Guidance below) 3. **Review templates and assets** - Read the matching template from `assets/` before authoring (see Type-Specific Guidance for the file mapping) - When a `references/` example exists for the type, read it as a concrete style guide - For any test class work, always read and use `generating-apex-test` skill 4. **Author with guardrails** -- apply every rule in the Rules section below - Generate `{ClassName}.cls` with ApexDoc - Generate `{ClassName}.cls-meta.xml` 5. **Generate test classes** -- Load the skill `generating-apex-test` to create `{ClassName}Test.cls` and `{ClassName}Test.cls-meta.xml`. Apex tests are always required to be generated to deploy. No test file creation or edits can occur without loading the `generating-apex-test` skill to generate tests. ### Phase 2 — Validate (required before reporting) Writing files is the midpoint, not the finish line. Steps 6 and 7 each require a tool invocation and produce output that must appear in the Step 8 report. Do not summarize or present the report until both steps have run and their output is captured. 6. **Run code analyzer** - Invoke MCP `run_code_analyzer` on all generated/updated `.cls` files. - Remediate all `sev0`, `sev1`, and `sev2` violations; re-run until clean. - Capture the final tool output verbatim for the report. - Fallback: `sf code-analyzer run --target <target>`. If both are unavailable, record `run_code_analyzer=unavailable: <error>` in the report. 7. **Execute Apex tests** - Run org tests including `{ClassName}Test` via `sf apex run test` or MCP. - Delegate all test generation/fixes/coverage work to `generating-apex-test`; iterate until the tests pass. - Capture pass/fail counts and coverage percentage for the report. - If unavailable, record `test_execution=unavailable: <error>