snow-flow 10.0.1-dev.405 → 10.0.1-dev.408
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
CHANGED
|
@@ -123,7 +123,9 @@ async function createFlowViaScheduledJob(
|
|
|
123
123
|
" }",
|
|
124
124
|
" } catch(t1e) { r.steps.tier1 = { success: false, error: t1e.getMessage ? t1e.getMessage() : t1e + '' }; }",
|
|
125
125
|
"",
|
|
126
|
-
// ── TIER 2: GlideRecord
|
|
126
|
+
// ── TIER 2: GlideRecord — minimal clean records ──
|
|
127
|
+
// Do NOT set flow_definition/latest_snapshot on flow record (computed/managed).
|
|
128
|
+
// Version: INSERT as draft → UPDATE is_current=true (triggers BRs that set latest_version).
|
|
127
129
|
" if (!flowSysId) {",
|
|
128
130
|
" try {",
|
|
129
131
|
" var f = new GlideRecord('sys_hub_flow');",
|
|
@@ -133,29 +135,30 @@ async function createFlowViaScheduledJob(
|
|
|
133
135
|
" f.setValue('run_as', runAs); f.setValue('active', false);",
|
|
134
136
|
" f.setValue('status', 'draft'); f.setValue('validated', true);",
|
|
135
137
|
" f.setValue('type', isSubflow ? 'subflow' : 'flow');",
|
|
136
|
-
" if (flowDefStr) { f.setValue('flow_definition', flowDefStr); f.setValue('latest_snapshot', flowDefStr); }",
|
|
137
138
|
" flowSysId = f.insert();",
|
|
138
139
|
" r.steps.flow_insert = { success: !!flowSysId, sys_id: flowSysId + '' };",
|
|
139
140
|
" if (flowSysId) {",
|
|
141
|
+
// Step 1: INSERT version as minimal draft
|
|
140
142
|
" var v = new GlideRecord('sys_hub_flow_version');",
|
|
141
143
|
" v.initialize();",
|
|
142
144
|
" v.setValue('flow', flowSysId); v.setValue('name', '1.0');",
|
|
143
145
|
" v.setValue('version', '1.0'); v.setValue('state', 'draft');",
|
|
144
|
-
" v.setValue('active',
|
|
145
|
-
" v.setValue('is_current',
|
|
146
|
-
" if (flowDefStr) v.setValue('flow_definition', flowDefStr);",
|
|
146
|
+
" v.setValue('active', false); v.setValue('compile_state', 'draft');",
|
|
147
|
+
" v.setValue('is_current', false);",
|
|
147
148
|
" verSysId = v.insert();",
|
|
148
149
|
" r.steps.version_insert = { success: !!verSysId, sys_id: verSysId + '' };",
|
|
150
|
+
// Step 2: UPDATE version → is_current=true, active=true (triggers BRs)
|
|
149
151
|
" if (verSysId) {",
|
|
150
|
-
" var
|
|
151
|
-
" if (
|
|
152
|
-
"
|
|
153
|
-
"
|
|
154
|
-
"
|
|
155
|
-
"
|
|
156
|
-
|
|
157
|
-
"
|
|
158
|
-
"
|
|
152
|
+
" var vu = new GlideRecord('sys_hub_flow_version');",
|
|
153
|
+
" if (vu.get(verSysId)) {",
|
|
154
|
+
" vu.setValue('is_current', true); vu.setValue('active', true);",
|
|
155
|
+
" vu.update();",
|
|
156
|
+
" r.steps.version_update = { success: true };",
|
|
157
|
+
" }",
|
|
158
|
+
// Check if BR set latest_version
|
|
159
|
+
" var lvCheck = new GlideRecord('sys_hub_flow');",
|
|
160
|
+
" if (lvCheck.get(flowSysId)) {",
|
|
161
|
+
" r.steps.latest_version_after_update = lvCheck.getValue('latest_version') + '';",
|
|
159
162
|
" }",
|
|
160
163
|
" }",
|
|
161
164
|
" r.tier_used = 'gliderecord_scheduled'; r.success = true;",
|
|
@@ -165,80 +168,68 @@ async function createFlowViaScheduledJob(
|
|
|
165
168
|
"",
|
|
166
169
|
// ── Trigger, action, variable creation (runs for any tier) ──
|
|
167
170
|
" if (flowSysId) {",
|
|
168
|
-
// Trigger — search
|
|
171
|
+
// Trigger — precise search with prefix matching + fallback without action_type
|
|
169
172
|
" if (!isSubflow && trigType !== 'manual') {",
|
|
170
173
|
" try {",
|
|
171
|
-
" var trigSearchTerms = {",
|
|
172
|
-
" 'record_created': ['record_created', 'record_insert', 'created'],",
|
|
173
|
-
" 'record_updated': ['record_updated', 'record_update', 'updated'],",
|
|
174
|
-
" 'scheduled': ['scheduled', 'timer', 'schedule']",
|
|
175
|
-
" };",
|
|
176
|
-
" var terms = trigSearchTerms[trigType] || [trigType];",
|
|
177
|
-
" var trigDef = null;",
|
|
178
174
|
" var trigDefId = null;",
|
|
175
|
+
" var trigDefName = '';",
|
|
179
176
|
" var trigSearchLog = [];",
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
"
|
|
183
|
-
"
|
|
184
|
-
"
|
|
185
|
-
"
|
|
186
|
-
"
|
|
187
|
-
"
|
|
188
|
-
"
|
|
189
|
-
"
|
|
177
|
+
"",
|
|
178
|
+
// Search 1: exact prefix 'sn_fd.trigger.' (standard Flow Designer naming)
|
|
179
|
+
" var exactNames = {",
|
|
180
|
+
" 'record_created': ['sn_fd.trigger.record_created', 'global.sn_fd.trigger.record_created'],",
|
|
181
|
+
" 'record_updated': ['sn_fd.trigger.record_updated', 'global.sn_fd.trigger.record_updated'],",
|
|
182
|
+
" 'scheduled': ['sn_fd.trigger.scheduled', 'global.sn_fd.trigger.scheduled']",
|
|
183
|
+
" };",
|
|
184
|
+
" var exactCandidates = exactNames[trigType] || [];",
|
|
185
|
+
" for (var ec = 0; ec < exactCandidates.length; ec++) {",
|
|
186
|
+
" var tgE = new GlideRecord('sys_hub_action_type_definition');",
|
|
187
|
+
" tgE.addQuery('internal_name', exactCandidates[ec]);",
|
|
188
|
+
" tgE.setLimit(1); tgE.query();",
|
|
189
|
+
" if (tgE.next()) {",
|
|
190
|
+
" trigDefId = tgE.getUniqueValue(); trigDefName = tgE.getValue('name');",
|
|
191
|
+
" trigSearchLog.push('exact:' + exactCandidates[ec] + ':found');",
|
|
192
|
+
" break;",
|
|
190
193
|
" }",
|
|
191
|
-
"
|
|
194
|
+
" trigSearchLog.push('exact:' + exactCandidates[ec] + ':not_found');",
|
|
192
195
|
" }",
|
|
193
|
-
|
|
196
|
+
"",
|
|
197
|
+
// Search 2: prefix STARTSWITH 'sn_fd.trigger' (catches variations)
|
|
194
198
|
" if (!trigDefId) {",
|
|
195
|
-
" var
|
|
196
|
-
"
|
|
197
|
-
"
|
|
198
|
-
"
|
|
199
|
-
"
|
|
200
|
-
"
|
|
201
|
-
"
|
|
202
|
-
"
|
|
199
|
+
" var tgP = new GlideRecord('sys_hub_action_type_definition');",
|
|
200
|
+
" tgP.addQuery('internal_name', 'STARTSWITH', 'sn_fd.trigger');",
|
|
201
|
+
" tgP.setLimit(10); tgP.query();",
|
|
202
|
+
" var prefixMatches = [];",
|
|
203
|
+
" while (tgP.next()) {",
|
|
204
|
+
" prefixMatches.push(tgP.getValue('internal_name'));",
|
|
205
|
+
" if (!trigDefId && (tgP.getValue('internal_name') + '').indexOf(trigType.replace('record_', '')) > -1) {",
|
|
206
|
+
" trigDefId = tgP.getUniqueValue(); trigDefName = tgP.getValue('name');",
|
|
207
|
+
" }",
|
|
203
208
|
" }",
|
|
209
|
+
" trigSearchLog.push('prefix_sn_fd.trigger:[' + prefixMatches.join(',') + ']');",
|
|
204
210
|
" }",
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
"
|
|
208
|
-
"
|
|
209
|
-
|
|
210
|
-
"
|
|
211
|
-
"
|
|
212
|
-
"
|
|
213
|
-
"
|
|
214
|
-
"
|
|
215
|
-
"
|
|
216
|
-
"
|
|
217
|
-
"
|
|
218
|
-
"
|
|
219
|
-
|
|
220
|
-
"
|
|
221
|
-
"
|
|
222
|
-
"
|
|
223
|
-
"
|
|
224
|
-
"
|
|
225
|
-
"
|
|
226
|
-
"
|
|
227
|
-
" trigSearchLog.push('type_values:[' + typeValues.join(',') + ']');",
|
|
228
|
-
" } catch(de) { trigSearchLog.push('discovery:error'); }",
|
|
229
|
-
" }",
|
|
230
|
-
" if (trigDefId) {",
|
|
231
|
-
" var trigInst = new GlideRecord('sys_hub_trigger_instance');",
|
|
232
|
-
" trigInst.initialize();",
|
|
233
|
-
" trigInst.setValue('flow', flowSysId); trigInst.setValue('action_type', trigDefId);",
|
|
234
|
-
" trigInst.setValue('name', trigType); trigInst.setValue('order', 0); trigInst.setValue('active', true);",
|
|
235
|
-
" if (trigTable) trigInst.setValue('table', trigTable);",
|
|
236
|
-
" if (trigCondition) trigInst.setValue('condition', trigCondition);",
|
|
237
|
-
" var trigId = trigInst.insert();",
|
|
238
|
-
" r.steps.trigger = { success: !!trigId, sys_id: trigId + '', def_name: trigDef.getValue('name'), def_internal: trigDef.getValue('internal_name'), search: trigSearchLog };",
|
|
239
|
-
" } else {",
|
|
240
|
-
" r.steps.trigger = { success: false, error: 'No trigger def found after broad search', search: trigSearchLog };",
|
|
241
|
-
" }",
|
|
211
|
+
"",
|
|
212
|
+
// Search 3 removed — sys_hub_trigger_definition returned wrong defs (Proactive Analytics, DevOps)
|
|
213
|
+
" if (!trigDefId) { trigSearchLog.push('no_exact_trigger_def_found'); }",
|
|
214
|
+
"",
|
|
215
|
+
// Create trigger instance — only set action_type if exact sn_fd.trigger.* match found
|
|
216
|
+
" var trigInst = new GlideRecord('sys_hub_trigger_instance');",
|
|
217
|
+
" trigInst.initialize();",
|
|
218
|
+
" trigInst.setValue('flow', flowSysId);",
|
|
219
|
+
" trigInst.setValue('name', trigType);",
|
|
220
|
+
" trigInst.setValue('order', 0);",
|
|
221
|
+
" trigInst.setValue('active', true);",
|
|
222
|
+
" if (trigDefId) trigInst.setValue('action_type', trigDefId);",
|
|
223
|
+
" if (trigTable) trigInst.setValue('table', trigTable);",
|
|
224
|
+
" if (trigCondition) trigInst.setValue('condition', trigCondition);",
|
|
225
|
+
" var trigId = trigInst.insert();",
|
|
226
|
+
" r.steps.trigger = {",
|
|
227
|
+
" success: !!trigId,",
|
|
228
|
+
" sys_id: trigId + '',",
|
|
229
|
+
" def_found: !!trigDefId,",
|
|
230
|
+
" def_name: trigDefName || 'none (created without action_type)',",
|
|
231
|
+
" search: trigSearchLog",
|
|
232
|
+
" };",
|
|
242
233
|
" } catch(te) { r.steps.trigger = { success: false, error: te.getMessage ? te.getMessage() : te + '' }; }",
|
|
243
234
|
" }",
|
|
244
235
|
// Actions
|
|
@@ -279,11 +270,11 @@ async function createFlowViaScheduledJob(
|
|
|
279
270
|
" }",
|
|
280
271
|
" r.steps.variables = { success: true, created: varsCreated };",
|
|
281
272
|
"",
|
|
282
|
-
// ── Engine:
|
|
283
|
-
// FlowAPI.publish()
|
|
284
|
-
// FlowAPI.compile()
|
|
285
|
-
//
|
|
286
|
-
" r.steps.engine = { apis_found: [], mode: '
|
|
273
|
+
// ── Engine: PROBE ONLY — do NOT call compile or publish ──
|
|
274
|
+
// FlowAPI.publish() → StackOverflowError (corrupts flow)
|
|
275
|
+
// FlowAPI.compile() → "success" but doesn't set latest_version, may corrupt
|
|
276
|
+
// Just probe what APIs exist for diagnostics.
|
|
277
|
+
" r.steps.engine = { apis_found: [], mode: 'probe_only' };",
|
|
287
278
|
" try {",
|
|
288
279
|
" if (typeof sn_fd !== 'undefined') {",
|
|
289
280
|
" r.steps.engine.sn_fd = 'available';",
|
|
@@ -302,24 +293,6 @@ async function createFlowViaScheduledJob(
|
|
|
302
293
|
" }",
|
|
303
294
|
" } catch(e) {}",
|
|
304
295
|
" }",
|
|
305
|
-
// Try compile (safe) — DO NOT try publish (StackOverflowError)
|
|
306
|
-
" var compiled = false;",
|
|
307
|
-
// Priority 1: FlowDesigner.compileFlow (most specific)
|
|
308
|
-
" if (!compiled && sn_fd.FlowDesigner && typeof sn_fd.FlowDesigner.compileFlow === 'function') {",
|
|
309
|
-
" try { sn_fd.FlowDesigner.compileFlow(flowSysId); r.steps.engine.compile = 'FlowDesigner.compileFlow:success'; compiled = true; }",
|
|
310
|
-
" catch(e) { r.steps.engine.compile = 'FlowDesigner.compileFlow:' + (e.getMessage ? e.getMessage() : e + ''); }",
|
|
311
|
-
" }",
|
|
312
|
-
// Priority 2: FlowCompiler.compile
|
|
313
|
-
" if (!compiled && sn_fd.FlowCompiler && typeof sn_fd.FlowCompiler.compile === 'function') {",
|
|
314
|
-
" try { sn_fd.FlowCompiler.compile(flowSysId); r.steps.engine.compile = 'FlowCompiler.compile:success'; compiled = true; }",
|
|
315
|
-
" catch(e) { r.steps.engine.compile = 'FlowCompiler.compile:' + (e.getMessage ? e.getMessage() : e + ''); }",
|
|
316
|
-
" }",
|
|
317
|
-
// Priority 3: FlowAPI.compile (available on this instance per diagnostics)
|
|
318
|
-
" if (!compiled && sn_fd.FlowAPI && typeof sn_fd.FlowAPI.compile === 'function') {",
|
|
319
|
-
" try { sn_fd.FlowAPI.compile(flowSysId); r.steps.engine.compile = 'FlowAPI.compile:success'; compiled = true; }",
|
|
320
|
-
" catch(e) { r.steps.engine.compile = 'FlowAPI.compile:' + (e.getMessage ? e.getMessage() : e + ''); }",
|
|
321
|
-
" }",
|
|
322
|
-
" r.steps.engine.compiled = compiled;",
|
|
323
296
|
" } else {",
|
|
324
297
|
" r.steps.engine.sn_fd = 'unavailable';",
|
|
325
298
|
" }",
|
|
@@ -1528,54 +1501,59 @@ export async function execute(args: any, context: ServiceNowContext): Promise<To
|
|
|
1528
1501
|
}
|
|
1529
1502
|
|
|
1530
1503
|
// Create trigger instance (non-manual flows only)
|
|
1531
|
-
//
|
|
1504
|
+
// Use precise prefix search, then fallback to creating without action_type
|
|
1532
1505
|
if (!isSubflow && triggerType !== 'manual') {
|
|
1533
1506
|
try {
|
|
1534
|
-
var trigSearchTerms: Record<string, string[]> = {
|
|
1535
|
-
'record_created': ['record_created', 'record_insert', 'created'],
|
|
1536
|
-
'record_updated': ['record_updated', 'record_update', 'updated'],
|
|
1537
|
-
'scheduled': ['scheduled', 'timer', 'schedule']
|
|
1538
|
-
};
|
|
1539
|
-
var searchTerms = trigSearchTerms[triggerType] || [triggerType];
|
|
1540
1507
|
var triggerDefId: string | null = null;
|
|
1541
1508
|
|
|
1542
|
-
|
|
1543
|
-
|
|
1509
|
+
// Search 1: exact sn_fd.trigger.* prefix
|
|
1510
|
+
var exactTrigNames: Record<string, string[]> = {
|
|
1511
|
+
'record_created': ['sn_fd.trigger.record_created', 'global.sn_fd.trigger.record_created'],
|
|
1512
|
+
'record_updated': ['sn_fd.trigger.record_updated', 'global.sn_fd.trigger.record_updated'],
|
|
1513
|
+
'scheduled': ['sn_fd.trigger.scheduled', 'global.sn_fd.trigger.scheduled']
|
|
1514
|
+
};
|
|
1515
|
+
var exactCands = exactTrigNames[triggerType] || [];
|
|
1516
|
+
for (var eci = 0; eci < exactCands.length && !triggerDefId; eci++) {
|
|
1517
|
+
var exactResp = await client.get('/api/now/table/sys_hub_action_type_definition', {
|
|
1544
1518
|
params: {
|
|
1545
|
-
sysparm_query: '
|
|
1519
|
+
sysparm_query: 'internal_name=' + exactCands[eci],
|
|
1546
1520
|
sysparm_fields: 'sys_id',
|
|
1547
1521
|
sysparm_limit: 1
|
|
1548
1522
|
}
|
|
1549
1523
|
});
|
|
1550
|
-
triggerDefId =
|
|
1524
|
+
triggerDefId = exactResp.data.result?.[0]?.sys_id || null;
|
|
1551
1525
|
}
|
|
1552
1526
|
|
|
1553
|
-
//
|
|
1527
|
+
// Search 2: STARTSWITH sn_fd.trigger
|
|
1554
1528
|
if (!triggerDefId) {
|
|
1555
|
-
var
|
|
1529
|
+
var prefixResp = await client.get('/api/now/table/sys_hub_action_type_definition', {
|
|
1556
1530
|
params: {
|
|
1557
|
-
sysparm_query: '
|
|
1558
|
-
sysparm_fields: 'sys_id',
|
|
1559
|
-
sysparm_limit:
|
|
1531
|
+
sysparm_query: 'internal_nameSTARTSWITHsn_fd.trigger',
|
|
1532
|
+
sysparm_fields: 'sys_id,internal_name',
|
|
1533
|
+
sysparm_limit: 10
|
|
1560
1534
|
}
|
|
1561
1535
|
});
|
|
1562
|
-
|
|
1536
|
+
var prefixResults = prefixResp.data.result || [];
|
|
1537
|
+
for (var pri = 0; pri < prefixResults.length && !triggerDefId; pri++) {
|
|
1538
|
+
if ((prefixResults[pri].internal_name || '').indexOf(triggerType.replace('record_', '')) > -1) {
|
|
1539
|
+
triggerDefId = prefixResults[pri].sys_id;
|
|
1540
|
+
}
|
|
1541
|
+
}
|
|
1563
1542
|
}
|
|
1564
1543
|
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
}
|
|
1544
|
+
// Create trigger instance (with or without action_type)
|
|
1545
|
+
var triggerData: any = {
|
|
1546
|
+
flow: flowSysId,
|
|
1547
|
+
name: triggerType,
|
|
1548
|
+
order: 0,
|
|
1549
|
+
active: true
|
|
1550
|
+
};
|
|
1551
|
+
if (triggerDefId) triggerData.action_type = triggerDefId;
|
|
1552
|
+
if (flowTable) triggerData.table = flowTable;
|
|
1553
|
+
if (triggerCondition) triggerData.condition = triggerCondition;
|
|
1554
|
+
|
|
1555
|
+
await client.post('/api/now/table/sys_hub_trigger_instance', triggerData);
|
|
1556
|
+
triggerCreated = true;
|
|
1579
1557
|
} catch (triggerError) {
|
|
1580
1558
|
// Best-effort
|
|
1581
1559
|
}
|