import { CallExpressionShape, Plugin } from '@servicenow/sdk-build-core' import { NowIdShape } from './now-id-plugin' /** * Creates a User Preference (sys_user_preference). * * @see https://docs.servicenow.com/csh?topicname=c_UserPreferences.html&version=latest * * @param config - an object containing the following properties: * * **$id** - unique id for the record, typically using `Now.ID["value"]` * * **name** - name of the feature or functionality * * **type** - the data type of entry accepted for the `value` * * **value** - current setting for this record * * **description**? - short description of the feature or functionality * * **system**? - whether this record indicates the system-wide default */ export const UserPreferencePlugin = Plugin.create({ name: 'UserPreferencePlugin', records: { sys_user_preference: { toShape(record) { return { success: true, value: new CallExpressionShape({ source: record, callee: 'UserPreference', args: [ record .transform(({ $, merge }) => ({ $id: $.val(NowIdShape.from(record)), system: $.from('system').toBoolean(), [merge]: $, })) .omit(['sys_scope', 'sys_update_name']), ], }), } }, }, }, shapes: [ { shape: CallExpressionShape, fileTypes: ['fluent'], async toRecord(callExpression, { factory }) { if (callExpression.getCallee() !== 'UserPreference') { return { success: false } } const pref = callExpression.getArgument(0).asObject() return { success: true, value: await factory.createRecord({ source: callExpression, table: 'sys_user_preference', explicitId: pref.get('$id'), properties: pref.pick(['name', 'type', 'value', 'description', 'system']), }), } }, }, ], })