Define application metadata in code with ServiceNow Fluent in the ServiceNow IDE
Define application metadata in code with ServiceNow Fluent in the ServiceNow IDE.
Avant de commencer
Create, convert, or clone an application and add it to your workspace. For more information, see Adding applications in ServiceNow IDE.
Role required: admin
Pourquoi et quand exécuter cette tâche
ServiceNow Fluent is a domain-specific language (DSL) based on TypeScript for defining the metadata files [sys_metadata] that make up applications and includes APIs for the different types of metadata, such as tables, roles, ACLs, business rules, and Automated Test Framework tests. For more information about ServiceNow Fluent APIs and examples, see ServiceNow Fluent API reference. The ServiceNow IDE has language processing and validation for ServiceNow Fluent APIs and applications by default.
Procédure
@fluent-ignore: Suppresses ServiceNow Fluent diagnostic warnings and errors in the following line of code.@fluent-disable-sync: Turns off syncing changes to a ServiceNow Fluent object. Use before a call expression (for example,Record({ ... })) to turn off syncing for that object and its child objects. Only use this directive if you want to ignore changes made outside of the source code to the object and never update it when syncing.@fluent-disable-sync-for-file: Turns off syncing changes to a ServiceNow Fluent file (.now.ts). Use in the first line of the file to turn off syncing for all code in the file. Only use this directive if you want to ignore changes made outside of the source code to the file and never update it when syncing.
Defining application metadata in source code with ServiceNow Fluent
In files with the .now.ts extension, use objects in the ServiceNow Fluent APIs to define metadata in the application. You must also include the required imports for the APIs from @servicenow/sdk/core. For objects with server-side scripts, such as the BusinessRule object, you can import and use code from JavaScript modules.
import '@servicenow/sdk/global'
import { BusinessRule, ClientScript, DateColumn, StringColumn, Table } from '@servicenow/sdk/core'
import { showStateUpdate } from '../server/script.js'
//creates todo table, with three columns (deadline, status and task)
export const x_snc_example_to_do = Table({
name: 'x_snc_example_to_do',
schema: {
deadline: DateColumn({ label: 'Deadline' }),
state: StringColumn({
label: 'State',
choices: {
ready: { label: 'Ready' },
completed: { label: 'Completed' },
inProgress: { label: 'In Progress' },
},
}),
task: StringColumn({ label: 'Task', maxLength: 120 }),
},
})
//creates a client script that pops up 'Table loaded successfully!!' message everytime todo record is loaded
ClientScript({
$id: Now.ID['cs0'],
name: 'my_client_script',
table: 'x_snc_example_to_do',
active: true,
appliesExtended: false,
global: true,
uiType: 'all',
description: 'Custom client script generated by Now SDK',
isolateScript: false,
type: 'onLoad',
script: Now.include('../client/client-script.js'),
})
//creates a business rule that pops up state change message whenever a todo record is updated
BusinessRule({
$id: Now.ID['br0'],
action: ['update'],
table: 'x_snc_example_to_do',
script: showStateUpdate,
name: 'LogStateChange',
order: 100,
when: 'after',
active: true,
})After building the application, this source code generates the following application metadata files on the instance.