ServiceNow Fluent usage

  • Freigeben Version: Australia
  • Aktualisiert 12. März 2026
  • 5 Minuten Lesedauer
  • Review the following usage guidance for writing ServiceNow Fluent code.

    ServiceNow Fluent files

    You write ServiceNow Fluent code in files with the now.ts extension in the src/fluent directory of an application. You can define application metadata in a single file or as many .now.ts files as you want and organize files in subdirectories within the fluent directory. If needed, the ServiceNow Fluent source directory can be customized with the fluentDir parameter in the now.config.json file for an application.

    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.

    Properties specific to ServiceNow Fluent

    ServiceNow Fluent APIs include some properties that are specific to the ServiceNow Fluent language rather than mapping directly to a field for the metadata defined by the API.

    $id
    Use the $id property to provide a unique ID for the ServiceNow Fluent object. When you build the application, this ID is hashed into a unique sys_id. For example:
    $id: Now.ID['String' or Number]

    The $id property is a required property in many ServiceNow Fluent APIs.

    $meta
    Use the $meta property to provide metadata about the application metadata. With the installMethod property, you can map the application metadata to an output directory that loads only in specific circumstances. For example:
    $meta: {
     installMethod: 'demo' or 'first install'
    }

    The $meta property is an optional property available in all ServiceNow Fluent APIs except the Table API.

    $override
    Use the $override property to configure a field that's not included in a ServiceNow Fluent API or override an existing field. For example:
    $override: {
     field: value, 
     ...
    }

    The $override property is an optional property available in all ServiceNow Fluent APIs.

    Warnung:
    Type safety isn't enforced for fields configured with the $override property.

    Code references

    Properties in ServiceNow Fluent APIs support referring to other code in multiple ways depending on the property. For information about which method a property supports, see the ServiceNow Fluent API reference documentation.

    Function imports
    In some ServiceNow Fluent APIs with server-side scripts, you can import functions from JavaScript modules. Import a function by providing the name of a function, a function expression, or default function exported from a JavaScript module and importing into the .now.ts file. For example:
    import { BusinessRule } from '@servicenow/sdk/core'
    import { FunctionExport } from '../server/scripts.js'
    
    const BR1 = BusinessRule({
        name: 'exportedFunction',
        table: 'x_snc_table',
        when: 'after',
        action: ['update', 'delete', 'insert'],
        script: FunctionExport,
        order: 100,
        active: true,
        add_message: false,
        message: '<p>message</p>',
        abort_action: false,
        $id: Now.ID[0],
    })

    For information about JavaScript modules, see JavaScript modules and third-party libraries.

    Now.include
    With any property, you can refer to content from another file in the application instead of including the content inline using the syntax Now.include('path/to/file'). Referring to content in a different file can be helpful for records with fields that support code, such as JavaScript, HTML, and CSS, to make developing different file types easier with the appropriate syntax highlighting. For example:
    import { Record } from '@servicenow/sdk/core'
    
    export const appCategory = Record({
       table: 'sys_app_category',
       $id: Now.ID['exampleCategory'],
       data: {
          name: 'example',
          style: Now.include('./css-file.css'),
       },
    })

    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.

    Variable identifier
    You can assign a variable to a ServiceNow Fluent object to reference it from other ServiceNow Fluent objects. For example:
    import { Role, Acl } from '@servicenow/sdk/core'
    
    const managerRole = Role({ 
       $id: Now.ID['manager_role'], 
       name: 'x_snc_example.manager' 
    })
    
    const adminRole = Role({ 
       $id: Now.ID['admin_role'], 
       name: 'x_snc_example.admin', 
       contains_roles: [managerRole] 
    })
    
    export default Acl({
        $id: Now.ID['task_delete_acl'],
        active: true,
        admin_overrides: true,
        type: 'record',
        table: 'task',
        field: 'description',
        operation: 'delete',
        roles: [adminRole, managerRole],
    })
    Now.ID
    You can use the Now.ID of a ServiceNow Fluent object to refer to it from other ServiceNow Fluent objects. For example:
    export const appMenu = ApplicationMenu({
        $id: Now.ID['my_app_menu'],
        title: 'My App Menu',
    })
    
    Record({
        table: 'sys_app_module',
        $id: Now.ID['my_record_module'],
        data: {
            application: appMenu.$id
        },
    })

    Code directives

    In code comments, you can use the following directives to help manage your code.
    @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

    The following example includes the definitions of a table, client script, and business rule in the application. The client script uses a script from the client-script.js file. The business rule uses a function from the script.js JavaScript module.
    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'),script: script`function onLoad() {
            g_form.addInfoMessage("Table loaded successfully!!")
        }`,
    })
    
    //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,
    })
    The client script referenced from the ClientScript object:
    function onLoad() {
        g_form.addInfoMessage("Table loaded successfully!!")
    }
    The JavaScript module referenced from the BusinessRule object:
    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.

    Abbildung : 1. Application metadata generated from ServiceNow Fluent code
    Application files generated from the example code.