ServiceNow Fluent
Summarize
Summary of ServiceNow Fluent
ServiceNow Fluent is a declarative, domain-specific language (DSL) based on TypeScript designed for defining application metadata files (sysmetadata) in ServiceNow. It simplifies the creation and management of application components such as tables, roles, ACLs, business rules, and Automated Test Framework tests by allowing developers to define metadata in concise code rather than using form or builder tool interfaces. Fluent supports two-way synchronization, enabling seamless updates between source code and ServiceNow AI Platform metadata.
Show less
Key Features
- Declarative TypeScript DSL: Define application metadata using ServiceNow Fluent’s expressive APIs within .now.ts files.
- APIs for Multiple Metadata Types: Includes dedicated APIs for common metadata and a Record API for less common types.
- Two-Way Synchronization: Changes made via ServiceNow interfaces or source code sync automatically to keep metadata consistent.
- Integration with ServiceNow IDE and SDK: Fluent is supported in applications created or converted with these tools, helping streamline development workflows.
- Script Integration: Server-side scripts (e.g., business rules) and client scripts can import code from JavaScript modules to enhance modularity and reuse.
- Code Directives: Special comments (@fluent-ignore, @fluent-disable-sync, @fluent-disable-sync-for-file) help manage synchronization behavior and suppress diagnostics where needed.
Practical Application
Developers use Fluent by importing the necessary APIs from @servicenow/sdk/core and defining application metadata objects such as tables, client scripts, and business rules directly in code. For example, a table with columns can be defined alongside associated client and server scripts that provide interactive functionality and business logic. This approach creates metadata files on the ServiceNow instance automatically upon build.
Limitations
Certain metadata types, including Metadata Snapshots and UX Assets, are not represented as Fluent code and remain as XML files within the application’s metadata directory.
Benefits for ServiceNow Customers
- Accelerates Development: Reduces the effort needed to define and maintain application metadata through concise, reusable code.
- Improves Source Control: Metadata as code enables better versioning, code review, and collaboration.
- Ensures Consistency: Two-way sync keeps metadata aligned between code and platform interfaces, reducing errors.
- Supports Modular Design: Allows use of JavaScript modules and scripts for better organization and maintainability.
Getting Started
To leverage ServiceNow Fluent, customers should use the ServiceNow IDE or ServiceNow SDK and follow the respective documentation. Reviewing the ServiceNow Fluent API reference and SDK examples repository on GitHub is recommended for the latest supported APIs and usage patterns.
Define application metadata in source code using the ServiceNow Fluent domain-specific programming language.
Overview of ServiceNow Fluent
ServiceNow Fluent is a declarative, 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.
Developers define this metadata in a few lines of code instead of through a form or builder tool user interface. Applications created or converted with the ServiceNow IDE or ServiceNow SDK support development in ServiceNow Fluent.
ServiceNow Fluent supports two-way synchronization, which allows changes to metadata to be synced from other ServiceNow AI Platform user interfaces into source code and changes to source code to be synced back to metadata across the instance.
To get started using the ServiceNow IDE or ServiceNow SDK, see the ServiceNow IDE or ServiceNow SDK documentation.
ServiceNow Fluent APIs
ServiceNow Fluent includes APIs for many types of metadata. You can use the Record API to define application metadata that doesn't have a dedicated API. For the latest list of supported APIs and examples, see the ServiceNow Fluent API reference and ServiceNow SDK examples repository on GitHub.
ServiceNow Fluent usage
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,
})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 the application, this source code generates the following application metadata files on the instance.
@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.