@jacebenson/jsn 5.5.11-dev.241.1 → 5.5.11-dev.242.1

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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jacebenson/jsn",
3
- "version": "5.5.11-dev.241.1",
3
+ "version": "5.5.11-dev.242.1",
4
4
  "description": "A command-line interface for ServiceNow that follows the Unix philosophy: simple, composable, and scriptable.",
5
5
  "type": "module",
6
6
  "bin": {
@@ -12,6 +12,7 @@ import {
12
12
  conditionInputFor,
13
13
  readTableDescriptor,
14
14
  readTriggerInputDefinitions,
15
+ readTriggerOutputDefinitions,
15
16
  tableInputFor,
16
17
  flowDesignParameter,
17
18
  triggerFamily,
@@ -1937,10 +1938,19 @@ export async function createFlowFromManifest(sdk, instance, inputManifest, optio
1937
1938
  for (const match of String(value).matchAll(/\{\{([^}]+)\}\}/g)) pills.push({ name: match[1], uiUniqueIdentifier, inputName });
1938
1939
  };
1939
1940
  const orderedManifest = { ...manifest, actions: manifest.actions.map((a, index) => ({ ...a, order: a.order ?? index + 1, inputs: a.inputs || {} })) };
1941
+ // The trigger definition is needed before the actions are planned: a manifest
1942
+ // that feeds a trigger output into an action input has to resolve that pill
1943
+ // even though the trigger instance is not inserted until the patch below.
1944
+ const triggerDefinition = await resolveTriggerDefinition(sdk, manifest.trigger.type);
1945
+ const pendingTrigger = {
1946
+ name: triggerDefinition.name,
1947
+ trigger: true,
1948
+ outputs: await readTriggerOutputDefinitions(sdk, triggerDefinition.sysId),
1949
+ };
1940
1950
  await planManifestSteps({ manifest: orderedManifest, beforeDefinition: {}, existingActions: [], existingOrders: new Set(), actionUiMap: new Map(), logicUiMap: new Map(), pendingSteps, idFactory, sdk, instance, scope, flowId: '', actionInserts: actionInstances, actionUpdates: [], flowLogicInserts, actionTargets: new Map(), changedActionInputs: new Map(), changes: [], collectPills });
1941
1951
  if (manifest.errorHandling !== undefined) await planErrorHandling({ sdk, instance, scope, flowId: '', beforeDefinition: {}, existingActions: [], actionInserts: actionInstances, actionUpdates: [], flowLogicInserts, flowLogicUpdates: [], changes: [], idFactory, actionUiMap: new Map(), pendingSteps, changedActionInputs: new Map(), collectPills, errorHandlingInput: manifest.errorHandling });
1942
1952
  for (const pill of pills) {
1943
- const binding = resolvePillBinding({}, pill.name, '', [], pendingSteps);
1953
+ const binding = resolvePillBinding({}, pill.name, '', [], [...pendingSteps, pendingTrigger]);
1944
1954
  const previous = labelCacheInserts.find(e => e.name === pill.name);
1945
1955
  if (previous) {
1946
1956
  if (!previous.usedInstances.some(i => i.uiUniqueIdentifier === pill.uiUniqueIdentifier && i.inputName === pill.inputName)) previous.usedInstances.push({ uiUniqueIdentifier: pill.uiUniqueIdentifier, inputName: pill.inputName });
@@ -1971,8 +1981,7 @@ export async function createFlowFromManifest(sdk, instance, inputManifest, optio
1971
1981
  // comes back empty), so the trigger is inserted by the same snFlowDesigner
1972
1982
  // patch as the actions. There is nothing to read back from the parent apart
1973
1983
  // from the flow identity.
1974
- const definition = await resolveTriggerDefinition(sdk, manifest.trigger.type);
1975
- const trigger = await buildCreateTrigger(sdk, flowId, manifest, definition, options);
1984
+ const trigger = await buildCreateTrigger(sdk, flowId, manifest, triggerDefinition, options);
1976
1985
 
1977
1986
  for (const action of actionInstances) action.flowSysId = flowId;
1978
1987
  for (const logic of flowLogicInserts) logic.flowSysId = flowId;
@@ -2110,7 +2119,7 @@ export function resolvePillBinding(definition, pillName, flowId, cache, pending
2110
2119
  const candidates = [
2111
2120
  ...findArray(definition, 'triggerInstances').map(step => ({ ...step, trigger: true })),
2112
2121
  ...findExistingActions(definition).map(step => ({ ...step, trigger: false })),
2113
- ...pending.map(step => ({ ...step, trigger: false })),
2122
+ ...pending.map(step => ({ ...step, trigger: step.trigger === true })),
2114
2123
  ...(findArray(definition, 'flowLogicInstances') || []).filter(fl => String(fl.definitionId) === '35d60003e6022010a5e40cdd1254dd23' || String(fl.definitionId) === '35d60003e6022010a5e40cdd1254dd21').map(fl => ({
2115
2124
  name: String(fl.definitionId) === '35d60003e6022010a5e40cdd1254dd23' ? 'Error Handler' : 'Catch',
2116
2125
  order: stringValue(fl.order),
@@ -194,6 +194,71 @@ export async function readTriggerInputDefinitions(sdk, definitionId) {
194
194
  .filter(input => input.name);
195
195
  }
196
196
 
197
+ export const TRIGGER_OUTPUT_TABLE = 'sys_hub_trigger_output';
198
+ export const TRIGGER_OUTPUT_NAME_PREFIX = 'var__m_sys_hub_trigger_output_';
199
+
200
+ // sys_hub_trigger_output.internal_type carries a human label ("String (Full
201
+ // UTF-8)", "Table Name"), while the flow graph stores the machine type in
202
+ // triggerInstances[].outputs[].parameter.type. Read live: the Inbound Email
203
+ // trigger's Body Text output is internal_type "String (Full UTF-8)" and its
204
+ // instance output is "string_full_utf8".
205
+ const OUTPUT_TYPE_BY_INTERNAL = new Map([
206
+ ['string', 'string'],
207
+ ['string (full utf-8)', 'string_full_utf8'],
208
+ ['reference', 'reference'],
209
+ ['table name', 'table_name'],
210
+ ['document id', 'document_id'],
211
+ ['date/time', 'glide_date_time'],
212
+ ['date', 'glide_date'],
213
+ ['integer', 'integer'],
214
+ ['decimal', 'decimal'],
215
+ ['floating point number', 'float'],
216
+ ['true/false', 'boolean'],
217
+ ['choice', 'choice'],
218
+ ['glide list', 'glide_list'],
219
+ ['glide duration', 'glide_duration'],
220
+ ['html', 'html'],
221
+ ['url', 'url'],
222
+ ['email', 'email'],
223
+ ['array.object', 'array.object'],
224
+ ['json', 'json'],
225
+ ]);
226
+
227
+ export function flowOutputType(internalType) {
228
+ const key = String(text(internalType) || '').trim().toLowerCase();
229
+ return OUTPUT_TYPE_BY_INTERNAL.get(key) || key.replace(/[^a-z0-9]+/g, '_').replace(/^_+|_+$/g, '');
230
+ }
231
+
232
+ /**
233
+ * The outputs a trigger definition declares, straight from
234
+ * sys_hub_trigger_output. Flow Designer derives a trigger instance's data pills
235
+ * from these, so a manifest that references `{{<trigger>_1.<output>}}` needs
236
+ * them before the trigger instance exists.
237
+ */
238
+ export async function readTriggerOutputDefinitions(sdk, definitionId) {
239
+ if (!isSysId(definitionId)) throw new Error(`Trigger definition id must be an exact sys_id, got "${definitionId}"`);
240
+ const rows = await sdk.list(TRIGGER_OUTPUT_TABLE, new URLSearchParams({
241
+ sysparm_query: `name=${TRIGGER_OUTPUT_NAME_PREFIX}${definitionId}^ORDERBYorder`,
242
+ sysparm_limit: '200',
243
+ sysparm_display_value: 'all',
244
+ sysparm_fields: 'sys_id,element,label,column_label,internal_type,order,max_length,reference',
245
+ }));
246
+ return rows
247
+ .map(row => {
248
+ const name = text(row.element);
249
+ const parameter = {
250
+ id: text(row.sys_id),
251
+ name,
252
+ label: displayValue(row.label) || displayValue(row.column_label) || name,
253
+ type: flowOutputType(row.internal_type),
254
+ maxsize: numeric(row.max_length),
255
+ reference: text(row.reference) ? displayValue(row.reference) : '',
256
+ };
257
+ return { id: parameter.id, name, label: parameter.label, type: parameter.type, order: numeric(row.order), reference: parameter.reference, parameter };
258
+ })
259
+ .filter(output => output.name);
260
+ }
261
+
197
262
  function normalizeTriggerInput(row) {
198
263
  const parameter = {
199
264
  id: text(row.sys_id),