@salesforce/afv-skills 1.1.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.
- package/LICENSE.txt +330 -0
- package/README.md +466 -0
- package/package.json +23 -0
- package/skills/apex-class/SKILL.md +253 -0
- package/skills/apex-class/examples/AccountDeduplicationBatch.cls +148 -0
- package/skills/apex-class/examples/AccountSelector.cls +193 -0
- package/skills/apex-class/examples/AccountService.cls +201 -0
- package/skills/apex-class/templates/abstract.cls +128 -0
- package/skills/apex-class/templates/batch.cls +125 -0
- package/skills/apex-class/templates/domain.cls +102 -0
- package/skills/apex-class/templates/dto.cls +108 -0
- package/skills/apex-class/templates/exception.cls +51 -0
- package/skills/apex-class/templates/interface.cls +25 -0
- package/skills/apex-class/templates/queueable.cls +92 -0
- package/skills/apex-class/templates/schedulable.cls +75 -0
- package/skills/apex-class/templates/selector.cls +92 -0
- package/skills/apex-class/templates/service.cls +69 -0
- package/skills/apex-class/templates/utility.cls +97 -0
- package/skills/apex-test-class/SKILL.md +101 -0
- package/skills/apex-test-class/references/assertion-patterns.md +209 -0
- package/skills/apex-test-class/references/async-testing.md +276 -0
- package/skills/apex-test-class/references/mocking-patterns.md +219 -0
- package/skills/apex-test-class/references/test-data-factory.md +176 -0
- package/skills/deployment-readiness-check/SKILL.md +257 -0
- package/skills/deployment-readiness-check/assets/deployment_checklist.md +286 -0
- package/skills/deployment-readiness-check/references/rollback_procedures.md +308 -0
- package/skills/deployment-readiness-check/scripts/check_metadata.sh +207 -0
- package/skills/salesforce-custom-application/SKILL.md +211 -0
- package/skills/salesforce-custom-field/SKILL.md +505 -0
- package/skills/salesforce-custom-lightning-type/SKILL.md +157 -0
- package/skills/salesforce-custom-object/SKILL.md +238 -0
- package/skills/salesforce-custom-tab/SKILL.md +78 -0
- package/skills/salesforce-experience-site/SKILL.md +178 -0
- package/skills/salesforce-flexipage/SKILL.md +445 -0
- package/skills/salesforce-flow/SKILL.md +368 -0
- package/skills/salesforce-fragment/SKILL.md +42 -0
- package/skills/salesforce-lightning-app-build/SKILL.md +254 -0
- package/skills/salesforce-list-view/SKILL.md +216 -0
- package/skills/salesforce-validation-rule/SKILL.md +72 -0
- package/skills/salesforce-web-app-creating-records/SKILL.md +84 -0
- package/skills/salesforce-web-app-feature/SKILL.md +70 -0
- package/skills/salesforce-web-app-list-and-create-records/SKILL.md +36 -0
- package/skills/salesforce-web-application/SKILL.md +34 -0
- package/skills/trigger-refactor-pipeline/SKILL.md +191 -0
- package/skills/trigger-refactor-pipeline/assets/test_template.apex +321 -0
- package/skills/trigger-refactor-pipeline/references/handler_patterns.md +442 -0
- package/skills/trigger-refactor-pipeline/scripts/analyze_trigger.py +258 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Service class for Account business logic.
|
|
3
|
+
* Provides account deduplication, enrichment, and territory assignment.
|
|
4
|
+
* Delegates queries to AccountSelector and SObject manipulation to AccountDomain.
|
|
5
|
+
* @author Generated by Apex Class Writer Skill
|
|
6
|
+
*/
|
|
7
|
+
public with sharing class AccountService {
|
|
8
|
+
|
|
9
|
+
// ─── Constants ───────────────────────────────────────────────────────
|
|
10
|
+
private static final String ERROR_NULL_INPUT = 'Input cannot be null or empty.';
|
|
11
|
+
private static final String ERROR_MERGE_FAILED = 'Account merge failed for master Id: ';
|
|
12
|
+
private static final Integer MAX_MERGE_BATCH = 3;
|
|
13
|
+
|
|
14
|
+
// ─── Public API ──────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @description Merges duplicate Account records into a master record.
|
|
18
|
+
* The master record retains its field values; child records are reparented.
|
|
19
|
+
* @param masterIds Map of master Account Id to Set of duplicate Account Ids to merge
|
|
20
|
+
* @return List of master Account Ids that were successfully merged
|
|
21
|
+
* @throws AccountServiceException if merge processing fails
|
|
22
|
+
* @example
|
|
23
|
+
* Map<Id, Set<Id>> mergeMap = new Map<Id, Set<Id>>{
|
|
24
|
+
* masterAcctId => new Set<Id>{ dupeId1, dupeId2 }
|
|
25
|
+
* };
|
|
26
|
+
* List<Id> mergedIds = AccountService.mergeAccounts(mergeMap);
|
|
27
|
+
*/
|
|
28
|
+
public static List<Id> mergeAccounts(Map<Id, Set<Id>> mergeMap) {
|
|
29
|
+
if (mergeMap == null || mergeMap.isEmpty()) {
|
|
30
|
+
throw new AccountServiceException(ERROR_NULL_INPUT);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Collect all Ids for a single query
|
|
34
|
+
Set<Id> allIds = new Set<Id>();
|
|
35
|
+
allIds.addAll(mergeMap.keySet());
|
|
36
|
+
for (Set<Id> dupeIds : mergeMap.values()) {
|
|
37
|
+
allIds.addAll(dupeIds);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Query all records at once via Selector
|
|
41
|
+
Map<Id, Account> accountMap = AccountSelector.selectMapByIds(allIds);
|
|
42
|
+
|
|
43
|
+
List<Id> successfulMerges = new List<Id>();
|
|
44
|
+
List<String> errors = new List<String>();
|
|
45
|
+
|
|
46
|
+
for (Id masterId : mergeMap.keySet()) {
|
|
47
|
+
Account master = accountMap.get(masterId);
|
|
48
|
+
if (master == null) {
|
|
49
|
+
errors.add('Master account not found: ' + masterId);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
List<Account> duplicates = new List<Account>();
|
|
54
|
+
for (Id dupeId : mergeMap.get(masterId)) {
|
|
55
|
+
Account dupe = accountMap.get(dupeId);
|
|
56
|
+
if (dupe != null) {
|
|
57
|
+
duplicates.add(dupe);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
// Apex merge supports up to 3 records at a time
|
|
63
|
+
for (List<Account> chunk : chunkAccounts(duplicates, MAX_MERGE_BATCH)) {
|
|
64
|
+
Database.merge(master, chunk);
|
|
65
|
+
}
|
|
66
|
+
successfulMerges.add(masterId);
|
|
67
|
+
} catch (DmlException e) {
|
|
68
|
+
errors.add(ERROR_MERGE_FAILED + masterId + ' - ' + e.getMessage());
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!errors.isEmpty()) {
|
|
73
|
+
System.debug(LoggingLevel.WARN, 'Merge errors: ' + String.join(errors, '\n'));
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return successfulMerges;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @description Assigns accounts to territories based on Billing State/Country.
|
|
81
|
+
* Uses Custom Metadata Type (Territory_Mapping__mdt) for mappings.
|
|
82
|
+
* @param accountIds Set of Account Ids to assign territories for
|
|
83
|
+
* @return Number of accounts successfully updated
|
|
84
|
+
* @throws AccountServiceException if territory assignment fails
|
|
85
|
+
*/
|
|
86
|
+
public static Integer assignTerritories(Set<Id> accountIds) {
|
|
87
|
+
if (accountIds == null || accountIds.isEmpty()) {
|
|
88
|
+
throw new AccountServiceException(ERROR_NULL_INPUT);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
List<Account> accounts = AccountSelector.selectWithBillingAddress(accountIds);
|
|
92
|
+
Map<String, String> territoryMap = loadTerritoryMappings();
|
|
93
|
+
|
|
94
|
+
List<Account> toUpdate = new List<Account>();
|
|
95
|
+
for (Account acct : accounts) {
|
|
96
|
+
String key = buildTerritoryKey(acct.BillingState, acct.BillingCountry);
|
|
97
|
+
String territory = territoryMap.get(key);
|
|
98
|
+
|
|
99
|
+
if (territory != null && territory != acct.Territory__c) {
|
|
100
|
+
toUpdate.add(new Account(
|
|
101
|
+
Id = acct.Id,
|
|
102
|
+
Territory__c = territory
|
|
103
|
+
));
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (!toUpdate.isEmpty()) {
|
|
108
|
+
List<Database.SaveResult> results = Database.update(toUpdate, false);
|
|
109
|
+
return countSuccesses(results);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// ─── Convenience Overloads ───────────────────────────────────────────
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* @description Single-account territory assignment convenience method
|
|
119
|
+
* @param accountId The Account Id to assign a territory for
|
|
120
|
+
* @return 1 if updated, 0 if no change needed
|
|
121
|
+
*/
|
|
122
|
+
public static Integer assignTerritory(Id accountId) {
|
|
123
|
+
return assignTerritories(new Set<Id>{ accountId });
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// ─── Private Helpers ─────────────────────────────────────────────────
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* @description Loads territory mappings from Custom Metadata
|
|
130
|
+
* @return Map of territory key (State:Country) to territory name
|
|
131
|
+
*/
|
|
132
|
+
private static Map<String, String> loadTerritoryMappings() {
|
|
133
|
+
Map<String, String> mappings = new Map<String, String>();
|
|
134
|
+
for (Territory_Mapping__mdt mapping : Territory_Mapping__mdt.getAll().values()) {
|
|
135
|
+
String key = buildTerritoryKey(mapping.State__c, mapping.Country__c);
|
|
136
|
+
mappings.put(key, mapping.Territory_Name__c);
|
|
137
|
+
}
|
|
138
|
+
return mappings;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* @description Builds a consistent territory lookup key
|
|
143
|
+
* @param state The billing state
|
|
144
|
+
* @param country The billing country
|
|
145
|
+
* @return A normalized key string
|
|
146
|
+
*/
|
|
147
|
+
private static String buildTerritoryKey(String state, String country) {
|
|
148
|
+
return (state ?? '').toUpperCase() + ':' + (country ?? '').toUpperCase();
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @description Chunks a list of Accounts into sublists of the given size
|
|
153
|
+
* @param accounts The accounts to chunk
|
|
154
|
+
* @param chunkSize Maximum chunk size
|
|
155
|
+
* @return List of account sublists
|
|
156
|
+
*/
|
|
157
|
+
private static List<List<Account>> chunkAccounts(List<Account> accounts, Integer chunkSize) {
|
|
158
|
+
List<List<Account>> chunks = new List<List<Account>>();
|
|
159
|
+
List<Account> current = new List<Account>();
|
|
160
|
+
|
|
161
|
+
for (Account acct : accounts) {
|
|
162
|
+
current.add(acct);
|
|
163
|
+
if (current.size() == chunkSize) {
|
|
164
|
+
chunks.add(current);
|
|
165
|
+
current = new List<Account>();
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (!current.isEmpty()) {
|
|
169
|
+
chunks.add(current);
|
|
170
|
+
}
|
|
171
|
+
return chunks;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* @description Counts successful results from a DML operation
|
|
176
|
+
* @param results List of Database.SaveResult
|
|
177
|
+
* @return Count of successful operations
|
|
178
|
+
*/
|
|
179
|
+
private static Integer countSuccesses(List<Database.SaveResult> results) {
|
|
180
|
+
Integer count = 0;
|
|
181
|
+
for (Database.SaveResult sr : results) {
|
|
182
|
+
if (sr.isSuccess()) {
|
|
183
|
+
count++;
|
|
184
|
+
} else {
|
|
185
|
+
for (Database.Error err : sr.getErrors()) {
|
|
186
|
+
System.debug(LoggingLevel.WARN,
|
|
187
|
+
'Update failed for ' + sr.getId() + ': ' + err.getMessage()
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return count;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// ─── Exception ───────────────────────────────────────────────────────
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* @description Custom exception for AccountService errors
|
|
199
|
+
*/
|
|
200
|
+
public class AccountServiceException extends Exception {}
|
|
201
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Abstract base class for {describe the family of classes this serves}.
|
|
3
|
+
* Provides common behavior and defines extension points for subclasses.
|
|
4
|
+
* Subclasses must implement the abstract methods to provide specific behavior.
|
|
5
|
+
* @author Generated by Apex Class Writer Skill
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Extending this abstract class:
|
|
9
|
+
* public class SalesforceIntegrationService extends {ClassName} {
|
|
10
|
+
* protected override String getEndpoint() {
|
|
11
|
+
* return 'callout:Salesforce_API/services/data/v62.0';
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* protected override Map<String, String> getHeaders() {
|
|
15
|
+
* return new Map<String, String>{
|
|
16
|
+
* 'Content-Type' => 'application/json'
|
|
17
|
+
* };
|
|
18
|
+
* }
|
|
19
|
+
* }
|
|
20
|
+
*/
|
|
21
|
+
public abstract with sharing class {ClassName} {
|
|
22
|
+
|
|
23
|
+
// ─── Constants ───────────────────────────────────────────────────────
|
|
24
|
+
private static final Integer DEFAULT_TIMEOUT_MS = 30000;
|
|
25
|
+
|
|
26
|
+
// ─── Protected State ─────────────────────────────────────────────────
|
|
27
|
+
protected Integer timeoutMs;
|
|
28
|
+
|
|
29
|
+
// ─── Constructor ─────────────────────────────────────────────────────
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @description Initializes the base class with default configuration
|
|
33
|
+
*/
|
|
34
|
+
protected {ClassName}() {
|
|
35
|
+
this.timeoutMs = DEFAULT_TIMEOUT_MS;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ─── Abstract Methods (must be implemented by subclasses) ────────────
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @description Returns the endpoint URL for this integration.
|
|
42
|
+
* Subclasses must provide their specific endpoint.
|
|
43
|
+
* @return The endpoint URL as a String
|
|
44
|
+
*/
|
|
45
|
+
protected abstract String getEndpoint();
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @description Returns the HTTP headers for this integration.
|
|
49
|
+
* Subclasses define their own required headers.
|
|
50
|
+
* @return Map of header name to header value
|
|
51
|
+
*/
|
|
52
|
+
protected abstract Map<String, String> getHeaders();
|
|
53
|
+
|
|
54
|
+
// ─── Virtual Methods (can be overridden by subclasses) ───────────────
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* @description Hook called before the main operation executes.
|
|
58
|
+
* Override to add pre-processing logic.
|
|
59
|
+
* Default implementation does nothing.
|
|
60
|
+
* @param context Map of contextual data
|
|
61
|
+
*/
|
|
62
|
+
protected virtual void beforeExecute(Map<String, Object> context) {
|
|
63
|
+
// Default: no-op — override in subclass if needed
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* @description Hook called after the main operation completes.
|
|
68
|
+
* Override to add post-processing logic.
|
|
69
|
+
* Default implementation does nothing.
|
|
70
|
+
* @param context Map of contextual data
|
|
71
|
+
* @param result The result from the operation
|
|
72
|
+
*/
|
|
73
|
+
protected virtual void afterExecute(Map<String, Object> context, Object result) {
|
|
74
|
+
// Default: no-op — override in subclass if needed
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ─── Template Method (common workflow) ───────────────────────────────
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @description Executes the operation using the template method pattern.
|
|
81
|
+
* Calls beforeExecute → doExecute → afterExecute in sequence.
|
|
82
|
+
* @param context Map of data needed for the operation
|
|
83
|
+
* @return The result of the operation
|
|
84
|
+
*/
|
|
85
|
+
public Object execute(Map<String, Object> context) {
|
|
86
|
+
beforeExecute(context);
|
|
87
|
+
|
|
88
|
+
Object result;
|
|
89
|
+
try {
|
|
90
|
+
result = doExecute(context);
|
|
91
|
+
} catch (Exception e) {
|
|
92
|
+
handleError(e);
|
|
93
|
+
throw e;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
afterExecute(context, result);
|
|
97
|
+
return result;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ─── Protected Helpers ───────────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @description Core execution logic — override this for the main operation.
|
|
104
|
+
* Default implementation throws — subclass must provide implementation.
|
|
105
|
+
* @param context Map of data needed for the operation
|
|
106
|
+
* @return The result of the operation
|
|
107
|
+
*/
|
|
108
|
+
protected virtual Object doExecute(Map<String, Object> context) {
|
|
109
|
+
throw new UnsupportedOperationException(
|
|
110
|
+
'Subclass must override doExecute()'
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* @description Error handler called when doExecute throws.
|
|
116
|
+
* Override to customize error handling (e.g., logging, retry).
|
|
117
|
+
* @param e The exception that was thrown
|
|
118
|
+
*/
|
|
119
|
+
protected virtual void handleError(Exception e) {
|
|
120
|
+
System.debug(LoggingLevel.ERROR,
|
|
121
|
+
this.toString() + ' error: ' + e.getMessage() + '\n' + e.getStackTraceString()
|
|
122
|
+
);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ─── Exception ───────────────────────────────────────────────────────
|
|
126
|
+
|
|
127
|
+
public class UnsupportedOperationException extends Exception {}
|
|
128
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Batch Apex class for {describe the batch operation}.
|
|
3
|
+
* Processes {SObject} records in configurable batch sizes.
|
|
4
|
+
* Implements Database.Stateful to track cumulative results across chunks.
|
|
5
|
+
* @author Generated by Apex Class Writer Skill
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Execute with default batch size
|
|
9
|
+
* Database.executeBatch(new {ClassName}());
|
|
10
|
+
*
|
|
11
|
+
* // Execute with custom batch size
|
|
12
|
+
* Database.executeBatch(new {ClassName}(), 100);
|
|
13
|
+
*/
|
|
14
|
+
public with sharing class {ClassName} implements Database.Batchable<SObject>, Database.Stateful {
|
|
15
|
+
|
|
16
|
+
// ─── Constants ───────────────────────────────────────────────────────
|
|
17
|
+
private static final Integer DEFAULT_BATCH_SIZE = 200;
|
|
18
|
+
|
|
19
|
+
// ─── Stateful Tracking ───────────────────────────────────────────────
|
|
20
|
+
private Integer totalProcessed = 0;
|
|
21
|
+
private Integer totalErrors = 0;
|
|
22
|
+
private List<String> errorMessages = new List<String>();
|
|
23
|
+
|
|
24
|
+
// ─── Constructor ─────────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @description Default constructor
|
|
28
|
+
*/
|
|
29
|
+
public {ClassName}() {
|
|
30
|
+
// Default configuration
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ─── Batchable Interface ─────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @description Defines the scope of records to process.
|
|
37
|
+
* Uses Database.QueryLocator for efficient large-dataset processing.
|
|
38
|
+
* @param bc The batch context
|
|
39
|
+
* @return QueryLocator for the records to process
|
|
40
|
+
*/
|
|
41
|
+
public Database.QueryLocator start(Database.BatchableContext bc) {
|
|
42
|
+
return Database.getQueryLocator([
|
|
43
|
+
SELECT Id, Name
|
|
44
|
+
// TODO: Add fields needed for processing
|
|
45
|
+
FROM {SObject}
|
|
46
|
+
// TODO: Add WHERE clause to scope the records
|
|
47
|
+
]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* @description Processes each batch of records.
|
|
52
|
+
* Uses Database.update with allOrNone=false for partial success handling.
|
|
53
|
+
* @param bc The batch context
|
|
54
|
+
* @param scope List of {SObject} records in the current batch
|
|
55
|
+
*/
|
|
56
|
+
public void execute(Database.BatchableContext bc, List<{SObject}> scope) {
|
|
57
|
+
List<{SObject}> recordsToUpdate = new List<{SObject}>();
|
|
58
|
+
|
|
59
|
+
for ({SObject} record : scope) {
|
|
60
|
+
// TODO: Apply business logic to each record
|
|
61
|
+
recordsToUpdate.add(record);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (!recordsToUpdate.isEmpty()) {
|
|
65
|
+
List<Database.SaveResult> results = Database.update(recordsToUpdate, false);
|
|
66
|
+
processResults(results);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @description Performs post-processing after all batches complete.
|
|
72
|
+
* Logs a summary of the batch execution.
|
|
73
|
+
* @param bc The batch context
|
|
74
|
+
*/
|
|
75
|
+
public void finish(Database.BatchableContext bc) {
|
|
76
|
+
String summary = String.format(
|
|
77
|
+
'{0} completed. Processed: {1}, Errors: {2}',
|
|
78
|
+
new List<Object>{
|
|
79
|
+
{ClassName}.class.getName(),
|
|
80
|
+
totalProcessed,
|
|
81
|
+
totalErrors
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
|
|
85
|
+
System.debug(LoggingLevel.INFO, summary);
|
|
86
|
+
|
|
87
|
+
if (!errorMessages.isEmpty()) {
|
|
88
|
+
System.debug(LoggingLevel.ERROR, 'Error details: ' + String.join(errorMessages, '\n'));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// TODO: Send completion notification email or post to chatter if needed
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
// ─── Private Helpers ─────────────────────────────────────────────────
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* @description Processes Database.SaveResult list, tracking successes and failures
|
|
98
|
+
* @param results List of SaveResult from a DML operation
|
|
99
|
+
*/
|
|
100
|
+
private void processResults(List<Database.SaveResult> results) {
|
|
101
|
+
for (Database.SaveResult result : results) {
|
|
102
|
+
if (result.isSuccess()) {
|
|
103
|
+
totalProcessed++;
|
|
104
|
+
} else {
|
|
105
|
+
totalErrors++;
|
|
106
|
+
for (Database.Error err : result.getErrors()) {
|
|
107
|
+
errorMessages.add(
|
|
108
|
+
'Record ' + result.getId() + ': ' +
|
|
109
|
+
err.getStatusCode() + ' - ' + err.getMessage()
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
// ─── Static Helpers ──────────────────────────────────────────────────
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @description Convenience method to execute with default batch size
|
|
120
|
+
* @return The batch job Id
|
|
121
|
+
*/
|
|
122
|
+
public static Id run() {
|
|
123
|
+
return Database.executeBatch(new {ClassName}(), DEFAULT_BATCH_SIZE);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Domain class for {SObject}.
|
|
3
|
+
* Encapsulates field-level defaults, derivations, and validations.
|
|
4
|
+
* Operates only on in-memory SObject data — no SOQL or DML.
|
|
5
|
+
* @author Generated by Apex Class Writer Skill
|
|
6
|
+
*/
|
|
7
|
+
public with sharing class {SObject}Domain {
|
|
8
|
+
|
|
9
|
+
// ─── Constants ───────────────────────────────────────────────────────
|
|
10
|
+
// TODO: Add constants for default values, statuses, etc.
|
|
11
|
+
|
|
12
|
+
// ─── Field Defaults ──────────────────────────────────────────────────
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @description Applies default field values to new {SObject} records.
|
|
16
|
+
* Call this before insert to ensure consistent defaults.
|
|
17
|
+
* @param records List of {SObject} records to apply defaults to
|
|
18
|
+
*/
|
|
19
|
+
public static void applyDefaults(List<{SObject}> records) {
|
|
20
|
+
if (records == null || records.isEmpty()) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
for ({SObject} record : records) {
|
|
25
|
+
// TODO: Set default field values
|
|
26
|
+
// Example:
|
|
27
|
+
// if (record.Status__c == null) {
|
|
28
|
+
// record.Status__c = DEFAULT_STATUS;
|
|
29
|
+
// }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// ─── Derivations ────────────────────────────────────────────────────
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* @description Derives calculated field values based on other fields.
|
|
37
|
+
* Call this before insert and before update.
|
|
38
|
+
* @param records List of {SObject} records to derive values for
|
|
39
|
+
*/
|
|
40
|
+
public static void deriveFields(List<{SObject}> records) {
|
|
41
|
+
if (records == null || records.isEmpty()) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
for ({SObject} record : records) {
|
|
46
|
+
// TODO: Derive calculated field values
|
|
47
|
+
// Example:
|
|
48
|
+
// record.FullAddress__c = buildFullAddress(record);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// ─── Validations ────────────────────────────────────────────────────
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* @description Validates {SObject} records and adds errors for any violations.
|
|
56
|
+
* Call this before insert and before update.
|
|
57
|
+
* @param records List of {SObject} records to validate
|
|
58
|
+
*/
|
|
59
|
+
public static void validate(List<{SObject}> records) {
|
|
60
|
+
if (records == null || records.isEmpty()) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
for ({SObject} record : records) {
|
|
65
|
+
// TODO: Add validation rules
|
|
66
|
+
// Example:
|
|
67
|
+
// if (String.isBlank(record.Name)) {
|
|
68
|
+
// record.addError('Name is required.');
|
|
69
|
+
// }
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ─── Comparisons ────────────────────────────────────────────────────
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* @description Determines which fields have changed between old and new record versions.
|
|
77
|
+
* Useful in before update context.
|
|
78
|
+
* @param oldRecord The previous version of the record
|
|
79
|
+
* @param newRecord The current version of the record
|
|
80
|
+
* @return Set of field API names that have changed
|
|
81
|
+
*/
|
|
82
|
+
public static Set<String> getChangedFields({SObject} oldRecord, {SObject} newRecord) {
|
|
83
|
+
Set<String> changedFields = new Set<String>();
|
|
84
|
+
|
|
85
|
+
if (oldRecord == null || newRecord == null) {
|
|
86
|
+
return changedFields;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.{SObject}.fields.getMap();
|
|
90
|
+
for (String fieldName : fieldMap.keySet()) {
|
|
91
|
+
if (oldRecord.get(fieldName) != newRecord.get(fieldName)) {
|
|
92
|
+
changedFields.add(fieldName);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
return changedFields;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ─── Private Helpers ─────────────────────────────────────────────────
|
|
100
|
+
|
|
101
|
+
// TODO: Add private helper methods as needed
|
|
102
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @description Data Transfer Object for {describe the data this DTO represents}.
|
|
3
|
+
* Used to pass structured data between layers without exposing SObjects.
|
|
4
|
+
* Serialization-friendly for use with JSON.serialize/deserialize and API responses.
|
|
5
|
+
* @author Generated by Apex Class Writer Skill
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* // Create from constructor
|
|
9
|
+
* {ClassName} dto = new {ClassName}('value1', 42);
|
|
10
|
+
*
|
|
11
|
+
* // Deserialize from JSON
|
|
12
|
+
* {ClassName} dto = ({ClassName}) JSON.deserialize(jsonString, {ClassName}.class);
|
|
13
|
+
*/
|
|
14
|
+
public with sharing class {ClassName} {
|
|
15
|
+
|
|
16
|
+
// ─── Properties ──────────────────────────────────────────────────────
|
|
17
|
+
|
|
18
|
+
/** @description {Describe this property} */
|
|
19
|
+
public String name { get; set; }
|
|
20
|
+
|
|
21
|
+
/** @description {Describe this property} */
|
|
22
|
+
public Id recordId { get; set; }
|
|
23
|
+
|
|
24
|
+
/** @description {Describe this property} */
|
|
25
|
+
public Boolean isActive { get; set; }
|
|
26
|
+
|
|
27
|
+
/** @description {Describe this property} */
|
|
28
|
+
public List<String> tags { get; set; }
|
|
29
|
+
|
|
30
|
+
// TODO: Add additional properties as needed
|
|
31
|
+
|
|
32
|
+
// ─── Constructors ────────────────────────────────────────────────────
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @description No-arg constructor for deserialization compatibility
|
|
36
|
+
*/
|
|
37
|
+
public {ClassName}() {
|
|
38
|
+
this.tags = new List<String>();
|
|
39
|
+
this.isActive = false;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @description Parameterized constructor for convenience
|
|
44
|
+
* @param name The name value
|
|
45
|
+
* @param recordId The associated record Id
|
|
46
|
+
*/
|
|
47
|
+
public {ClassName}(String name, Id recordId) {
|
|
48
|
+
this();
|
|
49
|
+
this.name = name;
|
|
50
|
+
this.recordId = recordId;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// ─── Factory Methods ─────────────────────────────────────────────────
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* @description Creates a DTO instance from an SObject record
|
|
57
|
+
* @param record The source {SObject} record
|
|
58
|
+
* @return A populated {ClassName} instance
|
|
59
|
+
*/
|
|
60
|
+
public static {ClassName} fromSObject(SObject record) {
|
|
61
|
+
if (record == null) {
|
|
62
|
+
return new {ClassName}();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
{ClassName} dto = new {ClassName}();
|
|
66
|
+
dto.recordId = record.Id;
|
|
67
|
+
dto.name = (String) record.get('Name');
|
|
68
|
+
// TODO: Map additional fields
|
|
69
|
+
|
|
70
|
+
return dto;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @description Creates a list of DTOs from a list of SObject records
|
|
75
|
+
* @param records The source records
|
|
76
|
+
* @return List of populated {ClassName} instances
|
|
77
|
+
*/
|
|
78
|
+
public static List<{ClassName}> fromSObjects(List<SObject> records) {
|
|
79
|
+
List<{ClassName}> dtos = new List<{ClassName}>();
|
|
80
|
+
if (records == null) {
|
|
81
|
+
return dtos;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
for (SObject record : records) {
|
|
85
|
+
dtos.add(fromSObject(record));
|
|
86
|
+
}
|
|
87
|
+
return dtos;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ─── Utility Methods ─────────────────────────────────────────────────
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @description Serializes this DTO to a JSON string
|
|
94
|
+
* @return JSON representation of this DTO
|
|
95
|
+
*/
|
|
96
|
+
public String toJson() {
|
|
97
|
+
return JSON.serialize(this);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @description Deserializes a JSON string into a {ClassName} instance
|
|
102
|
+
* @param jsonString The JSON string to deserialize
|
|
103
|
+
* @return A {ClassName} instance
|
|
104
|
+
*/
|
|
105
|
+
public static {ClassName} fromJson(String jsonString) {
|
|
106
|
+
return ({ClassName}) JSON.deserialize(jsonString, {ClassName}.class);
|
|
107
|
+
}
|
|
108
|
+
}
|