Define application metadata in code with ServiceNow Fluent and the ServiceNow SDK
Define application metadata in code with ServiceNow Fluent and the ServiceNow SDK.
시작하기 전에
- Install the ServiceNow Fluent Language server to get language processing and validation for ServiceNow Fluent in Visual Studio Code. For more information, see Install the ServiceNow Fluent Language server in Visual Studio Code.
- Add an application to your local system with the ServiceNow SDK. For more information, see Adding applications with the ServiceNow SDK.
Role required: admin
이 태스크 정보
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.
프로시저
@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.
Now.include('path/to/file'). Referring to content in a different file instead of including the content inline can
be helpful for records with fields that support JavaScript, HTML, CSS, and so on to make developing different file types easier with the appropriate syntax highlighting. The Now.include utility supports two-way synchronization so changes to fields from other ServiceNow AI Platform user interfaces are synced into the referenced file's source code and changes to the code are synced back to metadata across the instance.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,
})function onLoad() {
g_form.addInfoMessage("Table loaded successfully!!")
}import { gs } from '@servicenow/glide'
export function showStateUpdate(current, previous) {
const currentState = current.getValue('state')
const previousState = previous.getValue('state')
gs.addInfoMessage(`state updated from "${previousState}" to "${currentState}"`)
}After building and installing the application, this source code generates the following application metadata files on the instance.