@xrmforge/typegen 0.8.3 → 0.8.5
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 +21 -21
- package/MIGRATION.md +194 -194
- package/dist/index.js +4 -4
- package/dist/index.js.map +1 -1
- package/docs/architecture/00-README.md +26 -26
- package/docs/architecture/01-executive-summary.md +11 -11
- package/docs/architecture/02-packages.md +110 -110
- package/docs/architecture/03-generated-types.md +172 -172
- package/docs/architecture/04-cli.md +58 -58
- package/docs/architecture/05-build.md +50 -50
- package/docs/architecture/06-incremental.md +42 -42
- package/docs/architecture/07-http-client.md +59 -59
- package/docs/architecture/08-authentication.md +18 -18
- package/docs/architecture/09-testing.md +55 -55
- package/docs/architecture/10-eslint-plugin.md +82 -82
- package/docs/architecture/11-agent-md.md +38 -38
- package/docs/architecture/12-xrm-pitfalls.md +14 -14
- package/docs/architecture/13-helpers.md +50 -50
- package/docs/architecture/14-showcases.md +21 -21
- package/docs/architecture/15-ci-cd.md +49 -49
- package/docs/architecture/16-technical-debt.md +17 -17
- package/docs/architecture/17-roadmap.md +25 -25
- package/docs/architecture/18-design-principles.md +22 -22
- package/docs/architektur/00-README.md +26 -26
- package/docs/architektur/01-zusammenfassung.md +11 -11
- package/docs/architektur/02-packages.md +110 -110
- package/docs/architektur/03-generierte-typen.md +172 -172
- package/docs/architektur/04-cli.md +58 -58
- package/docs/architektur/05-build.md +50 -50
- package/docs/architektur/06-inkrementell.md +42 -42
- package/docs/architektur/07-http-client.md +59 -59
- package/docs/architektur/08-authentifizierung.md +18 -18
- package/docs/architektur/09-testing.md +55 -55
- package/docs/architektur/10-eslint-plugin.md +82 -82
- package/docs/architektur/11-agent-md.md +38 -38
- package/docs/architektur/12-xrm-fallstricke.md +14 -14
- package/docs/architektur/13-helpers.md +50 -50
- package/docs/architektur/14-showcases.md +21 -21
- package/docs/architektur/15-ci-cd.md +49 -49
- package/docs/architektur/16-technische-schulden.md +17 -17
- package/docs/architektur/17-roadmap.md +25 -25
- package/docs/architektur/18-designprinzipien.md +22 -22
- package/package.json +1 -1
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2026 XrmForge Contributors
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 XrmForge Contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/MIGRATION.md
CHANGED
|
@@ -1,194 +1,194 @@
|
|
|
1
|
-
# XrmForge Migration Guide
|
|
2
|
-
|
|
3
|
-
How to convert legacy Dynamics 365 JavaScript to type-safe TypeScript with XrmForge.
|
|
4
|
-
|
|
5
|
-
## Breaking Changes in v0.8.0 (ES Module Output)
|
|
6
|
-
|
|
7
|
-
### What changed
|
|
8
|
-
- Generated files are now `.ts` modules with `export` statements instead of `.d.ts` files with `declare namespace`
|
|
9
|
-
- Default output directory changed from `./typings` to `./generated`
|
|
10
|
-
- Entity Fields enums are now generated in a separate `fields/` directory
|
|
11
|
-
- Action declarations and runtime code are now in a single `.ts` file per group
|
|
12
|
-
- The barrel index uses `export * from` instead of `/// <reference path />`
|
|
13
|
-
|
|
14
|
-
### Migration steps
|
|
15
|
-
1. Update your `xrmforge generate` command: replace `--output ./typings` with `--output ./generated` (or omit for the new default)
|
|
16
|
-
2. Replace namespace access with imports:
|
|
17
|
-
```typescript
|
|
18
|
-
// Before (v0.7.x):
|
|
19
|
-
type AccountForm = XrmForge.Forms.Account.AccountMainForm;
|
|
20
|
-
|
|
21
|
-
// After (v0.8.0):
|
|
22
|
-
import type { AccountMainForm } from './generated/forms/account.js';
|
|
23
|
-
```
|
|
24
|
-
3. Update your tsconfig.json: replace `"typings/**/*.d.ts"` in `include` with `"generated/**/*.ts"`
|
|
25
|
-
4. Entity Fields enums are now available:
|
|
26
|
-
```typescript
|
|
27
|
-
import { AccountFields } from './generated/fields/account.js';
|
|
28
|
-
const result = await Xrm.WebApi.retrieveRecord('account', id, `?$select=${AccountFields.Name},${AccountFields.Telephone1}`);
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
## Breaking Changes in v0.7.0
|
|
32
|
-
|
|
33
|
-
- The `@xrmforge/typegen/helpers` subpath export has been removed.
|
|
34
|
-
- All browser-safe runtime code (`select`, `parseLookup`, `parseFormattedValue`,
|
|
35
|
-
`withProgress`, Xrm constants, Action/Function executors, `typedForm`) is now
|
|
36
|
-
in the new package `@xrmforge/helpers`.
|
|
37
|
-
- Update imports: `import { select } from '@xrmforge/typegen/helpers'` becomes
|
|
38
|
-
`import { select } from '@xrmforge/helpers'`.
|
|
39
|
-
- `@xrmforge/formhelpers` has been removed. `typedForm()` is now in
|
|
40
|
-
`@xrmforge/helpers`.
|
|
41
|
-
|
|
42
|
-
## Step 1: Initialize Project
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
npx @xrmforge/cli init my-project --prefix contoso
|
|
46
|
-
cd my-project
|
|
47
|
-
npm install
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
## Step 2: Generate Types from Dataverse
|
|
51
|
-
|
|
52
|
-
```bash
|
|
53
|
-
npx xrmforge generate \
|
|
54
|
-
--url https://YOUR-ORG.crm4.dynamics.com \
|
|
55
|
-
--auth interactive \
|
|
56
|
-
--tenant-id YOUR-TENANT-ID \
|
|
57
|
-
--client-id YOUR-CLIENT-ID \
|
|
58
|
-
--entities account,contact,opportunity \
|
|
59
|
-
--output ./generated
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
This generates:
|
|
63
|
-
- `generated/entities/*.ts` - Entity interfaces with typed attributes
|
|
64
|
-
- `generated/forms/*.ts` - Form interfaces with Fields enum, Tabs enum, Subgrid enum
|
|
65
|
-
- `generated/optionsets/*.ts` - OptionSet const enums with labels
|
|
66
|
-
- `generated/fields/*.ts` - Entity Fields enums for type-safe $select queries
|
|
67
|
-
- `generated/entity-names.ts` - EntityNames const enum
|
|
68
|
-
|
|
69
|
-
## Step 3: Convert Form Scripts
|
|
70
|
-
|
|
71
|
-
### Before (legacy JavaScript):
|
|
72
|
-
|
|
73
|
-
```javascript
|
|
74
|
-
// account.js - global functions, raw strings, no type safety
|
|
75
|
-
var LM = LM || {};
|
|
76
|
-
LM.Account = {
|
|
77
|
-
onLoad: function(executionContext) {
|
|
78
|
-
var formContext = executionContext.getFormContext();
|
|
79
|
-
var name = formContext.getAttribute("name"); // generic Attribute
|
|
80
|
-
var status = formContext.getAttribute("statuscode");
|
|
81
|
-
if (status.getValue() === 1) { // magic number!
|
|
82
|
-
formContext.getControl("revenue").setVisible(true);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
};
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
### After (XrmForge TypeScript):
|
|
89
|
-
|
|
90
|
-
```typescript
|
|
91
|
-
// account-form.ts - typed, safe, autocomplete everywhere
|
|
92
|
-
import { AccountMainFormFieldsEnum as Fields } from '../../generated/forms/account.js';
|
|
93
|
-
import type { AccountMainForm } from '../../generated/forms/account.js';
|
|
94
|
-
import { StatusCode } from '../../generated/optionsets/account.js';
|
|
95
|
-
|
|
96
|
-
export function onLoad(executionContext: Xrm.Events.EventContext): void {
|
|
97
|
-
const form = executionContext.getFormContext() as AccountMainForm;
|
|
98
|
-
|
|
99
|
-
// Fields enum: compile error on typos, autocomplete in IDE
|
|
100
|
-
const name = form.getAttribute(Fields.AccountName); // StringAttribute, not generic
|
|
101
|
-
const status = form.getAttribute(Fields.StatusCode); // OptionSetAttribute
|
|
102
|
-
|
|
103
|
-
// OptionSet enum: no magic numbers
|
|
104
|
-
if (status.getValue() === StatusCode.Active) {
|
|
105
|
-
form.getControl(Fields.Revenue).setVisible(true);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
```
|
|
109
|
-
|
|
110
|
-
### Key Differences:
|
|
111
|
-
|
|
112
|
-
| Legacy | XrmForge |
|
|
113
|
-
|--------|----------|
|
|
114
|
-
| `getAttribute("name")` | `getAttribute(Fields.AccountName)` |
|
|
115
|
-
| `getValue() === 1` | `getValue() === OptionSets.StatusCode.Active` |
|
|
116
|
-
| `formContext` (untyped) | `form as AccountMainForm` (typed) |
|
|
117
|
-
| `getControl("revenue")` | `getControl(Fields.Revenue)` |
|
|
118
|
-
| No compile-time checks | Typos are compile errors |
|
|
119
|
-
|
|
120
|
-
## Step 4: Replace Common Patterns
|
|
121
|
-
|
|
122
|
-
### Lookup Values
|
|
123
|
-
|
|
124
|
-
```typescript
|
|
125
|
-
// Before:
|
|
126
|
-
var value = formContext.getAttribute("primarycontactid").getValue();
|
|
127
|
-
var id = value[0].id.replace("{","").replace("}","");
|
|
128
|
-
|
|
129
|
-
// After: use parseLookup from @xrmforge/helpers
|
|
130
|
-
import { parseLookup } from '@xrmforge/helpers';
|
|
131
|
-
const contact = parseLookup(form.getAttribute(Fields.PrimaryContactId));
|
|
132
|
-
if (contact) {
|
|
133
|
-
console.log(contact.id); // already clean GUID
|
|
134
|
-
}
|
|
135
|
-
```
|
|
136
|
-
|
|
137
|
-
### Web API Queries
|
|
138
|
-
|
|
139
|
-
```typescript
|
|
140
|
-
// Before:
|
|
141
|
-
Xrm.WebApi.retrieveMultipleRecords("account",
|
|
142
|
-
"?$select=name,revenue&$filter=statecode eq 0");
|
|
143
|
-
|
|
144
|
-
// After: use Fields enum for $select
|
|
145
|
-
import { select } from '@xrmforge/helpers';
|
|
146
|
-
import { AccountFields } from '../../generated/fields/account.js';
|
|
147
|
-
Xrm.WebApi.retrieveMultipleRecords("account",
|
|
148
|
-
`?$select=${select(AccountFields.Name, AccountFields.Revenue)}&$filter=statecode eq 0`);
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
### Form Testing
|
|
152
|
-
|
|
153
|
-
```typescript
|
|
154
|
-
// Before: no tests, or complex manual mocks
|
|
155
|
-
|
|
156
|
-
// After: @xrmforge/testing
|
|
157
|
-
import { createFormMock, fireOnChange } from '@xrmforge/testing';
|
|
158
|
-
import type { AccountMainForm, AccountMainFormMockValues } from '../../generated/forms/account.js';
|
|
159
|
-
|
|
160
|
-
const mock = createFormMock<AccountMainForm, AccountMainFormMockValues>({
|
|
161
|
-
name: 'Contoso Ltd',
|
|
162
|
-
revenue: 1000000,
|
|
163
|
-
statuscode: 1,
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
onLoad(mock.executionContext);
|
|
167
|
-
expect(mock.formContext.getControl('revenue').getVisible()).toBe(true);
|
|
168
|
-
```
|
|
169
|
-
|
|
170
|
-
## Step 5: Build
|
|
171
|
-
|
|
172
|
-
```bash
|
|
173
|
-
npx xrmforge build # IIFE bundles for D365
|
|
174
|
-
npx xrmforge build --watch # Watch mode
|
|
175
|
-
```
|
|
176
|
-
|
|
177
|
-
## Step 6: Replace Magic Numbers
|
|
178
|
-
|
|
179
|
-
Search your code for patterns like:
|
|
180
|
-
- `getValue() === 123` or `getValue() !== 456`
|
|
181
|
-
- `setValue("statuscode", 1)`
|
|
182
|
-
- Raw OptionSet values in if/switch statements
|
|
183
|
-
|
|
184
|
-
Replace with generated const enums from `generated/optionsets/`.
|
|
185
|
-
|
|
186
|
-
## Checklist
|
|
187
|
-
|
|
188
|
-
- [ ] All `getAttribute("string")` calls use Fields enum
|
|
189
|
-
- [ ] All OptionSet comparisons use const enums (no magic numbers)
|
|
190
|
-
- [ ] All `Xrm.Page` calls replaced with `formContext`
|
|
191
|
-
- [ ] Form scripts export functions (not global namespace objects)
|
|
192
|
-
- [ ] Each form script has tests using `@xrmforge/testing`
|
|
193
|
-
- [ ] `xrmforge build` produces IIFE bundles
|
|
194
|
-
- [ ] `tsc --noEmit` passes with zero errors
|
|
1
|
+
# XrmForge Migration Guide
|
|
2
|
+
|
|
3
|
+
How to convert legacy Dynamics 365 JavaScript to type-safe TypeScript with XrmForge.
|
|
4
|
+
|
|
5
|
+
## Breaking Changes in v0.8.0 (ES Module Output)
|
|
6
|
+
|
|
7
|
+
### What changed
|
|
8
|
+
- Generated files are now `.ts` modules with `export` statements instead of `.d.ts` files with `declare namespace`
|
|
9
|
+
- Default output directory changed from `./typings` to `./generated`
|
|
10
|
+
- Entity Fields enums are now generated in a separate `fields/` directory
|
|
11
|
+
- Action declarations and runtime code are now in a single `.ts` file per group
|
|
12
|
+
- The barrel index uses `export * from` instead of `/// <reference path />`
|
|
13
|
+
|
|
14
|
+
### Migration steps
|
|
15
|
+
1. Update your `xrmforge generate` command: replace `--output ./typings` with `--output ./generated` (or omit for the new default)
|
|
16
|
+
2. Replace namespace access with imports:
|
|
17
|
+
```typescript
|
|
18
|
+
// Before (v0.7.x):
|
|
19
|
+
type AccountForm = XrmForge.Forms.Account.AccountMainForm;
|
|
20
|
+
|
|
21
|
+
// After (v0.8.0):
|
|
22
|
+
import type { AccountMainForm } from './generated/forms/account.js';
|
|
23
|
+
```
|
|
24
|
+
3. Update your tsconfig.json: replace `"typings/**/*.d.ts"` in `include` with `"generated/**/*.ts"`
|
|
25
|
+
4. Entity Fields enums are now available:
|
|
26
|
+
```typescript
|
|
27
|
+
import { AccountFields } from './generated/fields/account.js';
|
|
28
|
+
const result = await Xrm.WebApi.retrieveRecord('account', id, `?$select=${AccountFields.Name},${AccountFields.Telephone1}`);
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Breaking Changes in v0.7.0
|
|
32
|
+
|
|
33
|
+
- The `@xrmforge/typegen/helpers` subpath export has been removed.
|
|
34
|
+
- All browser-safe runtime code (`select`, `parseLookup`, `parseFormattedValue`,
|
|
35
|
+
`withProgress`, Xrm constants, Action/Function executors, `typedForm`) is now
|
|
36
|
+
in the new package `@xrmforge/helpers`.
|
|
37
|
+
- Update imports: `import { select } from '@xrmforge/typegen/helpers'` becomes
|
|
38
|
+
`import { select } from '@xrmforge/helpers'`.
|
|
39
|
+
- `@xrmforge/formhelpers` has been removed. `typedForm()` is now in
|
|
40
|
+
`@xrmforge/helpers`.
|
|
41
|
+
|
|
42
|
+
## Step 1: Initialize Project
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
npx @xrmforge/cli init my-project --prefix contoso
|
|
46
|
+
cd my-project
|
|
47
|
+
npm install
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Step 2: Generate Types from Dataverse
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
npx xrmforge generate \
|
|
54
|
+
--url https://YOUR-ORG.crm4.dynamics.com \
|
|
55
|
+
--auth interactive \
|
|
56
|
+
--tenant-id YOUR-TENANT-ID \
|
|
57
|
+
--client-id YOUR-CLIENT-ID \
|
|
58
|
+
--entities account,contact,opportunity \
|
|
59
|
+
--output ./generated
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
This generates:
|
|
63
|
+
- `generated/entities/*.ts` - Entity interfaces with typed attributes
|
|
64
|
+
- `generated/forms/*.ts` - Form interfaces with Fields enum, Tabs enum, Subgrid enum
|
|
65
|
+
- `generated/optionsets/*.ts` - OptionSet const enums with labels
|
|
66
|
+
- `generated/fields/*.ts` - Entity Fields enums for type-safe $select queries
|
|
67
|
+
- `generated/entity-names.ts` - EntityNames const enum
|
|
68
|
+
|
|
69
|
+
## Step 3: Convert Form Scripts
|
|
70
|
+
|
|
71
|
+
### Before (legacy JavaScript):
|
|
72
|
+
|
|
73
|
+
```javascript
|
|
74
|
+
// account.js - global functions, raw strings, no type safety
|
|
75
|
+
var LM = LM || {};
|
|
76
|
+
LM.Account = {
|
|
77
|
+
onLoad: function(executionContext) {
|
|
78
|
+
var formContext = executionContext.getFormContext();
|
|
79
|
+
var name = formContext.getAttribute("name"); // generic Attribute
|
|
80
|
+
var status = formContext.getAttribute("statuscode");
|
|
81
|
+
if (status.getValue() === 1) { // magic number!
|
|
82
|
+
formContext.getControl("revenue").setVisible(true);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### After (XrmForge TypeScript):
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
// account-form.ts - typed, safe, autocomplete everywhere
|
|
92
|
+
import { AccountMainFormFieldsEnum as Fields } from '../../generated/forms/account.js';
|
|
93
|
+
import type { AccountMainForm } from '../../generated/forms/account.js';
|
|
94
|
+
import { StatusCode } from '../../generated/optionsets/account.js';
|
|
95
|
+
|
|
96
|
+
export function onLoad(executionContext: Xrm.Events.EventContext): void {
|
|
97
|
+
const form = executionContext.getFormContext() as AccountMainForm;
|
|
98
|
+
|
|
99
|
+
// Fields enum: compile error on typos, autocomplete in IDE
|
|
100
|
+
const name = form.getAttribute(Fields.AccountName); // StringAttribute, not generic
|
|
101
|
+
const status = form.getAttribute(Fields.StatusCode); // OptionSetAttribute
|
|
102
|
+
|
|
103
|
+
// OptionSet enum: no magic numbers
|
|
104
|
+
if (status.getValue() === StatusCode.Active) {
|
|
105
|
+
form.getControl(Fields.Revenue).setVisible(true);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Key Differences:
|
|
111
|
+
|
|
112
|
+
| Legacy | XrmForge |
|
|
113
|
+
|--------|----------|
|
|
114
|
+
| `getAttribute("name")` | `getAttribute(Fields.AccountName)` |
|
|
115
|
+
| `getValue() === 1` | `getValue() === OptionSets.StatusCode.Active` |
|
|
116
|
+
| `formContext` (untyped) | `form as AccountMainForm` (typed) |
|
|
117
|
+
| `getControl("revenue")` | `getControl(Fields.Revenue)` |
|
|
118
|
+
| No compile-time checks | Typos are compile errors |
|
|
119
|
+
|
|
120
|
+
## Step 4: Replace Common Patterns
|
|
121
|
+
|
|
122
|
+
### Lookup Values
|
|
123
|
+
|
|
124
|
+
```typescript
|
|
125
|
+
// Before:
|
|
126
|
+
var value = formContext.getAttribute("primarycontactid").getValue();
|
|
127
|
+
var id = value[0].id.replace("{","").replace("}","");
|
|
128
|
+
|
|
129
|
+
// After: use parseLookup from @xrmforge/helpers
|
|
130
|
+
import { parseLookup } from '@xrmforge/helpers';
|
|
131
|
+
const contact = parseLookup(form.getAttribute(Fields.PrimaryContactId));
|
|
132
|
+
if (contact) {
|
|
133
|
+
console.log(contact.id); // already clean GUID
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
### Web API Queries
|
|
138
|
+
|
|
139
|
+
```typescript
|
|
140
|
+
// Before:
|
|
141
|
+
Xrm.WebApi.retrieveMultipleRecords("account",
|
|
142
|
+
"?$select=name,revenue&$filter=statecode eq 0");
|
|
143
|
+
|
|
144
|
+
// After: use Fields enum for $select
|
|
145
|
+
import { select } from '@xrmforge/helpers';
|
|
146
|
+
import { AccountFields } from '../../generated/fields/account.js';
|
|
147
|
+
Xrm.WebApi.retrieveMultipleRecords("account",
|
|
148
|
+
`?$select=${select(AccountFields.Name, AccountFields.Revenue)}&$filter=statecode eq 0`);
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Form Testing
|
|
152
|
+
|
|
153
|
+
```typescript
|
|
154
|
+
// Before: no tests, or complex manual mocks
|
|
155
|
+
|
|
156
|
+
// After: @xrmforge/testing
|
|
157
|
+
import { createFormMock, fireOnChange } from '@xrmforge/testing';
|
|
158
|
+
import type { AccountMainForm, AccountMainFormMockValues } from '../../generated/forms/account.js';
|
|
159
|
+
|
|
160
|
+
const mock = createFormMock<AccountMainForm, AccountMainFormMockValues>({
|
|
161
|
+
name: 'Contoso Ltd',
|
|
162
|
+
revenue: 1000000,
|
|
163
|
+
statuscode: 1,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
onLoad(mock.executionContext);
|
|
167
|
+
expect(mock.formContext.getControl('revenue').getVisible()).toBe(true);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
## Step 5: Build
|
|
171
|
+
|
|
172
|
+
```bash
|
|
173
|
+
npx xrmforge build # IIFE bundles for D365
|
|
174
|
+
npx xrmforge build --watch # Watch mode
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
## Step 6: Replace Magic Numbers
|
|
178
|
+
|
|
179
|
+
Search your code for patterns like:
|
|
180
|
+
- `getValue() === 123` or `getValue() !== 456`
|
|
181
|
+
- `setValue("statuscode", 1)`
|
|
182
|
+
- Raw OptionSet values in if/switch statements
|
|
183
|
+
|
|
184
|
+
Replace with generated const enums from `generated/optionsets/`.
|
|
185
|
+
|
|
186
|
+
## Checklist
|
|
187
|
+
|
|
188
|
+
- [ ] All `getAttribute("string")` calls use Fields enum
|
|
189
|
+
- [ ] All OptionSet comparisons use const enums (no magic numbers)
|
|
190
|
+
- [ ] All `Xrm.Page` calls replaced with `formContext`
|
|
191
|
+
- [ ] Form scripts export functions (not global namespace objects)
|
|
192
|
+
- [ ] Each form script has tests using `@xrmforge/testing`
|
|
193
|
+
- [ ] `xrmforge build` produces IIFE bundles
|
|
194
|
+
- [ ] `tsc --noEmit` passes with zero errors
|
package/dist/index.js
CHANGED
|
@@ -2391,7 +2391,7 @@ function deriveActionName(uniquename) {
|
|
|
2391
2391
|
function generateParamsInterface(name, params) {
|
|
2392
2392
|
if (params.length === 0) return "";
|
|
2393
2393
|
const lines = [];
|
|
2394
|
-
lines.push(`export
|
|
2394
|
+
lines.push(`export type ${name}Params = {`);
|
|
2395
2395
|
for (const param of params) {
|
|
2396
2396
|
const mapped = mapCustomApiParameterType(param.type, param.logicalentityname);
|
|
2397
2397
|
const optional = param.isoptional ? "?" : "";
|
|
@@ -2400,13 +2400,13 @@ function generateParamsInterface(name, params) {
|
|
|
2400
2400
|
}
|
|
2401
2401
|
lines.push(` ${param.uniquename}${optional}: ${mapped.tsType};`);
|
|
2402
2402
|
}
|
|
2403
|
-
lines.push("}");
|
|
2403
|
+
lines.push("};");
|
|
2404
2404
|
return lines.join("\n");
|
|
2405
2405
|
}
|
|
2406
2406
|
function generateResultInterface(name, props) {
|
|
2407
2407
|
if (props.length === 0) return "";
|
|
2408
2408
|
const lines = [];
|
|
2409
|
-
lines.push(`export
|
|
2409
|
+
lines.push(`export type ${name}Result = {`);
|
|
2410
2410
|
for (const prop of props) {
|
|
2411
2411
|
const mapped = mapCustomApiParameterType(prop.type, prop.logicalentityname);
|
|
2412
2412
|
if (prop.description) {
|
|
@@ -2414,7 +2414,7 @@ function generateResultInterface(name, props) {
|
|
|
2414
2414
|
}
|
|
2415
2415
|
lines.push(` ${prop.uniquename}: ${mapped.tsType};`);
|
|
2416
2416
|
}
|
|
2417
|
-
lines.push("}");
|
|
2417
|
+
lines.push("};");
|
|
2418
2418
|
return lines.join("\n");
|
|
2419
2419
|
}
|
|
2420
2420
|
function generateActionDeclarations(apis, _isFunction, _entityName, _options = {}) {
|