@salesforce/afv-skills 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (150) hide show
  1. package/package.json +6 -5
  2. package/skills/accessing-webapp-data/SKILL.md +178 -0
  3. package/skills/agentforce-development/SKILL.md +427 -0
  4. package/skills/agentforce-development/assets/README-legacy.md +89 -0
  5. package/skills/agentforce-development/assets/agent-spec-template.md +90 -0
  6. package/skills/agentforce-development/assets/agents/README.md +45 -0
  7. package/skills/agentforce-development/assets/agents/hello-world.agent +60 -0
  8. package/skills/agentforce-development/assets/agents/multi-topic.agent +105 -0
  9. package/skills/agentforce-development/assets/agents/production-faq.agent +101 -0
  10. package/skills/agentforce-development/assets/agents/production-faq.bundle-meta.xml +4 -0
  11. package/skills/agentforce-development/assets/agents/simple-qa.agent +72 -0
  12. package/skills/agentforce-development/assets/apex/models-api-queueable.cls +225 -0
  13. package/skills/agentforce-development/assets/bundle-meta.xml +23 -0
  14. package/skills/agentforce-development/assets/components/apex-action.agent +52 -0
  15. package/skills/agentforce-development/assets/components/error-handling.agent +58 -0
  16. package/skills/agentforce-development/assets/components/escalation-setup.agent +169 -0
  17. package/skills/agentforce-development/assets/components/flow-action.agent +66 -0
  18. package/skills/agentforce-development/assets/components/n-ary-conditions.agent +110 -0
  19. package/skills/agentforce-development/assets/components/topic-with-actions.agent +40 -0
  20. package/skills/agentforce-development/assets/deterministic-routing.agent +166 -0
  21. package/skills/agentforce-development/assets/escalation-pattern.agent +209 -0
  22. package/skills/agentforce-development/assets/flow-action-lookup.agent +115 -0
  23. package/skills/agentforce-development/assets/hub-and-spoke.agent +104 -0
  24. package/skills/agentforce-development/assets/invocable-apex-template.cls +187 -0
  25. package/skills/agentforce-development/assets/local-info-agent-annotated.agent +355 -0
  26. package/skills/agentforce-development/assets/metadata/basic-prompt-template.promptTemplate-meta.xml +109 -0
  27. package/skills/agentforce-development/assets/metadata/genai-function-apex.xml +92 -0
  28. package/skills/agentforce-development/assets/metadata/genai-function-flow.xml +57 -0
  29. package/skills/agentforce-development/assets/metadata/genai-plugin.xml +72 -0
  30. package/skills/agentforce-development/assets/metadata/http-callout-flow.flow-meta.xml +348 -0
  31. package/skills/agentforce-development/assets/metadata/record-grounded-prompt.promptTemplate-meta.xml +136 -0
  32. package/skills/agentforce-development/assets/minimal-starter.agent +42 -0
  33. package/skills/agentforce-development/assets/patterns/README.md +254 -0
  34. package/skills/agentforce-development/assets/patterns/action-callbacks.agent +178 -0
  35. package/skills/agentforce-development/assets/patterns/advanced-input-bindings.agent +141 -0
  36. package/skills/agentforce-development/assets/patterns/bidirectional-routing.agent +156 -0
  37. package/skills/agentforce-development/assets/patterns/critical-input-collection.agent +244 -0
  38. package/skills/agentforce-development/assets/patterns/delegation-routing.agent +89 -0
  39. package/skills/agentforce-development/assets/patterns/lifecycle-events.agent +127 -0
  40. package/skills/agentforce-development/assets/patterns/llm-controlled-actions.agent +184 -0
  41. package/skills/agentforce-development/assets/patterns/multi-step-workflow.agent +282 -0
  42. package/skills/agentforce-development/assets/patterns/open-gate-routing.agent +286 -0
  43. package/skills/agentforce-development/assets/patterns/procedural-instructions.agent +273 -0
  44. package/skills/agentforce-development/assets/patterns/prompt-template-action.agent +188 -0
  45. package/skills/agentforce-development/assets/patterns/system-instruction-overrides.agent +293 -0
  46. package/skills/agentforce-development/assets/prompt-rag-search.agent +131 -0
  47. package/skills/agentforce-development/assets/template-multi-topic.agent +160 -0
  48. package/skills/agentforce-development/assets/template-single-topic.agent +81 -0
  49. package/skills/agentforce-development/assets/verification-gate.agent +208 -0
  50. package/skills/agentforce-development/references/action-prompt-templates.md +164 -0
  51. package/skills/agentforce-development/references/actions-reference.md +592 -0
  52. package/skills/agentforce-development/references/agent-access-guide.md +72 -0
  53. package/skills/agentforce-development/references/agent-design-and-spec-creation.md +1010 -0
  54. package/skills/agentforce-development/references/agent-metadata-and-lifecycle.md +575 -0
  55. package/skills/agentforce-development/references/agent-script-core-language.md +1218 -0
  56. package/skills/agentforce-development/references/agent-topic-map-diagrams.md +323 -0
  57. package/skills/agentforce-development/references/agent-user-setup.md +526 -0
  58. package/skills/agentforce-development/references/agent-validation-and-debugging.md +803 -0
  59. package/skills/agentforce-development/references/known-issues.md +353 -0
  60. package/skills/agentforce-development/references/minimal-examples.md +67 -0
  61. package/skills/agentforce-development/references/production-gotchas.md +279 -0
  62. package/skills/agentforce-development/references/salesforce-cli-for-agents.md +393 -0
  63. package/skills/agentforce-development/references/version-history.md +23 -0
  64. package/skills/building-webapp-data-visualization/SKILL.md +72 -0
  65. package/skills/building-webapp-data-visualization/implementation/bar-line-chart.md +316 -0
  66. package/skills/building-webapp-data-visualization/implementation/dashboard-layout.md +189 -0
  67. package/skills/building-webapp-data-visualization/implementation/donut-chart.md +181 -0
  68. package/skills/building-webapp-data-visualization/implementation/stat-card.md +150 -0
  69. package/skills/building-webapp-react-components/SKILL.md +96 -0
  70. package/skills/building-webapp-react-components/implementation/component.md +78 -0
  71. package/skills/building-webapp-react-components/implementation/header-footer.md +132 -0
  72. package/skills/building-webapp-react-components/implementation/page.md +93 -0
  73. package/skills/configuring-webapp-csp-trusted-sites/SKILL.md +90 -0
  74. package/skills/configuring-webapp-csp-trusted-sites/implementation/metadata-format.md +281 -0
  75. package/skills/configuring-webapp-metadata/SKILL.md +158 -0
  76. package/skills/creating-webapp/SKILL.md +141 -0
  77. package/skills/deploying-webapp-to-salesforce/SKILL.md +229 -0
  78. package/skills/exploring-webapp-graphql-schema/SKILL.md +149 -0
  79. package/skills/fetching-webapp-rest-api/SKILL.md +167 -0
  80. package/skills/{salesforce-custom-application → generating-custom-application}/SKILL.md +2 -4
  81. package/skills/{salesforce-custom-field → generating-custom-field}/SKILL.md +1 -5
  82. package/skills/{salesforce-custom-lightning-type → generating-custom-lightning-type}/SKILL.md +36 -2
  83. package/skills/{salesforce-custom-object → generating-custom-object}/SKILL.md +1 -1
  84. package/skills/generating-custom-tab/SKILL.md +154 -0
  85. package/skills/generating-experience-lwr-site/SKILL.md +196 -0
  86. package/skills/generating-experience-lwr-site/docs/bootstrap-template-byo-lwr.md +224 -0
  87. package/skills/generating-experience-lwr-site/docs/configure-content-brandingSet.md +131 -0
  88. package/skills/generating-experience-lwr-site/docs/configure-content-route.md +232 -0
  89. package/skills/generating-experience-lwr-site/docs/configure-content-themeLayout.md +141 -0
  90. package/skills/generating-experience-lwr-site/docs/configure-content-view.md +233 -0
  91. package/skills/generating-experience-lwr-site/docs/configure-guest-sharing-rules.md +42 -0
  92. package/skills/generating-experience-lwr-site/docs/handle-component-and-region-ids.md +27 -0
  93. package/skills/generating-experience-lwr-site/docs/handle-ui-components.md +215 -0
  94. package/skills/generating-experience-react-site/SKILL.md +67 -0
  95. package/skills/generating-experience-react-site/docs/configure-metadata-custom-site.md +41 -0
  96. package/skills/generating-experience-react-site/docs/configure-metadata-digital-experience-bundle.md +17 -0
  97. package/skills/generating-experience-react-site/docs/configure-metadata-digital-experience-config.md +21 -0
  98. package/skills/generating-experience-react-site/docs/configure-metadata-digital-experience.md +38 -0
  99. package/skills/generating-experience-react-site/docs/configure-metadata-network.md +72 -0
  100. package/skills/{salesforce-flexipage → generating-flexipage}/SKILL.md +86 -9
  101. package/skills/{salesforce-flow → generating-flow}/SKILL.md +3 -3
  102. package/skills/generating-fragment/SKILL.md +117 -0
  103. package/skills/generating-lightning-app/SKILL.md +423 -0
  104. package/skills/{salesforce-list-view → generating-list-view}/SKILL.md +1 -1
  105. package/skills/generating-permission-set/SKILL.md +174 -0
  106. package/skills/{salesforce-validation-rule → generating-validation-rule}/SKILL.md +1 -1
  107. package/skills/generating-webapp-graphql-mutation-query/SKILL.md +258 -0
  108. package/skills/generating-webapp-graphql-read-query/SKILL.md +253 -0
  109. package/skills/implementing-webapp-file-upload/SKILL.md +396 -0
  110. package/skills/installing-webapp-features/SKILL.md +210 -0
  111. package/skills/managing-webapp-agentforce-conversation-client/SKILL.md +186 -0
  112. package/skills/managing-webapp-agentforce-conversation-client/references/constraints.md +134 -0
  113. package/skills/managing-webapp-agentforce-conversation-client/references/examples.md +132 -0
  114. package/skills/managing-webapp-agentforce-conversation-client/references/style-tokens.md +101 -0
  115. package/skills/managing-webapp-agentforce-conversation-client/references/troubleshooting.md +57 -0
  116. package/skills/switching-org/SKILL.md +28 -0
  117. package/skills/using-webapp-graphql/SKILL.md +324 -0
  118. package/skills/using-webapp-graphql/shared-schema.graphqls +1150 -0
  119. package/skills/apex-class/SKILL.md +0 -253
  120. package/skills/apex-class/examples/AccountDeduplicationBatch.cls +0 -148
  121. package/skills/apex-class/examples/AccountSelector.cls +0 -193
  122. package/skills/apex-class/examples/AccountService.cls +0 -201
  123. package/skills/apex-class/templates/abstract.cls +0 -128
  124. package/skills/apex-class/templates/batch.cls +0 -125
  125. package/skills/apex-class/templates/domain.cls +0 -102
  126. package/skills/apex-class/templates/dto.cls +0 -108
  127. package/skills/apex-class/templates/exception.cls +0 -51
  128. package/skills/apex-class/templates/interface.cls +0 -25
  129. package/skills/apex-class/templates/queueable.cls +0 -92
  130. package/skills/apex-class/templates/schedulable.cls +0 -75
  131. package/skills/apex-class/templates/selector.cls +0 -92
  132. package/skills/apex-class/templates/service.cls +0 -69
  133. package/skills/apex-class/templates/utility.cls +0 -97
  134. package/skills/apex-test-class/SKILL.md +0 -101
  135. package/skills/apex-test-class/references/assertion-patterns.md +0 -209
  136. package/skills/apex-test-class/references/async-testing.md +0 -276
  137. package/skills/apex-test-class/references/mocking-patterns.md +0 -219
  138. package/skills/apex-test-class/references/test-data-factory.md +0 -176
  139. package/skills/deployment-readiness-check/SKILL.md +0 -257
  140. package/skills/deployment-readiness-check/assets/deployment_checklist.md +0 -286
  141. package/skills/deployment-readiness-check/references/rollback_procedures.md +0 -308
  142. package/skills/deployment-readiness-check/scripts/check_metadata.sh +0 -207
  143. package/skills/salesforce-custom-tab/SKILL.md +0 -78
  144. package/skills/salesforce-experience-site/SKILL.md +0 -178
  145. package/skills/salesforce-fragment/SKILL.md +0 -42
  146. package/skills/salesforce-lightning-app-build/SKILL.md +0 -254
  147. package/skills/salesforce-web-app-creating-records/SKILL.md +0 -84
  148. package/skills/salesforce-web-app-feature/SKILL.md +0 -70
  149. package/skills/salesforce-web-app-list-and-create-records/SKILL.md +0 -36
  150. package/skills/salesforce-web-application/SKILL.md +0 -34
@@ -1,253 +0,0 @@
1
- ---
2
- name: apex-class
3
- description: Generate production-ready Apex classes for Salesforce following enterprise best practices. Covers service classes, selectors, domain classes, batch/queueable/schedulable, DTOs, utilities, interfaces, abstract classes, and custom exceptions. Not for triggers or unit tests. Use this skill whenever the user asks to create, write, build, generate, or scaffold any Apex class — including when they describe functionality that would require an Apex class without explicitly saying "Apex class." Trigger on phrases like "build a service for," "create a handler," "I need a class that," "write Apex to," "scaffold a batch job," "create a queueable," or any request involving Salesforce server-side logic. Also use when the user asks to refactor, improve, or restructure existing Apex classes. Even if the request is vague like "I need something that processes Opportunities nightly," use this skill if the solution would involve an Apex class.
4
- ---
5
-
6
- # Apex Class
7
-
8
- Generate well-structured, production-ready Apex classes for Salesforce development.
9
-
10
- ## Scope
11
-
12
- **Generates:**
13
- - Service classes (business logic layer)
14
- - Selector / query classes (SOQL encapsulation)
15
- - Domain classes (SObject-specific logic)
16
- - Batch Apex classes (`Database.Batchable`)
17
- - Queueable Apex classes (`Queueable`, optionally `Finalizer`)
18
- - Schedulable Apex classes (`Schedulable`)
19
- - DTO / wrapper / request-response classes
20
- - Utility / helper classes
21
- - Custom exception classes
22
- - Interfaces and abstract classes
23
-
24
- **Does NOT generate:**
25
- - Triggers (use a trigger framework skill)
26
- - Unit tests (use a test writer skill)
27
- - Aura controllers
28
- - LWC JavaScript controllers
29
-
30
- ---
31
-
32
- ## Gathering Requirements
33
-
34
- Before generating code, gather these inputs from the user (ask if not provided):
35
-
36
- 1. **Class type** — Service, Selector, Batch, Queueable, Schedulable, Domain, DTO, Utility, Interface, Abstract, Exception
37
- 2. **Class name** — or enough context to derive a meaningful name
38
- 3. **SObject(s) involved** — if applicable
39
- 4. **Business requirements** — plain-language description of what the class should do
40
- 5. **Optional preferences:**
41
- - Sharing model (`with sharing`, `without sharing`, `inherited sharing`)
42
- - Access modifier (`public` or `global`)
43
- - API version (default: `62.0`)
44
- - Whether to include ApexDoc comments (default: yes)
45
-
46
- If the user provides a clear, complete request, generate immediately without unnecessary back-and-forth.
47
-
48
- ---
49
-
50
- ## Code Standards — ALWAYS Follow These
51
-
52
- ### Separation of Concerns
53
- - **Service classes** contain business logic. They call Selectors for data and may call Domain classes for SObject-specific behavior.
54
- - **Selector classes** encapsulate all SOQL queries. Services never contain inline SOQL.
55
- - **Domain classes** encapsulate SObject-specific logic (field defaults, validation, transformation).
56
- - Keep classes focused on a single responsibility.
57
-
58
- ### Bulkification
59
- - All methods must operate on collections (`List`, `Set`, `Map`) by default.
60
- - Never accept a single SObject when a `List<SObject>` is appropriate.
61
- - Provide convenience overloads for single-record callers only when it makes the API cleaner, and have them delegate to the bulk method.
62
-
63
- ### Governor Limit Safety
64
- - **No SOQL or DML inside loops.** Ever.
65
- - Collect IDs/records first, query/DML once outside the loop.
66
- - Use `Limits` class checks in batch/bulk operations where appropriate.
67
- - Prefer `Database.insert(records, false)` with error handling in batch contexts.
68
-
69
- ### Sharing Model
70
- - Default to `with sharing` unless the user specifies otherwise.
71
- - If `without sharing` or `inherited sharing` is used, add an ApexDoc `@description` comment explaining WHY.
72
-
73
- ### Naming Conventions
74
-
75
- | Class Type | Pattern | Example |
76
- |-------------|-------------------------------|-------------------------------|
77
- | Service | `{SObject}Service` | `AccountService` |
78
- | Selector | `{SObject}Selector` | `AccountSelector` |
79
- | Domain | `{SObject}Domain` | `OpportunityDomain` |
80
- | Batch | `{Descriptive}Batch` | `AccountDeduplicationBatch` |
81
- | Queueable | `{Descriptive}Queueable` | `ExternalSyncQueueable` |
82
- | Schedulable | `{Descriptive}Schedulable` | `DailyCleanupSchedulable` |
83
- | DTO | `{Descriptive}DTO` | `AccountMergeRequestDTO` |
84
- | Wrapper | `{Descriptive}Wrapper` | `OpportunityLineWrapper` |
85
- | Utility | `{Descriptive}Util` | `StringUtil`, `DateUtil` |
86
- | Interface | `I{Descriptive}` | `INotificationService` |
87
- | Abstract | `Abstract{Descriptive}` | `AbstractIntegrationService` |
88
- | Exception | `{Descriptive}Exception` | `AccountServiceException` |
89
-
90
- ### ApexDoc Comments
91
- Include ApexDoc on every `public` and `global` method and on the class itself:
92
-
93
- ```apex
94
- /**
95
- * @description Brief description of what the class does
96
- * @author Generated by Apex Class Writer Skill
97
- */
98
- ```
99
-
100
- ```apex
101
- /**
102
- * @description Brief description of what the method does
103
- * @param paramName Description of the parameter
104
- * @return Description of the return value
105
- * @example
106
- * List<Account> results = AccountService.deduplicateAccounts(accountIds);
107
- */
108
- ```
109
-
110
- ### Null Safety
111
- - Use guard clauses at the top of methods for null/empty inputs.
112
- - Use safe navigation (`?.`) where appropriate.
113
- - Return empty collections rather than `null`.
114
-
115
- ### Constants
116
- - No magic strings or numbers in logic.
117
- - Use `private static final` constants or a dedicated constants class.
118
- - Use `Label.` custom labels for user-facing strings when appropriate.
119
-
120
- ### Custom Exceptions
121
- - Each service class should define or reference a corresponding custom exception.
122
- - Inner exception classes are preferred for simple cases: `public class AccountServiceException extends Exception {}`
123
- - Include meaningful error messages with context.
124
-
125
- ### Error Handling
126
- - Catch specific exceptions, not generic `Exception` unless re-throwing.
127
- - Log errors meaningfully (assume a logging utility exists or stub one).
128
- - In batch contexts, use `Database.SaveResult` / `Database.UpsertResult` with partial success.
129
-
130
- ---
131
-
132
- ## Output Format
133
-
134
- For every class, produce TWO files:
135
-
136
- 1. **`{ClassName}.cls`** — The Apex class source code
137
- 2. **`{ClassName}.cls-meta.xml`** — The metadata file
138
-
139
- ### Meta XML Template
140
-
141
- ```xml
142
- <?xml version="1.0" encoding="UTF-8"?>
143
- <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata">
144
- <apiVersion>{API_VERSION}</apiVersion>
145
- <status>Active</status>
146
- </ApexClass>
147
- ```
148
-
149
- Default `apiVersion` is `62.0` unless the user specifies otherwise.
150
-
151
- ---
152
-
153
- ## Class Type–Specific Instructions
154
-
155
- ### Service Classes
156
- - Read and follow: `templates/service.cls`
157
- - Stateless — no instance variables holding mutable state
158
- - All public methods should be `static` unless there's a compelling reason for instance methods
159
- - Delegate queries to a Selector class
160
- - Wrap business logic errors in a custom exception
161
-
162
- ### Selector Classes
163
- - Read and follow: `templates/selector.cls`
164
- - One Selector per SObject (or per logical query domain)
165
- - Return `List<SObject>` or `Map<Id, SObject>`
166
- - Accept filter criteria as method parameters, not hardcoded
167
- - Include a private method that returns the base field list to keep DRY
168
-
169
- ### Domain Classes
170
- - Read and follow: `templates/domain.cls`
171
- - Encapsulate field-level defaults, derivations, and validations
172
- - Operate on `List<SObject>` — designed to be called from triggers or services
173
- - No SOQL or DML — only in-memory SObject manipulation
174
-
175
- ### Batch Classes
176
- - Read and follow: `templates/batch.cls`
177
- - Implement `Database.Batchable<SObject>` and optionally `Database.Stateful`
178
- - Use `Database.QueryLocator` in `start()` for large datasets
179
- - Handle partial failures in `execute()` using `Database.SaveResult`
180
- - Implement meaningful `finish()` — at minimum, log completion
181
-
182
- ### Queueable Classes
183
- - Read and follow: `templates/queueable.cls`
184
- - Implement `Queueable` and optionally `Database.AllowsCallouts`
185
- - Accept data through the constructor — queueables are stateful
186
- - For chaining, include guard logic to prevent infinite chains
187
- - Optionally implement `Finalizer` for error recovery
188
-
189
- ### Schedulable Classes
190
- - Read and follow: `templates/schedulable.cls`
191
- - Implement `Schedulable`
192
- - Keep `execute()` lightweight — delegate to a Batch or Queueable
193
- - Include a static method that returns a CRON expression for convenience
194
- - Document the expected schedule in ApexDoc
195
-
196
- ### DTO / Wrapper Classes
197
- - Read and follow: `templates/dto.cls`
198
- - Use `public` properties — no getters/setters unless validation is needed
199
- - Include a no-arg constructor and optionally a parameterized constructor
200
- - Implement `Comparable` if sorting is needed
201
- - Keep them serialization-friendly (no transient state unless intentional)
202
-
203
- ### Utility Classes
204
- - Read and follow: `templates/utility.cls`
205
- - All methods `public static`
206
- - Class should be `public with sharing` with a `private` constructor to prevent instantiation
207
- - Group related utilities (e.g., `StringUtil`, `DateUtil`, `CollectionUtil`)
208
- - Every method must be side-effect-free (no DML, no SOQL)
209
-
210
- ### Interfaces
211
- - Read and follow: `templates/interface.cls`
212
- - Define the contract clearly with ApexDoc on every method signature
213
- - Use meaningful names that describe the capability: `INotificationService`, `IRetryable`
214
-
215
- ### Abstract Classes
216
- - Read and follow: `templates/abstract.cls`
217
- - Provide default implementations for common behavior
218
- - Mark extension points as `protected virtual` or `protected abstract`
219
- - Include a concrete example in the ApexDoc showing how to extend
220
-
221
- ### Custom Exceptions
222
- - Read and follow: `templates/exception.cls`
223
- - Extend `Exception`
224
- - Keep them simple — Apex exceptions don't support custom constructors well
225
- - Name them descriptively: `AccountServiceException`, `IntegrationTimeoutException`
226
-
227
- ---
228
-
229
- ## Generation Workflow
230
-
231
- 1. Determine the class type from the user's request
232
- 2. Read the corresponding template from `templates/`
233
- 3. Read relevant examples from `examples/` if the class type has one
234
- 4. Apply the user's requirements to the template pattern
235
- 5. Generate the `.cls` file with full ApexDoc
236
- 6. Generate the `.cls-meta.xml` file
237
- 7. Present both files to the user
238
- 8. Include a brief note on design decisions if any non-obvious choices were made
239
-
240
- ---
241
-
242
- ## Anti-Patterns to Avoid
243
-
244
- - ❌ SOQL or DML inside loops
245
- - ❌ Hardcoded IDs or record type names (use `Schema.SObjectType` or Custom Metadata)
246
- - ❌ God classes that mix query + logic + DML
247
- - ❌ `public` fields on service classes
248
- - ❌ Returning `null` from methods that should return collections
249
- - ❌ Generic `catch (Exception e)` without re-throwing or meaningful handling
250
- - ❌ Business logic in Batch `start()` methods
251
- - ❌ Tight coupling between classes — use interfaces for extensibility
252
- - ❌ Magic strings or numbers
253
- - ❌ Methods longer than ~40 lines — break them into private helpers
@@ -1,148 +0,0 @@
1
- /**
2
- * @description Batch Apex class for identifying and flagging duplicate Account records.
3
- * Compares Accounts by Name and BillingPostalCode to find potential duplicates.
4
- * Flags duplicates by setting the Is_Potential_Duplicate__c checkbox.
5
- * Implements Database.Stateful to track results across batch chunks.
6
- * @author Generated by Apex Class Writer Skill
7
- *
8
- * @example
9
- * // Run with default batch size (200)
10
- * Id jobId = AccountDeduplicationBatch.run();
11
- *
12
- * // Run with smaller batch size for complex processing
13
- * Database.executeBatch(new AccountDeduplicationBatch(), 50);
14
- */
15
- public with sharing class AccountDeduplicationBatch implements Database.Batchable<SObject>, Database.Stateful {
16
-
17
- // ─── Constants ───────────────────────────────────────────────────────
18
- private static final Integer DEFAULT_BATCH_SIZE = 200;
19
-
20
- // ─── Stateful Tracking ───────────────────────────────────────────────
21
- private Integer totalScanned = 0;
22
- private Integer duplicatesFound = 0;
23
- private Integer totalErrors = 0;
24
- private List<String> errorMessages = new List<String>();
25
-
26
- // ─── Batchable Interface ─────────────────────────────────────────────
27
-
28
- /**
29
- * @description Queries all active Accounts that haven't already been flagged
30
- * @param bc The batch context
31
- * @return QueryLocator scoped to unflagged active Accounts
32
- */
33
- public Database.QueryLocator start(Database.BatchableContext bc) {
34
- return Database.getQueryLocator([
35
- SELECT Id, Name, BillingPostalCode, Is_Potential_Duplicate__c
36
- FROM Account
37
- WHERE IsDeleted = FALSE
38
- AND Is_Potential_Duplicate__c = FALSE
39
- ORDER BY Name ASC
40
- ]);
41
- }
42
-
43
- /**
44
- * @description Processes each batch by building a duplicate key and checking for matches.
45
- * Uses a composite key of normalized Name + BillingPostalCode.
46
- * @param bc The batch context
47
- * @param scope List of Account records in the current batch
48
- */
49
- public void execute(Database.BatchableContext bc, List<Account> scope) {
50
- totalScanned += scope.size();
51
-
52
- // Build duplicate keys for this batch
53
- Map<String, List<Account>> dupeKeyMap = new Map<String, List<Account>>();
54
- for (Account acct : scope) {
55
- String key = buildDuplicateKey(acct);
56
- if (String.isNotBlank(key)) {
57
- if (!dupeKeyMap.containsKey(key)) {
58
- dupeKeyMap.put(key, new List<Account>());
59
- }
60
- dupeKeyMap.get(key).add(acct);
61
- }
62
- }
63
-
64
- // Flag records that share a key with another record
65
- List<Account> toUpdate = new List<Account>();
66
- for (String key : dupeKeyMap.keySet()) {
67
- List<Account> group = dupeKeyMap.get(key);
68
- if (group.size() > 1) {
69
- for (Account acct : group) {
70
- toUpdate.add(new Account(
71
- Id = acct.Id,
72
- Is_Potential_Duplicate__c = true
73
- ));
74
- }
75
- }
76
- }
77
-
78
- if (!toUpdate.isEmpty()) {
79
- List<Database.SaveResult> results = Database.update(toUpdate, false);
80
- processResults(results);
81
- }
82
- }
83
-
84
- /**
85
- * @description Logs a summary of the deduplication batch run
86
- * @param bc The batch context
87
- */
88
- public void finish(Database.BatchableContext bc) {
89
- String summary = String.format(
90
- 'AccountDeduplicationBatch completed. Scanned: {0}, Duplicates flagged: {1}, Errors: {2}',
91
- new List<Object>{ totalScanned, duplicatesFound, totalErrors }
92
- );
93
-
94
- System.debug(LoggingLevel.INFO, summary);
95
-
96
- if (!errorMessages.isEmpty()) {
97
- System.debug(LoggingLevel.ERROR, 'Error details:\n' + String.join(errorMessages, '\n'));
98
- }
99
- }
100
-
101
- // ─── Private Helpers ─────────────────────────────────────────────────
102
-
103
- /**
104
- * @description Builds a normalized composite key for duplicate detection
105
- * @param acct The Account record
106
- * @return Normalized key string, or null if insufficient data
107
- */
108
- private String buildDuplicateKey(Account acct) {
109
- if (String.isBlank(acct.Name)) {
110
- return null;
111
- }
112
-
113
- String normalizedName = acct.Name.trim().toUpperCase().replaceAll('\\s+', ' ');
114
- String postalCode = (acct.BillingPostalCode ?? '').trim().toUpperCase();
115
-
116
- return normalizedName + '|' + postalCode;
117
- }
118
-
119
- /**
120
- * @description Processes DML results, tracking successes and failures
121
- * @param results List of Database.SaveResult from update operation
122
- */
123
- private void processResults(List<Database.SaveResult> results) {
124
- for (Database.SaveResult sr : results) {
125
- if (sr.isSuccess()) {
126
- duplicatesFound++;
127
- } else {
128
- totalErrors++;
129
- for (Database.Error err : sr.getErrors()) {
130
- errorMessages.add(
131
- 'Record ' + sr.getId() + ': ' +
132
- err.getStatusCode() + ' - ' + err.getMessage()
133
- );
134
- }
135
- }
136
- }
137
- }
138
-
139
- // ─── Static Helpers ──────────────────────────────────────────────────
140
-
141
- /**
142
- * @description Convenience method to execute with default batch size
143
- * @return The batch job Id
144
- */
145
- public static Id run() {
146
- return Database.executeBatch(new AccountDeduplicationBatch(), DEFAULT_BATCH_SIZE);
147
- }
148
- }
@@ -1,193 +0,0 @@
1
- /**
2
- * @description Selector class for Account queries.
3
- * Encapsulates all SOQL for Account records.
4
- * All methods return bulkified results (Lists or Maps).
5
- * @author Generated by Apex Class Writer Skill
6
- */
7
- public with sharing class AccountSelector {
8
-
9
- // ─── Field Lists ─────────────────────────────────────────────────────
10
-
11
- /**
12
- * @description Returns the default set of fields to query for Account.
13
- * Centralizes field references to keep queries DRY.
14
- * @return Comma-separated field list as a String
15
- */
16
- private static String getDefaultFields() {
17
- return String.join(
18
- new List<String>{
19
- 'Id',
20
- 'Name',
21
- 'AccountNumber',
22
- 'Type',
23
- 'Industry',
24
- 'AnnualRevenue',
25
- 'NumberOfEmployees',
26
- 'Phone',
27
- 'Website',
28
- 'OwnerId',
29
- 'CreatedDate',
30
- 'LastModifiedDate'
31
- },
32
- ', '
33
- );
34
- }
35
-
36
- /**
37
- * @description Returns fields needed for billing/territory operations
38
- * @return Comma-separated field list as a String
39
- */
40
- private static String getBillingFields() {
41
- return String.join(
42
- new List<String>{
43
- 'Id',
44
- 'Name',
45
- 'BillingStreet',
46
- 'BillingCity',
47
- 'BillingState',
48
- 'BillingPostalCode',
49
- 'BillingCountry',
50
- 'Territory__c'
51
- },
52
- ', '
53
- );
54
- }
55
-
56
- // ─── Query Methods ───────────────────────────────────────────────────
57
-
58
- /**
59
- * @description Selects Account records by their Ids
60
- * @param recordIds Set of Account Ids to query
61
- * @return List of Account records matching the provided Ids
62
- * @example
63
- * Set<Id> ids = new Set<Id>{ '001xx000003DGbY' };
64
- * List<Account> results = AccountSelector.selectByIds(ids);
65
- */
66
- public static List<Account> selectByIds(Set<Id> recordIds) {
67
- if (recordIds == null || recordIds.isEmpty()) {
68
- return new List<Account>();
69
- }
70
-
71
- return Database.query(
72
- 'SELECT ' + getDefaultFields() +
73
- ' FROM Account' +
74
- ' WHERE Id IN :recordIds'
75
- );
76
- }
77
-
78
- /**
79
- * @description Selects Account records as a Map keyed by Id
80
- * @param recordIds Set of Account Ids to query
81
- * @return Map of Id to Account
82
- */
83
- public static Map<Id, Account> selectMapByIds(Set<Id> recordIds) {
84
- return new Map<Id, Account>(selectByIds(recordIds));
85
- }
86
-
87
- /**
88
- * @description Selects Accounts with billing address fields for territory assignment
89
- * @param recordIds Set of Account Ids to query
90
- * @return List of Account records with billing address fields populated
91
- */
92
- public static List<Account> selectWithBillingAddress(Set<Id> recordIds) {
93
- if (recordIds == null || recordIds.isEmpty()) {
94
- return new List<Account>();
95
- }
96
-
97
- return Database.query(
98
- 'SELECT ' + getBillingFields() +
99
- ' FROM Account' +
100
- ' WHERE Id IN :recordIds'
101
- );
102
- }
103
-
104
- /**
105
- * @description Selects Accounts by Account Type
106
- * @param accountTypes Set of Account Type values to filter by
107
- * @return List of matching Account records
108
- * @example
109
- * List<Account> prospects = AccountSelector.selectByType(
110
- * new Set<String>{ 'Prospect', 'Customer - Direct' }
111
- * );
112
- */
113
- public static List<Account> selectByType(Set<String> accountTypes) {
114
- if (accountTypes == null || accountTypes.isEmpty()) {
115
- return new List<Account>();
116
- }
117
-
118
- return Database.query(
119
- 'SELECT ' + getDefaultFields() +
120
- ' FROM Account' +
121
- ' WHERE Type IN :accountTypes' +
122
- ' ORDER BY Name ASC'
123
- );
124
- }
125
-
126
- /**
127
- * @description Selects Accounts by Industry with a minimum annual revenue
128
- * @param industries Set of Industry values to filter by
129
- * @param minRevenue Minimum AnnualRevenue threshold
130
- * @return List of matching Account records ordered by revenue descending
131
- * @example
132
- * List<Account> techAccounts = AccountSelector.selectByIndustryAndRevenue(
133
- * new Set<String>{ 'Technology' },
134
- * 1000000
135
- * );
136
- */
137
- public static List<Account> selectByIndustryAndRevenue(Set<String> industries, Decimal minRevenue) {
138
- if (industries == null || industries.isEmpty()) {
139
- return new List<Account>();
140
- }
141
-
142
- Decimal revenueThreshold = minRevenue ?? 0;
143
-
144
- return Database.query(
145
- 'SELECT ' + getDefaultFields() +
146
- ' FROM Account' +
147
- ' WHERE Industry IN :industries' +
148
- ' AND AnnualRevenue >= :revenueThreshold' +
149
- ' ORDER BY AnnualRevenue DESC'
150
- );
151
- }
152
-
153
- /**
154
- * @description Selects Accounts with their related Contacts (subquery)
155
- * @param recordIds Set of Account Ids to query
156
- * @return List of Account records with nested Contacts
157
- */
158
- public static List<Account> selectWithContacts(Set<Id> recordIds) {
159
- if (recordIds == null || recordIds.isEmpty()) {
160
- return new List<Account>();
161
- }
162
-
163
- return [
164
- SELECT Id, Name, Type, Industry,
165
- (SELECT Id, FirstName, LastName, Email, Title
166
- FROM Contacts
167
- ORDER BY LastName ASC)
168
- FROM Account
169
- WHERE Id IN :recordIds
170
- ];
171
- }
172
-
173
- // ─── Aggregate Queries ───────────────────────────────────────────────
174
-
175
- /**
176
- * @description Returns a count of Accounts grouped by Industry
177
- * @return List of AggregateResult with Industry and record count
178
- * @example
179
- * List<AggregateResult> results = AccountSelector.countByIndustry();
180
- * for (AggregateResult ar : results) {
181
- * System.debug(ar.get('Industry') + ': ' + ar.get('cnt'));
182
- * }
183
- */
184
- public static List<AggregateResult> countByIndustry() {
185
- return [
186
- SELECT Industry, COUNT(Id) cnt
187
- FROM Account
188
- WHERE Industry != NULL
189
- GROUP BY Industry
190
- ORDER BY COUNT(Id) DESC
191
- ];
192
- }
193
- }