import { CallExpressionShape, Plugin } from '@servicenow/sdk-build-core' import { NowIdShape } from './now-id-plugin' import { ModuleFunctionShape } from './server-module-plugin' export const ScriptActionPlugin = Plugin.create({ name: 'ScriptActionPlugin', records: { sysevent_script_action: { toShape(record) { return { success: true, value: new CallExpressionShape({ source: record, callee: 'ScriptAction', args: [ record.transform(({ $ }) => ({ $id: $.val(NowIdShape.from(record)), name: $, description: $.def(''), eventName: $.from('event_name'), active: $.toBoolean().def(false), conditionScript: $.from('condition_script').def(''), script: $, order: $.toNumber().def(100), })), ], }), } }, }, }, shapes: [ { shape: CallExpressionShape, fileTypes: ['fluent'], async toRecord(callExpression, { factory }) { if (callExpression.getCallee() !== 'ScriptAction') { return { success: false } } const args = callExpression.getArgument(0).asObject() const record = await factory.createRecord({ source: callExpression, table: 'sysevent_script_action', explicitId: args.get('$id'), properties: args.transform(({ $ }) => ({ name: $, description: $.def(''), event_name: $.from('eventName'), active: $.def(false), condition_script: $.from('conditionScript').toCdata(), script: $.map( (v) => v.if(ModuleFunctionShape)?.toString((n) => `${n}({{PARAMS}})`) ?? v ).toCdata(), order: $.def(100), sys_name: $.from('name'), })), }) return { success: true, value: record } }, }, ], })