import { SingleLineTextVariable, MultiLineTextVariable, WideSingleLineTextVariable, HtmlVariable, MaskedVariable, EmailVariable, UrlVariable, IpAddressVariable, RichTextLabelVariable, LabelVariable, // Choice Variables SelectBoxVariable, MultipleChoiceVariable, LookupSelectBoxVariable, LookupMultipleChoiceVariable, NumericScaleVariable, YesNoVariable, // Date/Time Variables DateVariable, DateTimeVariable, DurationVariable, // Reference Variables ReferenceVariable, RequestedForVariable, // Special Variables AttachmentVariable, ListCollectorVariable, UIPageVariable, // Layout Variables ContainerStartVariable, ContainerEndVariable, ContainerSplitVariable, BreakVariable, // Custom Variables CustomVariable, CustomWithLabelVariable, CheckboxVariable, } from '@servicenow/sdk-core/runtime/service-catalog' // Map variable type codes to their corresponding component classes export const VARIABLE_TYPE_TO_NAME: Record = { '1': YesNoVariable.name, '2': MultiLineTextVariable.name, '3': MultipleChoiceVariable.name, '4': NumericScaleVariable.name, '5': SelectBoxVariable.name, '6': SingleLineTextVariable.name, '7': CheckboxVariable.name, '8': ReferenceVariable.name, '9': DateVariable.name, '10': DateTimeVariable.name, '11': LabelVariable.name, '12': BreakVariable.name, '14': CustomVariable.name, '15': UIPageVariable.name, '16': WideSingleLineTextVariable.name, '17': CustomWithLabelVariable.name, '18': LookupSelectBoxVariable.name, '19': ContainerStartVariable.name, '20': ContainerEndVariable.name, '21': ListCollectorVariable.name, '22': LookupMultipleChoiceVariable.name, '23': HtmlVariable.name, '24': ContainerSplitVariable.name, '25': MaskedVariable.name, '26': EmailVariable.name, '27': UrlVariable.name, '28': IpAddressVariable.name, '29': DurationVariable.name, '31': RequestedForVariable.name, '32': RichTextLabelVariable.name, '33': AttachmentVariable.name, } // Reverse mapping from API name to type code export const VARIABLE_TYPE_TO_API: Record = Object.fromEntries( Object.entries(VARIABLE_TYPE_TO_NAME).map(([key, value]) => [value, key]) ) export const VARIABLE_NAME_TO_TYPE = Object.fromEntries( Object.entries(VARIABLE_TYPE_TO_NAME).map(([key, value]) => [value, key]) ) as Record /** * Gets the variable type code from its class name * @param className - The class name of the variable (e.g., 'ReferenceVariable') * @returns The corresponding type code or '6' (SingleLineText) as default */ export function getVariableTypeFromName(className: string): string { return VARIABLE_NAME_TO_TYPE[className] || '6' // Default to SingleLineText if not found } // Define the default template constant export const DEFAULT_SAVE_SCRIPT = `/** This script is executed before the Record is generated * \`current\`- GlideRecord produced by Record Producer * Don't use \`current.update()\` or \`current.insert()\` as the record is generated by Record Producer * Don't use \`current.setValue('sys_class_name', 'xxx')\` as this will trigger reparent flow and can cause data loss * Avoid \`current.setAbortAction()\` and generate a separate record * Use \`producer.var1\` to access variables */` export const VariableTypeName = { YES_NO: YesNoVariable.name, MULTI_LINE_TEXT: MultiLineTextVariable.name, MULTIPLE_CHOICE: MultipleChoiceVariable.name, NUMERIC_SCALE: NumericScaleVariable.name, SELECT_BOX: SelectBoxVariable.name, SINGLE_LINE_TEXT: SingleLineTextVariable.name, CHECKBOX: CheckboxVariable.name, REFERENCE: ReferenceVariable.name, DATE: DateVariable.name, DATE_TIME: DateTimeVariable.name, LABEL: LabelVariable.name, BREAK: BreakVariable.name, CUSTOM: CustomVariable.name, UI_PAGE: UIPageVariable.name, WIDE_SINGLE_LINE_TEXT: WideSingleLineTextVariable.name, CUSTOM_WITH_LABEL: CustomWithLabelVariable.name, LOOKUP_SELECT_BOX: LookupSelectBoxVariable.name, CONTAINER_START: ContainerStartVariable.name, CONTAINER_END: ContainerEndVariable.name, LIST_COLLECTOR: ListCollectorVariable.name, LOOKUP_MULTIPLE_CHOICE: LookupMultipleChoiceVariable.name, HTML: HtmlVariable.name, CONTAINER_SPLIT: ContainerSplitVariable.name, MASKED: MaskedVariable.name, EMAIL: EmailVariable.name, URL: UrlVariable.name, IP_ADDRESS: IpAddressVariable.name, DURATION: DurationVariable.name, REQUESTED_FOR: RequestedForVariable.name, RICH_TEXT_LABEL: RichTextLabelVariable.name, ATTACHMENT: AttachmentVariable.name, } as const /** Variable types the ServiceNow platform allows as a dependentQuestion's source (parent) variable. */ const DEPENDENT_QUESTION_PARENT_TYPES: readonly string[] = [VariableTypeName.REFERENCE, VariableTypeName.REQUESTED_FOR] /** Checks whether a variable type name (callee or resolved type) is a valid dependentQuestion parent type. */ export function isValidDependentQuestionParentType(typeName: string | undefined): boolean { return typeName !== undefined && DEPENDENT_QUESTION_PARENT_TYPES.includes(typeName) }