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
- building Flow XML or Flow orchestration → sf-flow
- writing SOQL only → sf-soql
- deploying or validating metadata to orgs → sf-deploy
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:
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 |
avoid coding against wrong schema |
| seed bulk or edge-case data |
sf-data |
create realistic test datasets |
| run Apex tests / fix failing tests |
sf-testing |
execute and iterate on failures |
| deploy to org |
sf-deploy |
validation and deployment orchestration |
| build Flow that calls Apex |
sf-flow |
declarative orchestration |
| build LWC that calls Apex |
sf-lwc |
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-apex
3description: >
4 Generates and reviews Salesforce Apex code with 150-point scoring.
5 TRIGGER when: user writes, reviews, or fixes Apex classes, triggers, test classes,
6 batch/queueable/schedulable jobs, or touches .cls/.trigger files.
7 DO NOT TRIGGER when: LWC JavaScript (use sf-lwc), Flow XML (use sf-flow),
8 SOQL-only queries (use sf-soql), or non-Salesforce code.
9license: MIT
10metadata:
11 source: https://skillmd.com/skills/om-scogo/sf-apex
12 version: "1.1.0"
13 author: "Jag Valaiyapathy"
14 scoring: "150 points across 8 categories"
15---
16
17# sf-apex: Salesforce Apex Code Generation and Review
18
19Use 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.
20
21## When This Skill Owns the Task
22
23Use `sf-apex` when the work involves:
24- Apex class generation or refactoring
25- trigger design and trigger-framework decisions
26- `@InvocableMethod`, Queueable, Batch, Schedulable, or test-class work
27- review of bulkification, sharing, security, testing, or maintainability
28
29Delegate elsewhere when the user is:
30- editing LWC JavaScript / HTML / CSS → [sf-lwc](../sf-lwc/SKILL.md)
31- building Flow XML or Flow orchestration → [sf-flow](../sf-flow/SKILL.md)
32- writing SOQL only → [sf-soql](../sf-soql/SKILL.md)
33- deploying or validating metadata to orgs → [sf-deploy](../sf-deploy/SKILL.md)
34
35---
36
37## Required Context to Gather First
38
39Ask for or infer:
40- class type: trigger, service, selector, batch, queueable, schedulable, invocable, test
41- target object(s) and business goal
42- whether code is net-new, refactor, or fix
43- org / API constraints if known
44- expected test coverage or deployment target
45
46Before authoring, inspect the project shape:
47- existing classes / triggers
48- current trigger framework or handler pattern
49- related tests, flows, and selectors
50- whether TAF is already in use
51
52---
53
54## Recommended Workflow
55
56### 1. Discover local architecture
57Check for:
58- existing trigger handlers / frameworks
59- service-selector-domain conventions
60- related tests and data factories
61- invocable or async patterns already used in the repo
62
63### 2. Choose the smallest correct pattern
64| Need | Preferred pattern |
65|---|---|
66| simple reusable logic | service class |
67| query-heavy data access | selector |
68| single object trigger behavior | one trigger + handler / TAF action |
69| Flow needs complex logic | `@InvocableMethod` |
70| background processing | Queueable by default |
71| very large datasets | Batch Apex or `Database.Cursor` patterns |
72| repeatable verification | dedicated test class + test data factory |
73
74### 3. Author with guardrails
75Generate code that is:
76- bulk-safe
77- sharing-aware
78- CRUD/FLS-safe where applicable
79- testable in isolation
80- consistent with project naming and layering
81
82### 4. Validate and score
83Evaluate against the 150-point rubric before handoff.
84
85### 5. Hand off deploy/test next steps
86When org validation is needed, hand off to:
87- [sf-testing](../sf-testing/SKILL.md) for test execution loops
88- [sf-deploy](../sf-deploy/SKILL.md) for deploy / dry-run / verification
89
90---
91
92## Generation Guardrails
93
94Never generate these without explicitly stopping and explaining the problem:
95
96| Anti-pattern | Why it blocks |
97|---|---|
98| SOQL in loops | governor-limit failure |
99| DML in loops | governor-limit failure |
100| missing sharing model | security / data exposure risk |
101| hardcoded IDs | deployment and portability failure |
102| empty `catch` blocks | silent failure / poor observability |
103| string-built SOQL with user input | injection risk |
104| tests without assertions | false-positive test suite |
105
106Default fix direction:
107- query once, operate on collections
108- use `with sharing` unless justified otherwise
109- use bind variables and `WITH USER_MODE` where appropriate
110- create assertions for positive, negative, and bulk cases
111
112See [references/anti-patterns.md](references/anti-patterns.md) and [references/security-guide.md](references/security-guide.md).
113
114---
115
116## High-Signal Build Rules
117
118### Trigger architecture
119- Prefer **one trigger per object**.
120- If TAF is already installed and used, extend it instead of inventing a second trigger pattern.
121- Triggers should delegate logic; avoid heavy business logic directly in trigger bodies.
122
123### Async choice
124| Scenario | Default |
125|---|---|
126| standard async work | Queueable |
127| very large record processing | Batch Apex |
128| recurring schedule | Scheduled Flow or Schedulable |
129| post-job cleanup | Finalizer |
130| long-running Lightning callouts | `Continuation` |
131
132### Testing minimums
133Use the **PNB** pattern for every feature:
134- **Positive** path
135- **Negative** / error path
136- **Bulk** path (251+ records where relevant)
137
138### Modern Apex expectations
139Prefer current idioms when available:
140- safe navigation: `obj?.Field__c`
141- null coalescing: `value ?? fallback`
142- `Assert.*` over legacy assertion style
143- `WITH USER_MODE` and explicit security handling where relevant
144
145---
146
147## Output Format
148
149When finishing, report in this order:
1501. **What was created or reviewed**
1512. **Files changed**
1523. **Key design decisions**
1534. **Risk / guardrail notes**
1545. **Test guidance**
1556. **Deployment guidance**
156
157Suggested shape:
158
159```text
160Apex work: <summary>
161Files: <paths>
162Design: <pattern / framework choices>
163Risks: <security, bulkification, async, dependency notes>
164Tests: <what to run / add>
165Deploy: <dry-run or next step>
166```
167
168---
169
170## LSP Validation Note
171
172This skill supports an LSP-assisted authoring loop for `.cls` and `.trigger` files:
173- syntax issues can be detected immediately after write/edit
174- the skill can auto-fix common syntax errors in a short loop
175- semantic quality still depends on the 150-point review rubric
176
177Full guide: [references/troubleshooting.md](references/troubleshooting.md#lsp-based-validation-auto-fix-loop)
178
179---
180
181## Cross-Skill Integration
182
183| Need | Delegate to | Reason |
184|---|---|---|
185| describe objects / fields first | [sf-metadata](../sf-metadata/SKILL.md) | avoid coding against wrong schema |
186| seed bulk or edge-case data | [sf-data](../sf-data/SKILL.md) | create realistic test datasets |
187| run Apex tests / fix failing tests | [sf-testing](../sf-testing/SKILL.md) | execute and iterate on failures |
188| deploy to org | [sf-deploy](../sf-deploy/SKILL.md) | validation and deployment orchestration |
189| build Flow that calls Apex | [sf-flow](../sf-flow/SKILL.md) | declarative orchestration |
190| build LWC that calls Apex | [sf-lwc](../sf-lwc/SKILL.md) | UI/controller integration |
191
192---
193
194## Reference Map
195
196### Start here
197- [references/patterns-deep-dive.md](references/patterns-deep-dive.md)
198- [references/security-guide.md](references/security-guide.md)
199- [references/bulkification-guide.md](references/bulkification-guide.md)
200- [references/testing-patterns.md](references/testing-patterns.md)
201
202### High-signal checklists
203- [references/code-review-checklist.md](references/code-review-checklist.md)
204- [references/anti-patterns.md](references/anti-patterns.md)
205- [references/naming-conventions.md](references/naming-conventions.md)
206
207### Specialized patterns
208- [references/trigger-actions-framework.md](references/trigger-actions-framework.md)
209- [references/automation-density-guide.md](references/automation-density-guide.md)
210- [references/flow-integration.md](references/flow-integration.md)
211- [references/triangle-pattern.md](references/triangle-pattern.md)
212- [references/design-patterns.md](references/design-patterns.md)
213- [references/solid-principles.md](references/solid-principles.md)
214
215### Troubleshooting / validation
216- [references/troubleshooting.md](references/troubleshooting.md)
217- [references/llm-anti-patterns.md](references/llm-anti-patterns.md)
218- [references/testing-guide.md](references/testing-guide.md)
219
220---
221
222## Score Guide
223
224| Score | Meaning |
225|---|---|
226| 120+ | strong production-ready Apex |
227| 90–119 | good implementation, review before deploy |
228| 67–89 | acceptable but needs improvement |
229| < 67 | block deployment |