@salesforce/afv-skills 1.6.5 → 1.6.6
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/package.json +3 -3
- package/skills/generating-experience-lwr-site/SKILL.md +11 -1
- package/skills/generating-experience-lwr-site/docs/update-site-urls.md +100 -0
- package/skills/generating-ui-bundle-site/SKILL.md +13 -1
- package/skills/generating-ui-bundle-site/docs/configure-metadata-digital-experience.md +4 -2
- package/skills/generating-ui-bundle-site/docs/update-site-urls.md +100 -0
- package/skills/implementing-ui-bundle-agentforce-conversation-client/SKILL.md +216 -37
- package/skills/implementing-ui-bundle-agentforce-conversation-client/references/style-tokens.md +168 -21
- package/skills/searching-media/SKILL.md +1 -1
- package/skills/using-ui-bundle-salesforce-data/SKILL.md +337 -91
- package/skills/using-ui-bundle-salesforce-data/references/mutation-query-generation.md +0 -140
- package/skills/using-ui-bundle-salesforce-data/references/query-testing.md +0 -78
- package/skills/using-ui-bundle-salesforce-data/references/read-query-generation.md +0 -307
- package/skills/using-ui-bundle-salesforce-data/references/schema-introspection.md +0 -53
- package/skills/using-ui-bundle-salesforce-data/references/ui-bundle-integration.md +0 -221
|
@@ -5,27 +5,18 @@ description: "Salesforce data access for reading, writing, and querying records
|
|
|
5
5
|
|
|
6
6
|
# Salesforce Data Access
|
|
7
7
|
|
|
8
|
-
## When to Use
|
|
9
|
-
|
|
10
|
-
Use this skill when the user wants to:
|
|
11
|
-
|
|
12
|
-
- **Fetch or display Salesforce data** — Query records (Account, Contact, Opportunity, custom objects) to show in a component
|
|
13
|
-
- **Create, update, or delete records** — Perform mutations on Salesforce data
|
|
14
|
-
- **Add data fetching to a component** — Wire up a React component to Salesforce data
|
|
15
|
-
- **Call REST APIs** — Use Connect REST, Apex REST, or UI API endpoints
|
|
16
|
-
- **Explore the org schema** — Discover available objects, fields, or relationships
|
|
17
|
-
|
|
18
8
|
## Data SDK Requirement
|
|
19
9
|
|
|
20
10
|
> **All Salesforce data access MUST use the Data SDK** (`@salesforce/sdk-data`). The SDK handles authentication, CSRF, and base URL resolution.
|
|
21
11
|
|
|
22
12
|
```typescript
|
|
23
13
|
import { createDataSDK, gql } from "@salesforce/sdk-data";
|
|
14
|
+
import type { ResponseTypeQuery } from "../graphql-operations-types";
|
|
24
15
|
|
|
25
16
|
const sdk = await createDataSDK();
|
|
26
17
|
|
|
27
18
|
// GraphQL for record queries/mutations (PREFERRED)
|
|
28
|
-
const response = await sdk.graphql?.<
|
|
19
|
+
const response = await sdk.graphql?.<ResponseTypeQuery>(query, variables);
|
|
29
20
|
|
|
30
21
|
// REST for Connect REST, Apex REST, UI API (when GraphQL insufficient)
|
|
31
22
|
const res = await sdk.fetch?.("/services/apexrest/my-resource");
|
|
@@ -33,6 +24,16 @@ const res = await sdk.fetch?.("/services/apexrest/my-resource");
|
|
|
33
24
|
|
|
34
25
|
**Always use optional chaining** (`sdk.graphql?.()`, `sdk.fetch?.()`) — these methods may be undefined in some surfaces.
|
|
35
26
|
|
|
27
|
+
## Preconditions — verify before starting
|
|
28
|
+
|
|
29
|
+
| # | Requirement | How to verify | If missing |
|
|
30
|
+
|---|-------------|---------------|------------|
|
|
31
|
+
| 1 | `@salesforce/sdk-data` installed | Check `package.json` in the UI bundle dir | Cannot proceed — tell user to install it |
|
|
32
|
+
| 2 | `schema.graphql` at project root | Check if file exists | Run `npm run graphql:schema` from UI bundle dir |
|
|
33
|
+
| 3 | Custom objects/fields deployed | Run `graphql-search.sh <Entity>` — no output means not deployed | Ask user to deploy metadata and assign permission sets |
|
|
34
|
+
|
|
35
|
+
**If preconditions are not met**, you may scaffold components, routes, layout, and UI logic, but use empty arrays / `null` for data and mark query locations with `// TODO: add query after schema verification` and include in the plan to go back, resolve requirements and write the GraphQL. Do not write GraphQL query strings until the schema workflow is complete.
|
|
36
|
+
|
|
36
37
|
## Supported APIs
|
|
37
38
|
|
|
38
39
|
**Only the following APIs are permitted.** Any endpoint not listed here must not be used.
|
|
@@ -71,90 +72,241 @@ const res = await sdk.fetch?.("/services/apexrest/my-resource");
|
|
|
71
72
|
|
|
72
73
|
These rules exist because Salesforce GraphQL has platform-specific behaviors that differ from standard GraphQL. Violations cause silent runtime failures.
|
|
73
74
|
|
|
74
|
-
1. **
|
|
75
|
+
1. **HTTP 200 does not mean success** — Salesforce returns HTTP 200 even when operations fail. **Always parse the `errors` array in the response body.**
|
|
76
|
+
|
|
77
|
+
2. **Schema is the single source of truth** — Every entity name, field name, and type must be confirmed via the schema search script before use in a query. Never guess — Salesforce field names are case-sensitive, relationships may be polymorphic, and custom objects use suffixes (`__c`, `__e`). Objects added to UI API in v60+ may use a `_Record` suffix (e.g., `FeedItem_Record` instead of `FeedItem`).
|
|
78
|
+
|
|
79
|
+
3. **`@optional` on all record fields** (read queries) — Salesforce field-level security (FLS) causes queries to fail entirely if the user lacks access to even one field. The `@optional` directive (v65+) tells the server to omit inaccessible fields instead of failing. Apply it to every scalar field, parent relationship, and child relationship. Consuming code must use optional chaining (`?.`) and nullish coalescing (`??`).
|
|
75
80
|
|
|
76
|
-
|
|
81
|
+
4. **Correct mutation syntax** — Mutations wrap under `uiapi(input: { allOrNone: true/false })`, not bare `uiapi { ... }`. Always set `allOrNone` explicitly. Output fields cannot include child relationships or navigated reference fields.
|
|
77
82
|
|
|
78
|
-
|
|
83
|
+
5. **Explicit pagination** — Always include `first:` in every query. If omitted, the server silently defaults to 10 records. Include `pageInfo { hasNextPage endCursor }` for any query that may need pagination. Forward-only (`first`/`after`) — `last`/`before` are unsupported.
|
|
79
84
|
|
|
80
|
-
|
|
85
|
+
6. **SOQL-derived execution limits** — Max 10 subqueries per request, max 5 levels of child-to-parent traversal, max 1 level of parent-to-child (no grandchildren), max 2,000 records per subquery. If a query would exceed these, split into multiple requests.
|
|
81
86
|
|
|
82
|
-
|
|
87
|
+
7. **Only requested fields** — Only generate fields the user explicitly asked for. Do NOT add extra fields.
|
|
83
88
|
|
|
84
|
-
|
|
89
|
+
8. **Compound fields** — When filtering or ordering, use constituent fields (e.g., `BillingCity`, `BillingCountry`), not the compound wrapper (`BillingAddress`). The compound wrapper is only for selection.
|
|
85
90
|
|
|
86
91
|
---
|
|
87
92
|
|
|
88
93
|
## GraphQL Workflow
|
|
89
94
|
|
|
95
|
+
| Step | Action | Key output |
|
|
96
|
+
|------|--------|------------|
|
|
97
|
+
| 1 | Acquire schema | `schema.graphql` exists |
|
|
98
|
+
| 2 | Look up entities | Field names, types, relationships confirmed |
|
|
99
|
+
| 3 | Generate query | `.graphql` file or inline `gql` tag |
|
|
100
|
+
| 4 | Generate types | `graphql-operations-types.ts` |
|
|
101
|
+
| 5 | Validate | Lint + codegen pass |
|
|
102
|
+
|
|
90
103
|
### Step 1: Acquire Schema
|
|
91
104
|
|
|
92
|
-
The `schema.graphql` file (265K+ lines) is the source of truth. **Never open or parse it directly
|
|
105
|
+
The `schema.graphql` file (265K+ lines) is the source of truth. **Never open or parse it directly** — no cat, less, head, tail, editors, or programmatic parsers.
|
|
93
106
|
|
|
94
|
-
|
|
95
|
-
2. If missing, run from the **UI bundle dir**: `npm run graphql:schema`
|
|
96
|
-
3. Custom objects appear only after metadata is deployed
|
|
107
|
+
Verify preconditions 1–3 (see [Preconditions](#preconditions--verify-before-starting)), then proceed to Step 2.
|
|
97
108
|
|
|
98
109
|
### Step 2: Look Up Entity Schema
|
|
99
110
|
|
|
100
|
-
Map user intent to PascalCase names ("accounts" → `Account`), then **run the search script from the project root**:
|
|
111
|
+
Map user intent to PascalCase names ("accounts" → `Account`), then **run the search script from the `sfdx-project` folder (project root)**:
|
|
101
112
|
|
|
102
113
|
```bash
|
|
103
|
-
# Look up all relevant schema info for one or more entities
|
|
104
114
|
bash scripts/graphql-search.sh Account
|
|
105
|
-
|
|
106
|
-
# Multiple entities at once
|
|
115
|
+
# Multiple entities:
|
|
107
116
|
bash scripts/graphql-search.sh Account Contact Opportunity
|
|
108
117
|
```
|
|
109
118
|
|
|
110
|
-
The script outputs
|
|
119
|
+
The script outputs seven sections per entity:
|
|
111
120
|
1. **Type definition** — all queryable fields and relationships
|
|
112
121
|
2. **Filter options** — available fields for `where:` conditions
|
|
113
122
|
3. **Sort options** — available fields for `orderBy:`
|
|
114
|
-
4. **Create
|
|
115
|
-
5. **
|
|
123
|
+
4. **Create mutation wrapper** — `<Entity>CreateInput`
|
|
124
|
+
5. **Create mutation fields** — `<Entity>CreateRepresentation` (fields accepted by create mutations)
|
|
125
|
+
6. **Update mutation wrapper** — `<Entity>UpdateInput`
|
|
126
|
+
7. **Update mutation fields** — `<Entity>UpdateRepresentation` (fields accepted by update mutations)
|
|
116
127
|
|
|
117
|
-
|
|
128
|
+
**Maximum 2 script runs.** If the entity still can't be found, ask the user — the object may not be deployed.
|
|
129
|
+
|
|
130
|
+
#### Entity Identification
|
|
131
|
+
|
|
132
|
+
If a candidate does not match:
|
|
133
|
+
- Try `__c` suffix for custom objects, `__e` for platform events
|
|
134
|
+
- Try `_Record` suffix — objects added in v60+ may use `<EntityName>_Record`
|
|
135
|
+
- If still unresolved, **ask the user** — do not guess
|
|
136
|
+
|
|
137
|
+
#### Iterative Introspection (max 3 cycles)
|
|
138
|
+
|
|
139
|
+
1. **Introspect** — Run the script for each unresolved entity
|
|
140
|
+
2. **Fields** — Extract requested field names and types from the type definition
|
|
141
|
+
3. **References** — Identify reference fields. If polymorphic (multiple types), use inline fragments. Add newly discovered entity types to the working list.
|
|
142
|
+
4. **Child relationships** — Identify Connection types. Add child entity types to the working list.
|
|
143
|
+
5. **Repeat** if unresolved entities remain (max 3 cycles)
|
|
144
|
+
|
|
145
|
+
**Hard stops:** If no data returned for an entity, stop — it may not be deployed. If unknown entities remain after 3 cycles, ask the user. Do not generate queries with unconfirmed entities or fields.
|
|
118
146
|
|
|
119
147
|
### Step 3: Generate Query
|
|
120
148
|
|
|
121
|
-
|
|
149
|
+
Every field name **must** be verified from the script output in Step 2.
|
|
122
150
|
|
|
123
151
|
#### Read Query Template
|
|
124
152
|
|
|
125
153
|
```graphql
|
|
126
|
-
query
|
|
154
|
+
query QueryName($after: String) {
|
|
127
155
|
uiapi {
|
|
128
156
|
query {
|
|
129
|
-
|
|
157
|
+
EntityName(
|
|
158
|
+
first: 10
|
|
159
|
+
after: $after
|
|
160
|
+
where: { ... }
|
|
161
|
+
orderBy: { ... }
|
|
162
|
+
) {
|
|
130
163
|
edges {
|
|
131
164
|
node {
|
|
132
165
|
Id
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
# Parent relationship
|
|
166
|
+
FieldName @optional { value }
|
|
167
|
+
# Parent relationship (non-polymorphic)
|
|
136
168
|
Owner @optional { Name { value } }
|
|
137
|
-
#
|
|
138
|
-
|
|
169
|
+
# Parent relationship (polymorphic — use fragments)
|
|
170
|
+
What @optional {
|
|
171
|
+
...WhatAccount
|
|
172
|
+
...WhatOpportunity
|
|
173
|
+
}
|
|
174
|
+
# Child relationship — max 1 level, no grandchildren
|
|
175
|
+
Contacts @optional(first: 10) {
|
|
139
176
|
edges { node { Name @optional { value } } }
|
|
140
177
|
}
|
|
141
178
|
}
|
|
142
179
|
}
|
|
180
|
+
pageInfo { hasNextPage endCursor }
|
|
143
181
|
}
|
|
144
182
|
}
|
|
145
183
|
}
|
|
146
184
|
}
|
|
185
|
+
|
|
186
|
+
fragment WhatAccount on Account {
|
|
187
|
+
Id
|
|
188
|
+
Name @optional { value }
|
|
189
|
+
}
|
|
190
|
+
fragment WhatOpportunity on Opportunity {
|
|
191
|
+
Id
|
|
192
|
+
Name @optional { value }
|
|
193
|
+
}
|
|
147
194
|
```
|
|
148
195
|
|
|
149
|
-
**
|
|
196
|
+
**Consuming code must defend against missing fields:**
|
|
150
197
|
|
|
151
198
|
```typescript
|
|
152
199
|
const name = node.Name?.value ?? "";
|
|
200
|
+
const relatedName = node.Owner?.Name?.value ?? "N/A";
|
|
153
201
|
```
|
|
154
202
|
|
|
203
|
+
#### Filtering
|
|
204
|
+
|
|
205
|
+
```graphql
|
|
206
|
+
# Implicit AND
|
|
207
|
+
Account(where: { Industry: { eq: "Technology" }, AnnualRevenue: { gt: 1000000 } })
|
|
208
|
+
|
|
209
|
+
# Explicit OR
|
|
210
|
+
Account(where: { OR: [{ Industry: { eq: "Technology" } }, { Industry: { eq: "Finance" } }] })
|
|
211
|
+
|
|
212
|
+
# NOT
|
|
213
|
+
Account(where: { NOT: { Industry: { eq: "Technology" } } })
|
|
214
|
+
|
|
215
|
+
# Date literal
|
|
216
|
+
Opportunity(where: { CloseDate: { eq: { value: "2024-12-31" } } })
|
|
217
|
+
|
|
218
|
+
# Relative date
|
|
219
|
+
Opportunity(where: { CloseDate: { gte: { literal: TODAY } } })
|
|
220
|
+
|
|
221
|
+
# Relationship filter (nested objects, NOT dot notation)
|
|
222
|
+
Contact(where: { Account: { Name: { like: "Acme%" } } })
|
|
223
|
+
|
|
224
|
+
# Polymorphic relationship filter
|
|
225
|
+
Account(where: { Owner: { User: { Username: { like: "admin%" } } } })
|
|
226
|
+
```
|
|
227
|
+
|
|
228
|
+
String equality (`eq`) is case-insensitive. Both 15-char and 18-char record IDs are accepted.
|
|
229
|
+
|
|
230
|
+
#### Ordering
|
|
231
|
+
|
|
232
|
+
```graphql
|
|
233
|
+
Account(
|
|
234
|
+
first: 10,
|
|
235
|
+
orderBy: { Name: { order: ASC }, CreatedDate: { order: DESC } }
|
|
236
|
+
) { ... }
|
|
237
|
+
```
|
|
238
|
+
|
|
239
|
+
Unsupported for ordering: multi-select picklist, rich text, long text area, encrypted fields. Add `Id` as tie-breaker for deterministic ordering.
|
|
240
|
+
|
|
241
|
+
#### UpperBound Pagination (v59+)
|
|
242
|
+
|
|
243
|
+
For >200 records per page or >4,000 total records, use `upperBound`. `first` must be 200–2000 when set.
|
|
244
|
+
|
|
245
|
+
```graphql
|
|
246
|
+
Account(first: 2000, after: $cursor, upperBound: 10000) {
|
|
247
|
+
edges { node { Id Name @optional { value } } }
|
|
248
|
+
pageInfo { hasNextPage endCursor }
|
|
249
|
+
}
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
#### Semi-Join and Anti-Join
|
|
253
|
+
|
|
254
|
+
Filter a parent entity by conditions on child entities using `inq` (semi-join) or `ninq` (anti-join) on the parent's `Id`. If the only condition is child existence, use `Id: { ne: null }`.
|
|
255
|
+
|
|
256
|
+
```graphql
|
|
257
|
+
query SemiJoinExample {
|
|
258
|
+
uiapi {
|
|
259
|
+
query {
|
|
260
|
+
Account(where: {
|
|
261
|
+
Id: {
|
|
262
|
+
inq: {
|
|
263
|
+
Contact: { LastName: { like: "Smith%" } }
|
|
264
|
+
ApiName: "AccountId"
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}, first: 10) {
|
|
268
|
+
edges { node { Id Name @optional { value } } }
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
Replace `inq` with `ninq` for anti-join. Restrictions: no `OR` in subquery, no `orderBy` in subquery, no nesting joins within each other.
|
|
276
|
+
|
|
277
|
+
#### Current User
|
|
278
|
+
|
|
279
|
+
Use `uiapi.currentUser` (no arguments) instead of the standard query pattern:
|
|
280
|
+
|
|
281
|
+
```graphql
|
|
282
|
+
query CurrentUser {
|
|
283
|
+
uiapi { currentUser { Id Name { value } } }
|
|
284
|
+
}
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
#### Field Value Wrappers
|
|
288
|
+
|
|
289
|
+
Schema fields use typed wrappers — access via `.value`:
|
|
290
|
+
|
|
291
|
+
| Wrapper Type | Underlying | Wrapper Type | Underlying |
|
|
292
|
+
|---|---|---|---|
|
|
293
|
+
| `StringValue` | `String` | `BooleanValue` | `Boolean` |
|
|
294
|
+
| `IntValue` | `Int` | `DoubleValue` | `Double` |
|
|
295
|
+
| `CurrencyValue` | `Currency` | `PercentValue` | `Percent` |
|
|
296
|
+
| `DateTimeValue` | `DateTime` | `DateValue` | `Date` |
|
|
297
|
+
| `PicklistValue` | `Picklist` | `LongValue` | `Long` |
|
|
298
|
+
| `IDValue` | `ID` | `TextAreaValue` | `TextArea` |
|
|
299
|
+
| `EmailValue` | `Email` | `PhoneNumberValue` | `PhoneNumber` |
|
|
300
|
+
| `UrlValue` | `Url` | | |
|
|
301
|
+
|
|
302
|
+
All wrappers also expose `displayValue: String` (server-rendered via `toLabel()`/`format()`) — use for UI display instead of formatting client-side.
|
|
303
|
+
|
|
155
304
|
#### Mutation Template
|
|
156
305
|
|
|
306
|
+
Mutations are GA in API v66+. Three operations: **Create**, **Update**, **Delete**.
|
|
307
|
+
|
|
157
308
|
```graphql
|
|
309
|
+
# Create
|
|
158
310
|
mutation CreateAccount($input: AccountCreateInput!) {
|
|
159
311
|
uiapi(input: { allOrNone: true }) {
|
|
160
312
|
AccountCreate(input: $input) {
|
|
@@ -162,20 +314,71 @@ mutation CreateAccount($input: AccountCreateInput!) {
|
|
|
162
314
|
}
|
|
163
315
|
}
|
|
164
316
|
}
|
|
317
|
+
|
|
318
|
+
# Update — must include Id
|
|
319
|
+
mutation UpdateAccount {
|
|
320
|
+
uiapi(input: { allOrNone: true }) {
|
|
321
|
+
AccountUpdate(input: { Id: "001xx000003GYkZAAW", Account: { Name: "New Name" } }) {
|
|
322
|
+
Record { Id Name { value } }
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
165
326
|
```
|
|
166
327
|
|
|
167
|
-
**
|
|
168
|
-
- Create
|
|
169
|
-
- Update
|
|
170
|
-
- Delete
|
|
328
|
+
**Input constraints:**
|
|
329
|
+
- **Create**: Required fields (unless `defaultedOnCreate`), only `createable` fields, no child relationships. Reference fields set by `ApiName` (e.g., `AccountId`).
|
|
330
|
+
- **Update**: Must include `Id`, only `updateable` fields, no child relationships.
|
|
331
|
+
- **Delete**: `Id` only.
|
|
332
|
+
- **`IdOrRef` type**: The `Id` field in Update and Delete inputs uses the `IdOrRef` type, which accepts either a literal record ID (e.g., `"001xx..."`) or a mutation chaining reference (`"@{Alias}"`). Reference fields in Create inputs (e.g., `AccountId`) also accept `@{Alias}` for chaining.
|
|
333
|
+
- **Raw values**: No commas, currency symbols, or locale formatting (e.g., `80000` not `"$80,000"`).
|
|
171
334
|
|
|
172
|
-
|
|
335
|
+
**Output constraints:**
|
|
336
|
+
- Create/Update: Exclude child relationships, exclude navigated reference fields (only `ApiName` member allowed). Output field is always named `Record`.
|
|
337
|
+
- Delete: `Id` only.
|
|
173
338
|
|
|
174
|
-
|
|
339
|
+
**`allOrNone` semantics:**
|
|
340
|
+
- `true` (default) — All operations succeed or all roll back.
|
|
341
|
+
- `false` — Independent operations succeed individually, but dependent operations (using `@{alias}`) still roll back together.
|
|
175
342
|
|
|
176
|
-
|
|
343
|
+
#### Mutation Chaining
|
|
344
|
+
|
|
345
|
+
Chain related mutations using `@{alias}` references to `Id` from earlier mutations. Required for parent-child creation (nested child creates are not supported).
|
|
346
|
+
|
|
347
|
+
```graphql
|
|
348
|
+
mutation CreateAccountAndContact {
|
|
349
|
+
uiapi(input: { allOrNone: true }) {
|
|
350
|
+
AccountCreate(input: { Account: { Name: "Acme" } }) {
|
|
351
|
+
Record { Id }
|
|
352
|
+
}
|
|
353
|
+
ContactCreate(input: { Contact: { LastName: "Smith", AccountId: "@{AccountCreate}" } }) {
|
|
354
|
+
Record { Id }
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
Rules: `A` must come before `B` in the query. `@{A}` is always the `Id` from mutation `A`. Only `Create` or `Delete` can be chained from (not `Update`).
|
|
361
|
+
|
|
362
|
+
#### Delete Mutation
|
|
363
|
+
|
|
364
|
+
Delete uses generic `RecordDeleteInput` (not entity-specific). Output is `Id` only — no `Record` field.
|
|
365
|
+
|
|
366
|
+
```graphql
|
|
367
|
+
mutation DeleteAccount($id: ID!) {
|
|
368
|
+
uiapi(input: { allOrNone: true }) {
|
|
369
|
+
AccountDelete(input: { Id: $id }) {
|
|
370
|
+
Id
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
```
|
|
375
|
+
|
|
376
|
+
#### Object Metadata & Picklist Values
|
|
377
|
+
|
|
378
|
+
Use `uiapi { objectInfos(...) }` to fetch field metadata or picklist values. Pass **either** `apiNames` or `objectInfoInputs` — never both.
|
|
177
379
|
|
|
178
380
|
```typescript
|
|
381
|
+
// Object metadata
|
|
179
382
|
const GET_OBJECT_INFO = gql`
|
|
180
383
|
query GetObjectInfo($apiNames: [String!]!) {
|
|
181
384
|
uiapi {
|
|
@@ -183,26 +386,13 @@ const GET_OBJECT_INFO = gql`
|
|
|
183
386
|
ApiName
|
|
184
387
|
label
|
|
185
388
|
labelPlural
|
|
186
|
-
fields {
|
|
187
|
-
ApiName
|
|
188
|
-
label
|
|
189
|
-
dataType
|
|
190
|
-
updateable
|
|
191
|
-
createable
|
|
192
|
-
}
|
|
389
|
+
fields { ApiName label dataType updateable createable }
|
|
193
390
|
}
|
|
194
391
|
}
|
|
195
392
|
}
|
|
196
393
|
`;
|
|
197
394
|
|
|
198
|
-
|
|
199
|
-
const response = await sdk.graphql?.(GET_OBJECT_INFO, { apiNames: ["Account"] });
|
|
200
|
-
const objectInfos = response?.data?.uiapi?.objectInfos ?? [];
|
|
201
|
-
```
|
|
202
|
-
|
|
203
|
-
**Picklist values** (use `objectInfoInputs` + `... on PicklistField` inline fragment):
|
|
204
|
-
|
|
205
|
-
```typescript
|
|
395
|
+
// Picklist values (use objectInfoInputs + inline fragment)
|
|
206
396
|
const GET_PICKLIST_VALUES = gql`
|
|
207
397
|
query GetPicklistValues($objectInfoInputs: [ObjectInfoInput!]!) {
|
|
208
398
|
uiapi {
|
|
@@ -213,10 +403,7 @@ const GET_PICKLIST_VALUES = gql`
|
|
|
213
403
|
... on PicklistField {
|
|
214
404
|
picklistValuesByRecordTypeIDs {
|
|
215
405
|
recordTypeID
|
|
216
|
-
picklistValues {
|
|
217
|
-
label
|
|
218
|
-
value
|
|
219
|
-
}
|
|
406
|
+
picklistValues { label value }
|
|
220
407
|
}
|
|
221
408
|
}
|
|
222
409
|
}
|
|
@@ -224,52 +411,95 @@ const GET_PICKLIST_VALUES = gql`
|
|
|
224
411
|
}
|
|
225
412
|
}
|
|
226
413
|
`;
|
|
414
|
+
```
|
|
227
415
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
416
|
+
### Step 4: Generate Types (codegen)
|
|
417
|
+
|
|
418
|
+
After writing the query (whether in a `.graphql` file or inline with `gql`), generate TypeScript types:
|
|
419
|
+
|
|
420
|
+
```bash
|
|
421
|
+
# Run from UI bundle dir
|
|
422
|
+
npm run graphql:codegen
|
|
232
423
|
```
|
|
233
424
|
|
|
234
|
-
|
|
425
|
+
Output: `src/api/graphql-operations-types.ts`
|
|
235
426
|
|
|
236
|
-
|
|
237
|
-
|
|
427
|
+
Generated type naming conventions:
|
|
428
|
+
- `<OperationName>Query` / `<OperationName>Mutation` — response types
|
|
429
|
+
- `<OperationName>QueryVariables` / `<OperationName>MutationVariables` — variable types
|
|
238
430
|
|
|
239
|
-
**
|
|
431
|
+
**Always import and use the generated types** when calling `sdk.graphql`:
|
|
240
432
|
|
|
241
|
-
```
|
|
242
|
-
|
|
243
|
-
|
|
433
|
+
```typescript
|
|
434
|
+
import type { GetAccountsQuery, GetAccountsQueryVariables } from "../graphql-operations-types";
|
|
435
|
+
|
|
436
|
+
const response = await sdk.graphql?.<GetAccountsQuery, GetAccountsQueryVariables>(GET_ACCOUNTS, variables);
|
|
437
|
+
```
|
|
438
|
+
|
|
439
|
+
Use `NodeOfConnection<T>` to extract the node type from a Connection for cleaner typing:
|
|
440
|
+
|
|
441
|
+
```typescript
|
|
442
|
+
import { type NodeOfConnection } from "@salesforce/sdk-data";
|
|
443
|
+
|
|
444
|
+
type AccountNode = NodeOfConnection<GetAccountsQuery["uiapi"]["query"]["Account"]>;
|
|
244
445
|
```
|
|
245
446
|
|
|
246
|
-
|
|
447
|
+
### Step 5: Validate & Test
|
|
448
|
+
|
|
449
|
+
1. **Lint**: `npx eslint <file>` from UI bundle dir
|
|
450
|
+
2. **codegen**: `npm run graphql:codegen` from UI bundle dir
|
|
451
|
+
|
|
452
|
+
#### Common Error patterns
|
|
453
|
+
|
|
454
|
+
| Error Contains | Resolution |
|
|
455
|
+
|----------------|------------|
|
|
456
|
+
| `Cannot query field` / `ValidationError` | Field name wrong — re-run `graphql-search.sh <Entity>` |
|
|
457
|
+
| `Unknown type` | Type name wrong — verify PascalCase entity name via script |
|
|
458
|
+
| `Unknown argument` | Argument wrong — check Filter/OrderBy sections in script output |
|
|
459
|
+
| `invalid syntax` / `InvalidSyntax` | Fix syntax per error message |
|
|
460
|
+
| `VariableTypeMismatch` / `UnknownType` | Correct argument type from schema |
|
|
461
|
+
| `invalid cross reference id` | Entity deleted — ask for valid Id |
|
|
462
|
+
| `OperationNotSupported` | Check object availability and API version |
|
|
463
|
+
| `is not currently available in mutation results` | Remove field from mutation output |
|
|
464
|
+
| `Cannot invoke JsonElement.isJsonObject()` | Use API version 64+ for update mutation `Record` selection |
|
|
465
|
+
|
|
466
|
+
**On PARTIAL** If a mutation returns both data and errors (partial success): Report inaccessible fields, explain they cannot be in mutation output, offer to remove them. **Wait for user consent** before changing.
|
|
247
467
|
|
|
248
468
|
---
|
|
249
469
|
|
|
250
470
|
## UI Bundle Integration (React)
|
|
251
471
|
|
|
252
|
-
Two integration patterns
|
|
472
|
+
Two integration patterns:
|
|
473
|
+
|
|
474
|
+
### Pattern 1 — External `.graphql` file (complex queries)
|
|
253
475
|
|
|
254
|
-
|
|
255
|
-
|
|
476
|
+
**One operation per `.graphql` file.** Each file contains exactly one `query` or `mutation` (plus its fragments). Do not combine multiple operations in a single file.
|
|
477
|
+
|
|
478
|
+
```typescript
|
|
479
|
+
import { createDataSDK, type NodeOfConnection } from "@salesforce/sdk-data";
|
|
480
|
+
import MY_QUERY from "./query/myQuery.graphql?raw"; // ?raw suffix required
|
|
481
|
+
import type { GetMyDataQuery, GetMyDataQueryVariables } from "../graphql-operations-types";
|
|
482
|
+
|
|
483
|
+
const sdk = await createDataSDK();
|
|
484
|
+
const response = await sdk.graphql?.<GetMyDataQuery, GetMyDataQueryVariables>(MY_QUERY, variables);
|
|
485
|
+
```
|
|
486
|
+
|
|
487
|
+
After creating/changing `.graphql` files, run `npm run graphql:codegen` to generate types into `src/api/graphql-operations-types.ts`.
|
|
488
|
+
|
|
489
|
+
### Pattern 2 — Inline `gql` tag (simple queries)
|
|
490
|
+
|
|
491
|
+
**Must use `gql`** — plain template strings bypass ESLint schema validation.
|
|
256
492
|
|
|
257
493
|
```typescript
|
|
258
494
|
import { createDataSDK, gql } from "@salesforce/sdk-data";
|
|
495
|
+
import type { GetAccountsQuery } from "../graphql-operations-types";
|
|
259
496
|
|
|
260
497
|
const GET_ACCOUNTS = gql`
|
|
261
498
|
query GetAccounts {
|
|
262
499
|
uiapi {
|
|
263
500
|
query {
|
|
264
501
|
Account(first: 10) {
|
|
265
|
-
edges {
|
|
266
|
-
node {
|
|
267
|
-
Id
|
|
268
|
-
Name @optional {
|
|
269
|
-
value
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
}
|
|
502
|
+
edges { node { Id Name @optional { value } } }
|
|
273
503
|
}
|
|
274
504
|
}
|
|
275
505
|
}
|
|
@@ -277,15 +507,30 @@ const GET_ACCOUNTS = gql`
|
|
|
277
507
|
`;
|
|
278
508
|
|
|
279
509
|
const sdk = await createDataSDK();
|
|
280
|
-
const response = await sdk.graphql
|
|
510
|
+
const response = await sdk.graphql?.<GetAccountsQuery>(GET_ACCOUNTS);
|
|
511
|
+
```
|
|
512
|
+
|
|
513
|
+
### Error Handling
|
|
514
|
+
|
|
515
|
+
```typescript
|
|
516
|
+
// Strict (default) — any errors = failure
|
|
281
517
|
if (response?.errors?.length) {
|
|
282
518
|
throw new Error(response.errors.map(e => e.message).join("; "));
|
|
283
519
|
}
|
|
520
|
+
|
|
521
|
+
// Tolerant — log errors, use available data
|
|
522
|
+
if (response?.errors?.length) {
|
|
523
|
+
console.warn("GraphQL partial errors:", response.errors);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
// Discriminated — fail only when no data returned
|
|
527
|
+
if (!response?.data && response?.errors?.length) {
|
|
528
|
+
throw new Error(response.errors.map(e => e.message).join("; "));
|
|
529
|
+
}
|
|
530
|
+
|
|
284
531
|
const accounts = response?.data?.uiapi?.query?.Account?.edges?.map(e => e.node) ?? [];
|
|
285
532
|
```
|
|
286
533
|
|
|
287
|
-
For detailed patterns (external .graphql files, codegen, error handling strategies, quality checklists), see [UI Bundle Integration](references/ui-bundle-integration.md).
|
|
288
|
-
|
|
289
534
|
---
|
|
290
535
|
|
|
291
536
|
## REST API Patterns
|
|
@@ -335,6 +580,7 @@ const response = await sdk.graphql?.(GET_CURRENT_USER);
|
|
|
335
580
|
<project-root>/ ← SFDX project root
|
|
336
581
|
├── schema.graphql ← grep target (lives here)
|
|
337
582
|
├── sfdx-project.json
|
|
583
|
+
├── scripts/graphql-search.sh ← schema lookup script
|
|
338
584
|
└── force-app/main/default/uiBundles/<app-name>/ ← UI bundle dir
|
|
339
585
|
├── package.json ← npm scripts
|
|
340
586
|
└── src/
|
|
@@ -343,9 +589,9 @@ const response = await sdk.graphql?.(GET_CURRENT_USER);
|
|
|
343
589
|
| Command | Run From | Why |
|
|
344
590
|
|---------|----------|-----|
|
|
345
591
|
| `npm run graphql:schema` | UI bundle dir | Script in UI bundle's package.json |
|
|
592
|
+
| `npm run graphql:codegen` | UI bundle dir | Generate GraphQL types |
|
|
346
593
|
| `npx eslint <file>` | UI bundle dir | Reads eslint.config.js |
|
|
347
594
|
| `bash scripts/graphql-search.sh <Entity>` | project root | Schema lookup |
|
|
348
|
-
| `sf api request rest` | project root | Needs sfdx-project.json |
|
|
349
595
|
|
|
350
596
|
---
|
|
351
597
|
|