import { Plugin, CallExpressionShape, StringShape, ObjectShape, type RecordId, GUID } from '@servicenow/sdk-build-core' import { CallExpressionPlugin } from './call-expression-plugin' export const NowDeletePlugin = Plugin.create({ name: 'NowDeletePlugin', nodes: [ { node: 'CallExpression', async toShape(node, { transform, factory }) { const result = await transform.toShape(node, CallExpressionPlugin) if ( !result.success || !result.value.is(CallExpressionShape) || result.value.getCallee() !== 'Now.del' ) { return { success: false } } const callExpression = result.value const table = callExpression .getArgument(0) .asString('The first argument to Now.del() must be a table name') .getValue() const guidOrKeys = callExpression .getArgument(1) .as( [StringShape, ObjectShape], 'The second argument to Now.del() must be a sys_id GUID or a coalesce keys object' ) const [guid, keysShape] = guidOrKeys.isString() ? [guidOrKeys.asString().getValue(), undefined] // Now.del(table, guid) : [undefined, guidOrKeys.asObject()] // Now.del(table, keys) const properties: globalThis.Record = { sys_id: guid ?? GUID() } for (const [k, v] of keysShape?.entries() ?? []) { properties[k] = typeof v === 'string' ? v : (v.ifRecord()?.getId() ?? v.ifRecordId() ?? v.ifDefined()?.toString().getValue()) || 'NULL' } return { success: true, value: await factory.createRecord({ source: node, table, properties, action: 'DELETE', installCategory: 'author_elective_update', }), } }, }, ], })