Tutorial part 3: Define a table in ServiceNow Fluent code

  • Rversion finale: Australia
  • Mis à jour 12 mars 2026
  • 5 minutes de lecture
  • Create a table and reference it in sample script definitions using the ServiceNow Fluent APIs.

    Avant de commencer

    Complete Tutorial part 2: Initialize a repository for your application.

    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. You use objects in the ServiceNow Fluent APIs to define metadata in files with the .now.ts extension. The ServiceNow IDE has language processing and validation for ServiceNow Fluent APIs and applications by default. For more information about ServiceNow Fluent, see ServiceNow Fluent.

    In this example, you create a simple table for a to-do list using objects in the ServiceNow Fluent Table API. Then, you update the sample code for business rule and client script definitions to reference the new table. Lastly, you review your changes from the File Categories view. For more information about the Table API, see Table API - ServiceNow Fluent.

    Procédure

    1. From the Activity Bar, select the File Explorer view (File Explorer).
    2. Navigate to the src/fluent directory in your application.
    3. Open the index.now.ts sample file.
      Conseil :
      You can write ServiceNow Fluent code in a single file or as many .now.ts files as you want and organize files in directories within the fluent directory.
    4. On a new line at the end of the file, enter Table({}) to add the Table object.
    5. From the Status Bar, select the diagnostics icon (Diagnostics) to open the Problems panel and check for issues in the code.

      Quick Fix menu with options for quick fixes

    6. Right-click the error that appears and select the Update import from "@servicenow/sdk/core" quick fix.

      Quick fix to add the Table import from @servicenow/sdk/core in the Problems panel

      In line 2, the Table object is added to the list of imports from @servicenow/sdk/core:
      import { BusinessRule, ClientScript, Table } from '@servicenow/sdk/core'
      Conseil :
      After fixing this issue, you can close the Problems panel while you proceed through the following steps. In a later step, you return to it if any issues remain.
    7. In the Table object, add the following properties.
      • name: The table name must begin with the application scope and use all lowercase letters in the following format: <scope>_<name>. You can find the scope in the now.config.json file for the application.
      • label: The label should be unique and appears for the table in list and form views.
      • extends: The name of another table on which the table is based.
      Table({
          name: 'x_snc_hello_world_to_do', //ensure that the name begins with the correct scope (<scope>_<name>)
          label: 'To-do Items',
          extends: 'task',
      })
      Conseil :
      Hover over an object to see its in-product documentation.

      Documentation for the Table API appears when hovering over the Table object.

    8. For type-ahead support when defining columns in the table, before the Table object, add an exported variable with the same name as the name property.
      export const x_snc_hello_world_to_do = Table({
          name: 'x_snc_hello_world_to_do',
          label: 'To-do Items',
          extends: 'task',
      })
    9. In the Table object, add the schema property to define columns in the table.
      export const x_snc_hello_world_to_do = Table({
          name: 'x_snc_hello_world_to_do',
          label: 'To-do Items',
          extends: 'task',
          schema: {
              //define columns here
          }
      })

      The schema property is an array of Column objects. There are many types of columns based on the field type. Column objects use the format <Type>Column where <Type> is the field type.

      Use the following details to define three columns in the table: Deadline, Matrix, and Task. Refer to the Column object documentation to help you configure each column.

      Column name Details
      deadline
      • Label: Deadline
      • Type: Date/Time
      matrix
      • Label: Matrix
      • Type: String
      • Choices:
        • Label: Urgent and Important
        • Label: Important but Not Urgent
        • Label: Urgent but Not Important
        • Label: Neither Urgent nor Important
      task
      • Label: Task
      • Type: String
      • Max length: 120
      With these details, the schema property should look similar to this example. The keys that you use for the choices can be any string.
      export const x_snc_hello_world_to_do = Table({
          name: 'x_snc_hello_world_to_do',
          label: 'To-do Items',
          extends: 'task',
          schema: {
              deadline: DateColumn({ label: 'Deadline' }),
              matrix: StringColumn({
                  label: 'Matrix',
                  choices: {
                      do: { label: 'Urgent and Important' },
                      decide: { label: 'Important but Not Urgent' },
                      delegate: { label: 'Urgent but Not Important' },
                      delete: { label: 'Neither Urgent nor Important' },
                  },
              }),
              task: StringColumn({ label: 'Task', maxLength: 120 }),
          },
      })
    10. In line 2, add imports for the DateColumn and StringColumn objects.
      import { BusinessRule, ClientScript, Table, DateColumn, StringColumn } from '@servicenow/sdk/core'
    11. Update the existing business rule and client script definitions to reference the table you created.
      1. In the ClientScript object, change the value of the table property to the table name (x_snc_hello_world_to_do).
      2. Repeat the previous step for the BusinessRule object.
    12. If the diagnostics icon (Diagnostics) shows any errors or warnings, select it to open the Problems panel and review the diagnostic messages and quick fixes to resolve them.
    13. Save your changes using one of the following keyboard shortcuts.
      • Windows: Ctrl-S
      • Mac: Cmd-S
      Remarque :
      If you have unsaved changes in a file, a dot icon appears on the file tab.
    14. From the Status Bar, select Build and Install.

      If the installation completes successfully, the updated ServiceNow Fluent source code is compiled into Application Files [sys_metadata] on the instance.

    15. Review your changes as metadata.
      1. From the Activity Bar, select the File Categories view.
      2. Select your application to expand it.
      3. Navigate to Data > Table and select To-do Items.
        The table opens in Table Builder.

        The To-do Items table in Table Builder accessed from the File Categories view in the ServiceNow IDE.

    16. Facultatif : Edit the metadata and synchronize your changes into the source code.
      From the File Categories view, you can simulate another user editing the metadata outside of the source code to see changes transformed back into the code you added.
      1. In Table Builder, search for the Task field and change the Column label from "Task" to "To Do".
      2. Select Save.
      3. From the Activity Bar, select the Now SDK view (Now SDK).
      4. Select Sync Changes.

        The Sync command in the Now SDK view.

        In the index.now.ts file, you should see the label property of the task column changed to 'To Do'.
        task: StringColumn({ label: 'To Do', maxLength: 120 }),

    Résultats

    You have created your first application metadata using ServiceNow Fluent APIs. The To-do Items [x_snc_hello_world_to_do] table can be modified in source code by other ServiceNow IDE users or from other ServiceNow AI Platform user interfaces.

    From the ServiceNow AI Platform, you can navigate to the list view of the table by entering x_snc_hello_world_to_do.list in the navigation filter. Because you updated the client script definition to run on the To-do Items [x_snc_hello_world_to_do] table, if you select New to add a record to the table, the message from the client script appears when the record loads.

    The message string from the client script definition appears when a new record is added to the To-do Items table

    Que faire ensuite

    Continue to Tutorial part 4: Install and use a third-party library.