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 enables developers to define metadata such as tables, roles, ACLs, business rules, and Automated Test Framework tests through concise code instead of using forms or builder tools. Fluent supports two-way synchronization between source code and metadata within the ServiceNow AI Platform, allowing seamless updates in either direction.
Show less
Applications developed or converted using the ServiceNow IDE or ServiceNow SDK support Fluent, facilitating metadata management as source code. To begin using Fluent, customers should consult the ServiceNow IDE or ServiceNow SDK documentation.
Key Features
- Declarative API-based Metadata Definition: Fluent provides APIs for many metadata types, including a Record API for those without dedicated APIs, allowing flexible and efficient metadata coding.
- Two-Way Synchronization: Changes made in the ServiceNow UI sync back to source code and vice versa, maintaining consistency across development environments.
- TypeScript-Based DSL: Metadata is defined in .now.ts files using Fluent APIs, with support for importing server-side scripts from JavaScript modules for business rules and other objects.
- Code Management Directives: Fluent supports special comment directives like @fluent-ignore, @fluent-disable-sync, and @fluent-disable-sync-for-file to control diagnostics and synchronization behavior for specific code lines or files.
- Partial Metadata Coverage: Some metadata types (e.g., Metadata Snapshots, UX Assets) are not supported in Fluent and remain as XML files within the application’s metadata directory.
Practical Usage
Developers write Fluent code to define tables, client scripts, business rules, and other metadata components. For example, a table with columns can be defined alongside client and business rule scripts that reference external JavaScript modules. This approach simplifies application development and metadata management by using code-centric workflows.
After code is written and the application is built, ServiceNow Fluent generates corresponding metadata files in the instance, ensuring the metadata is correctly applied within the platform.
Benefits for ServiceNow Customers
- Streamlined Application Development: Define and manage application metadata quickly and efficiently using code rather than manual UI configuration.
- Improved Synchronization: Keep source code and instance metadata aligned, reducing errors and manual effort.
- Enhanced Maintainability: Use TypeScript’s strong typing and modular scripts to build more maintainable and scalable applications.
- Flexibility: Integrate server-side scripts and external JavaScript modules seamlessly within metadata definitions.
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.