snow-flow 10.0.1-dev.403 → 10.0.1-dev.405
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
|
@@ -165,24 +165,79 @@ async function createFlowViaScheduledJob(
|
|
|
165
165
|
"",
|
|
166
166
|
// ── Trigger, action, variable creation (runs for any tier) ──
|
|
167
167
|
" if (flowSysId) {",
|
|
168
|
-
// Trigger
|
|
168
|
+
// Trigger — search broadly (no type filter, internal_name varies per instance)
|
|
169
169
|
" if (!isSubflow && trigType !== 'manual') {",
|
|
170
170
|
" try {",
|
|
171
|
-
" var
|
|
172
|
-
"
|
|
173
|
-
"
|
|
174
|
-
"
|
|
175
|
-
"
|
|
176
|
-
"
|
|
177
|
-
"
|
|
178
|
-
"
|
|
179
|
-
"
|
|
180
|
-
|
|
181
|
-
"
|
|
182
|
-
"
|
|
183
|
-
"
|
|
184
|
-
"
|
|
185
|
-
"
|
|
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
|
+
" var trigDefId = null;",
|
|
179
|
+
" var trigSearchLog = [];",
|
|
180
|
+
// Search 1: CONTAINS on internal_name (NO type filter — 'type' column values vary)
|
|
181
|
+
" for (var ts = 0; ts < terms.length; ts++) {",
|
|
182
|
+
" var tg = new GlideRecord('sys_hub_action_type_definition');",
|
|
183
|
+
" tg.addQuery('internal_name', 'CONTAINS', terms[ts]);",
|
|
184
|
+
" tg.setLimit(5); tg.query();",
|
|
185
|
+
" while (tg.next()) {",
|
|
186
|
+
" var tgType = tg.getValue('type') + '';",
|
|
187
|
+
" var tgName = tg.getValue('internal_name') + '';",
|
|
188
|
+
" trigSearchLog.push(terms[ts] + ':' + tgName + '(type=' + tgType + ')');",
|
|
189
|
+
" if (!trigDefId) { trigDef = tg; trigDefId = tg.getUniqueValue(); }",
|
|
190
|
+
" }",
|
|
191
|
+
" if (trigDefId) break;",
|
|
192
|
+
" }",
|
|
193
|
+
// Search 2: by display name (no type filter)
|
|
194
|
+
" if (!trigDefId) {",
|
|
195
|
+
" var tg2 = new GlideRecord('sys_hub_action_type_definition');",
|
|
196
|
+
" tg2.addQuery('name', 'CONTAINS', trigType.replace('_', ' '));",
|
|
197
|
+
" tg2.setLimit(5); tg2.query();",
|
|
198
|
+
" while (tg2.next()) {",
|
|
199
|
+
" var tg2Type = tg2.getValue('type') + '';",
|
|
200
|
+
" var tg2Name = tg2.getValue('internal_name') + '';",
|
|
201
|
+
" trigSearchLog.push('name:' + tg2Name + '(type=' + tg2Type + ')');",
|
|
202
|
+
" if (!trigDefId) { trigDef = tg2; trigDefId = tg2.getUniqueValue(); }",
|
|
203
|
+
" }",
|
|
204
|
+
" }",
|
|
205
|
+
// Search 3: sys_hub_trigger_definition table (alternative on some instances)
|
|
206
|
+
" if (!trigDefId) {",
|
|
207
|
+
" try {",
|
|
208
|
+
" var tg3 = new GlideRecord('sys_hub_trigger_definition');",
|
|
209
|
+
" if (tg3.isValid()) {",
|
|
210
|
+
" tg3.addQuery('internal_name', 'CONTAINS', terms[0]);",
|
|
211
|
+
" tg3.setLimit(3); tg3.query();",
|
|
212
|
+
" while (tg3.next()) {",
|
|
213
|
+
" trigSearchLog.push('trig_def_table:' + tg3.getValue('internal_name'));",
|
|
214
|
+
" if (!trigDefId) { trigDef = tg3; trigDefId = tg3.getUniqueValue(); }",
|
|
215
|
+
" }",
|
|
216
|
+
" } else { trigSearchLog.push('trig_def_table:invalid'); }",
|
|
217
|
+
" } catch(e3) { trigSearchLog.push('trig_def_table:error'); }",
|
|
218
|
+
" }",
|
|
219
|
+
// Discovery: log what type values exist (helps debug trigger issues)
|
|
220
|
+
" if (!trigDefId) {",
|
|
221
|
+
" try {",
|
|
222
|
+
" var disc = new GlideAggregate('sys_hub_action_type_definition');",
|
|
223
|
+
" disc.addAggregate('COUNT', 'type');",
|
|
224
|
+
" disc.groupBy('type'); disc.query();",
|
|
225
|
+
" var typeValues = [];",
|
|
226
|
+
" while (disc.next()) { typeValues.push(disc.getValue('type') + ':' + disc.getAggregate('COUNT', 'type')); }",
|
|
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 };",
|
|
186
241
|
" }",
|
|
187
242
|
" } catch(te) { r.steps.trigger = { success: false, error: te.getMessage ? te.getMessage() : te + '' }; }",
|
|
188
243
|
" }",
|
|
@@ -224,40 +279,47 @@ async function createFlowViaScheduledJob(
|
|
|
224
279
|
" }",
|
|
225
280
|
" r.steps.variables = { success: true, created: varsCreated };",
|
|
226
281
|
"",
|
|
227
|
-
// ── Engine
|
|
228
|
-
|
|
282
|
+
// ── Engine: probe APIs + try compile (NOT publish — that StackOverflowed) ──
|
|
283
|
+
// FlowAPI.publish() caused StackOverflowError and corrupted the flow.
|
|
284
|
+
// FlowAPI.compile() should be safer — it registers the flow with the
|
|
285
|
+
// engine and sets latest_version without the publish side effects.
|
|
286
|
+
" r.steps.engine = { apis_found: [], mode: 'probe_and_compile' };",
|
|
229
287
|
" try {",
|
|
230
288
|
" if (typeof sn_fd !== 'undefined') {",
|
|
231
289
|
" r.steps.engine.sn_fd = 'available';",
|
|
232
|
-
"
|
|
233
|
-
"
|
|
234
|
-
"
|
|
235
|
-
"
|
|
236
|
-
"
|
|
237
|
-
"
|
|
238
|
-
"
|
|
239
|
-
"
|
|
290
|
+
" var probeNames = ['FlowDesigner', 'FlowCompiler', 'FlowAPI', 'FlowPublisher'];",
|
|
291
|
+
" for (var pn = 0; pn < probeNames.length; pn++) {",
|
|
292
|
+
" try {",
|
|
293
|
+
" if (sn_fd[probeNames[pn]]) {",
|
|
294
|
+
" r.steps.engine.apis_found.push(probeNames[pn]);",
|
|
295
|
+
" var methods = [];",
|
|
296
|
+
" var pObj = sn_fd[probeNames[pn]];",
|
|
297
|
+
" var checkMethods = ['createFlow','publishFlow','activateFlow','compileFlow','compile','publish','getFlow'];",
|
|
298
|
+
" for (var cm = 0; cm < checkMethods.length; cm++) {",
|
|
299
|
+
" if (typeof pObj[checkMethods[cm]] === 'function') methods.push(checkMethods[cm]);",
|
|
300
|
+
" }",
|
|
301
|
+
" if (methods.length > 0) r.steps.engine[probeNames[pn] + '_methods'] = methods;",
|
|
302
|
+
" }",
|
|
303
|
+
" } catch(e) {}",
|
|
240
304
|
" }",
|
|
241
|
-
|
|
242
|
-
"
|
|
243
|
-
|
|
244
|
-
"
|
|
245
|
-
"
|
|
246
|
-
"
|
|
247
|
-
" } catch(ce) { r.steps.engine.compile = ce.getMessage ? ce.getMessage() : ce + ''; }",
|
|
248
|
-
" }",
|
|
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 + ''); }",
|
|
249
311
|
" }",
|
|
250
|
-
|
|
251
|
-
"
|
|
252
|
-
"
|
|
253
|
-
"
|
|
254
|
-
" var apiObj = sn_fd[otherApis[oa]];",
|
|
255
|
-
" if (typeof apiObj.publish === 'function') {",
|
|
256
|
-
" try { apiObj.publish(flowSysId); r.steps.engine[otherApis[oa] + '_publish'] = 'success'; }",
|
|
257
|
-
" catch(e) { r.steps.engine[otherApis[oa] + '_publish'] = e + ''; }",
|
|
258
|
-
" }",
|
|
259
|
-
" }",
|
|
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 + ''); }",
|
|
260
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;",
|
|
261
323
|
" } else {",
|
|
262
324
|
" r.steps.engine.sn_fd = 'unavailable';",
|
|
263
325
|
" }",
|
|
@@ -1466,39 +1528,53 @@ export async function execute(args: any, context: ServiceNowContext): Promise<To
|
|
|
1466
1528
|
}
|
|
1467
1529
|
|
|
1468
1530
|
// Create trigger instance (non-manual flows only)
|
|
1531
|
+
// Search broadly — internal_name varies across ServiceNow instances
|
|
1469
1532
|
if (!isSubflow && triggerType !== 'manual') {
|
|
1470
1533
|
try {
|
|
1471
|
-
var
|
|
1472
|
-
'record_created': '
|
|
1473
|
-
'record_updated': '
|
|
1474
|
-
'scheduled': '
|
|
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']
|
|
1475
1538
|
};
|
|
1476
|
-
var
|
|
1539
|
+
var searchTerms = trigSearchTerms[triggerType] || [triggerType];
|
|
1540
|
+
var triggerDefId: string | null = null;
|
|
1477
1541
|
|
|
1478
|
-
|
|
1542
|
+
for (var tsi = 0; tsi < searchTerms.length && !triggerDefId; tsi++) {
|
|
1479
1543
|
var triggerDefResp = await client.get('/api/now/table/sys_hub_action_type_definition', {
|
|
1480
1544
|
params: {
|
|
1481
|
-
sysparm_query: '
|
|
1545
|
+
sysparm_query: 'internal_nameLIKE' + searchTerms[tsi] + '^type=trigger',
|
|
1482
1546
|
sysparm_fields: 'sys_id',
|
|
1483
1547
|
sysparm_limit: 1
|
|
1484
1548
|
}
|
|
1485
1549
|
});
|
|
1550
|
+
triggerDefId = triggerDefResp.data.result?.[0]?.sys_id || null;
|
|
1551
|
+
}
|
|
1486
1552
|
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1553
|
+
// Fallback: search by display name
|
|
1554
|
+
if (!triggerDefId) {
|
|
1555
|
+
var trigNameResp = await client.get('/api/now/table/sys_hub_action_type_definition', {
|
|
1556
|
+
params: {
|
|
1557
|
+
sysparm_query: 'nameLIKE' + triggerType.replace(/_/g, ' ') + '^type=trigger',
|
|
1558
|
+
sysparm_fields: 'sys_id',
|
|
1559
|
+
sysparm_limit: 1
|
|
1560
|
+
}
|
|
1561
|
+
});
|
|
1562
|
+
triggerDefId = trigNameResp.data.result?.[0]?.sys_id || null;
|
|
1563
|
+
}
|
|
1564
|
+
|
|
1565
|
+
if (triggerDefId) {
|
|
1566
|
+
var triggerData: any = {
|
|
1567
|
+
flow: flowSysId,
|
|
1568
|
+
action_type: triggerDefId,
|
|
1569
|
+
name: triggerType,
|
|
1570
|
+
order: 0,
|
|
1571
|
+
active: true
|
|
1572
|
+
};
|
|
1573
|
+
if (flowTable) triggerData.table = flowTable;
|
|
1574
|
+
if (triggerCondition) triggerData.condition = triggerCondition;
|
|
1575
|
+
|
|
1576
|
+
await client.post('/api/now/table/sys_hub_trigger_instance', triggerData);
|
|
1577
|
+
triggerCreated = true;
|
|
1502
1578
|
}
|
|
1503
1579
|
} catch (triggerError) {
|
|
1504
1580
|
// Best-effort
|