sf-apex: Salesforce Apex Code Generation and Review
Use this skill when the user needs production Apex: new classes, triggers, selectors, services, async jobs, invocable methods, test classes, or evidence-based review of existing .cls / .trigger code.
When This Skill Owns the Task
Use @sf-apex when the work involves:
- Apex class generation or refactoring
- trigger design and trigger-framework decisions
@InvocableMethod, Queueable, Batch, Schedulable, or test-class work
- review of bulkification, sharing, security, testing, or maintainability
Delegate elsewhere when the user is:
- editing LWC JavaScript / HTML / CSS → [sf-lwc](../sf-lwc/rule file)
- building Flow XML or Flow orchestration → [sf-flow](../sf-flow/rule file)
- writing SOQL only → [sf-soql](../sf-soql/rule file)
- deploying or validating metadata to orgs → [sf-deploy](../sf-deploy/rule file)
Required Context to Gather First
Ask for or infer:
- class type: trigger, service, selector, batch, queueable, schedulable, invocable, test
- target object(s) and business goal
- whether code is net-new, refactor, or fix
- org / API constraints if known
- expected test coverage or deployment target
Before authoring, inspect the project shape:
- existing classes / triggers
- current trigger framework or handler pattern
- related tests, flows, and selectors
- whether TAF is already in use
Recommended Workflow
1. Discover local architecture
Check for:
- existing trigger handlers / frameworks
- service-selector-domain conventions
- related tests and data factories
- invocable or async patterns already used in the repo
2. Choose the smallest correct pattern
| Need |
Preferred pattern |
| simple reusable logic |
service class |
| query-heavy data access |
selector |
| single object trigger behavior |
one trigger + handler / TAF action |
| Flow needs complex logic |
@InvocableMethod |
| background processing |
Queueable by default |
| very large datasets |
Batch Apex or Database.Cursor patterns |
| repeatable verification |
dedicated test class + test data factory |
3. Author with guardrails
Generate code that is:
- bulk-safe
- sharing-aware
- CRUD/FLS-safe where applicable
- testable in isolation
- consistent with project naming and layering
4. Validate and score
Evaluate against the 150-point rubric before handoff.
5. Hand off deploy/test next steps
When org validation is needed, hand off to:
- [sf-testing](../sf-testing/rule file) for test execution loops
- [sf-deploy](../sf-deploy/rule file) for deploy / dry-run / verification
Generation Guardrails
Never generate these without explicitly stopping and explaining the problem:
| Anti-pattern |
Why it blocks |
| SOQL in loops |
governor-limit failure |
| DML in loops |
governor-limit failure |
| missing sharing model |
security / data exposure risk |
| hardcoded IDs |
deployment and portability failure |
empty catch blocks |
silent failure / poor observability |
| string-built SOQL with user input |
injection risk |
| tests without assertions |
false-positive test suite |
Default fix direction:
- query once, operate on collections
- use
with sharing unless justified otherwise
- use bind variables and
WITH USER_MODE where appropriate
- create assertions for positive, negative, and bulk cases
See references/anti-patterns.md and references/security-guide.md.
High-Signal Build Rules
Trigger architecture
- Prefer one trigger per object.
- If TAF is already installed and used, extend it instead of inventing a second trigger pattern.
- Triggers should delegate logic; avoid heavy business logic directly in trigger bodies.
Async choice
| Scenario |
Default |
| standard async work |
Queueable |
| very large record processing |
Batch Apex |
| recurring schedule |
Scheduled Flow or Schedulable |
| post-job cleanup |
Finalizer |
| long-running Lightning callouts |
Continuation |
Testing minimums
Use the PNB pattern for every feature:
- Positive path
- Negative / error path
- Bulk path (251+ records where relevant)
Modern Apex expectations
Prefer current idioms when available:
- safe navigation:
obj?.Field__c
- null coalescing:
value ?? fallback
Assert.* over legacy assertion style
WITH USER_MODE and explicit security handling where relevant
Output Format
When finishing, report in this order:
- What was created or reviewed
- Files changed
- Key design decisions
- Risk / guardrail notes
- Test guidance
- Deployment guidance
Suggested shape:
Apex work: <summary>
Files: <paths>
Design: <pattern / framework choices>
Risks: <security, bulkification, async, dependency notes>
Tests: <what to run / add>
Deploy: <dry-run or next step>
LSP Validation Note
This skill supports an LSP-assisted authoring loop for .cls and .trigger files:
- syntax issues can be detected immediately after write/edit
- the skill can auto-fix common syntax errors in a short loop
- semantic quality still depends on the 150-point review rubric
Full guide: references/troubleshooting.md
Cross-Skill Integration
| Need |
Delegate to |
Reason |
| describe objects / fields first |
[sf-metadata](../sf-metadata/rule file) |
avoid coding against wrong schema |
| seed bulk or edge-case data |
[sf-data](../sf-data/rule file) |
create realistic test datasets |
| run Apex tests / fix failing tests |
[sf-testing](../sf-testing/rule file) |
execute and iterate on failures |
| deploy to org |
[sf-deploy](../sf-deploy/rule file) |
validation and deployment orchestration |
| build Flow that calls Apex |
[sf-flow](../sf-flow/rule file) |
declarative orchestration |
| build LWC that calls Apex |
[sf-lwc](../sf-lwc/rule file) |
UI/controller integration |
Reference Map
Start here
- references/patterns-deep-dive.md
- references/security-guide.md
- references/bulkification-guide.md
- references/testing-patterns.md
High-signal checklists
- references/code-review-checklist.md
- references/anti-patterns.md
- references/naming-conventions.md
Specialized patterns
- references/trigger-actions-framework.md
- references/automation-density-guide.md
- references/flow-integration.md
- references/triangle-pattern.md
- references/design-patterns.md
- references/solid-principles.md
Troubleshooting / validation
- references/troubleshooting.md
- references/llm-anti-patterns.md
- references/testing-guide.md
Score Guide
| Score |
Meaning |
| 120+ |
strong production-ready Apex |
| 90–119 |
good implementation, review before deploy |
| 67–89 |
acceptable but needs improvement |
| < 67 |
block deployment |
1---2name: sf-apex3description: Generates and reviews Salesforce Apex code with 150-point scoring. TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes, batch/queueable/schedulable jobs, or touches .cls/.trigger files. DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow), SOQL-only queries (use sf-soql), or non-Salesforce code.4license: MIT5---67# sf-apex: Salesforce Apex Code Generation and Review89Use this skill when the user needs **production Apex**: new classes, triggers, selectors, services, async jobs, invocable methods, test classes, or evidence-based review of existing `.cls` / `.trigger` code.1011## When This Skill Owns the Task1213Use `@sf-apex` when the work involves:14- Apex class generation or refactoring15- trigger design and trigger-framework decisions16- `@InvocableMethod`, Queueable, Batch, Schedulable, or test-class work17- review of bulkification, sharing, security, testing, or maintainability1819Delegate elsewhere when the user is:20- editing LWC JavaScript / HTML / CSS → [sf-lwc](../sf-lwc/rule file)21- building Flow XML or Flow orchestration → [sf-flow](../sf-flow/rule file)22- writing SOQL only → [sf-soql](../sf-soql/rule file)23- deploying or validating metadata to orgs → [sf-deploy](../sf-deploy/rule file)2425---2627## Required Context to Gather First2829Ask for or infer:30- class type: trigger, service, selector, batch, queueable, schedulable, invocable, test31- target object(s) and business goal32- whether code is net-new, refactor, or fix33- org / API constraints if known34- expected test coverage or deployment target3536Before authoring, inspect the project shape:37- existing classes / triggers38- current trigger framework or handler pattern39- related tests, flows, and selectors40- whether TAF is already in use4142---4344## Recommended Workflow4546### 1. Discover local architecture47Check for:48- existing trigger handlers / frameworks49- service-selector-domain conventions50- related tests and data factories51- invocable or async patterns already used in the repo5253### 2. Choose the smallest correct pattern54| Need | Preferred pattern |55|---|---|56| simple reusable logic | service class |57| query-heavy data access | selector |58| single object trigger behavior | one trigger + handler / TAF action |59| Flow needs complex logic | `@InvocableMethod` |60| background processing | Queueable by default |61| very large datasets | Batch Apex or `Database.Cursor` patterns |62| repeatable verification | dedicated test class + test data factory |6364### 3. Author with guardrails65Generate code that is:66- bulk-safe67- sharing-aware68- CRUD/FLS-safe where applicable69- testable in isolation70- consistent with project naming and layering7172### 4. Validate and score73Evaluate against the 150-point rubric before handoff.7475### 5. Hand off deploy/test next steps76When org validation is needed, hand off to:77- [sf-testing](../sf-testing/rule file) for test execution loops78- [sf-deploy](../sf-deploy/rule file) for deploy / dry-run / verification7980---8182## Generation Guardrails8384Never generate these without explicitly stopping and explaining the problem:8586| Anti-pattern | Why it blocks |87|---|---|88| SOQL in loops | governor-limit failure |89| DML in loops | governor-limit failure |90| missing sharing model | security / data exposure risk |91| hardcoded IDs | deployment and portability failure |92| empty `catch` blocks | silent failure / poor observability |93| string-built SOQL with user input | injection risk |94| tests without assertions | false-positive test suite |9596Default fix direction:97- query once, operate on collections98- use `with sharing` unless justified otherwise99- use bind variables and `WITH USER_MODE` where appropriate100- create assertions for positive, negative, and bulk cases101102See [references/anti-patterns.md](references/anti-patterns.md) and [references/security-guide.md](references/security-guide.md).103104---105106## High-Signal Build Rules107108### Trigger architecture109- Prefer **one trigger per object**.110- If TAF is already installed and used, extend it instead of inventing a second trigger pattern.111- Triggers should delegate logic; avoid heavy business logic directly in trigger bodies.112113### Async choice114| Scenario | Default |115|---|---|116| standard async work | Queueable |117| very large record processing | Batch Apex |118| recurring schedule | Scheduled Flow or Schedulable |119| post-job cleanup | Finalizer |120| long-running Lightning callouts | `Continuation` |121122### Testing minimums123Use the **PNB** pattern for every feature:124- **Positive** path125- **Negative** / error path126- **Bulk** path (251+ records where relevant)127128### Modern Apex expectations129Prefer current idioms when available:130- safe navigation: `obj?.Field__c`131- null coalescing: `value ?? fallback`132- `Assert.*` over legacy assertion style133- `WITH USER_MODE` and explicit security handling where relevant134135---136137## Output Format138139When finishing, report in this order:1401. **What was created or reviewed**1412. **Files changed**1423. **Key design decisions**1434. **Risk / guardrail notes**1445. **Test guidance**1456. **Deployment guidance**146147Suggested shape:148149```text150Apex work: <summary>151Files: <paths>152Design: <pattern / framework choices>153Risks: <security, bulkification, async, dependency notes>154Tests: <what to run / add>155Deploy: <dry-run or next step>156```157158---159160## LSP Validation Note161162This skill supports an LSP-assisted authoring loop for `.cls` and `.trigger` files:163- syntax issues can be detected immediately after write/edit164- the skill can auto-fix common syntax errors in a short loop165- semantic quality still depends on the 150-point review rubric166167Full guide: [references/troubleshooting.md](references/troubleshooting.md#lsp-based-validation-auto-fix-loop)168169---170171## Cross-Skill Integration172173| Need | Delegate to | Reason |174|---|---|---|175| describe objects / fields first | [sf-metadata](../sf-metadata/rule file) | avoid coding against wrong schema |176| seed bulk or edge-case data | [sf-data](../sf-data/rule file) | create realistic test datasets |177| run Apex tests / fix failing tests | [sf-testing](../sf-testing/rule file) | execute and iterate on failures |178| deploy to org | [sf-deploy](../sf-deploy/rule file) | validation and deployment orchestration |179| build Flow that calls Apex | [sf-flow](../sf-flow/rule file) | declarative orchestration |180| build LWC that calls Apex | [sf-lwc](../sf-lwc/rule file) | UI/controller integration |181182---183184## Reference Map185186### Start here187- [references/patterns-deep-dive.md](references/patterns-deep-dive.md)188- [references/security-guide.md](references/security-guide.md)189- [references/bulkification-guide.md](references/bulkification-guide.md)190- [references/testing-patterns.md](references/testing-patterns.md)191192### High-signal checklists193- [references/code-review-checklist.md](references/code-review-checklist.md)194- [references/anti-patterns.md](references/anti-patterns.md)195- [references/naming-conventions.md](references/naming-conventions.md)196197### Specialized patterns198- [references/trigger-actions-framework.md](references/trigger-actions-framework.md)199- [references/automation-density-guide.md](references/automation-density-guide.md)200- [references/flow-integration.md](references/flow-integration.md)201- [references/triangle-pattern.md](references/triangle-pattern.md)202- [references/design-patterns.md](references/design-patterns.md)203- [references/solid-principles.md](references/solid-principles.md)204205### Troubleshooting / validation206- [references/troubleshooting.md](references/troubleshooting.md)207- [references/llm-anti-patterns.md](references/llm-anti-patterns.md)208- [references/testing-guide.md](references/testing-guide.md)209210---211212## Score Guide213214| Score | Meaning |215|---|---|216| 120+ | strong production-ready Apex |217| 90–119 | good implementation, review before deploy |218| 67–89 | acceptable but needs improvement |219| < 67 | block deployment |