{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":2390,"path":"application-development/servicenow-fluent.md","title":"ServiceNow Fluent","bundle":null,"doc_type":null,"release":"australia","body":"\n# ServiceNow Fluent\n\nDefine application metadata in source code using the ServiceNow Fluent domain-specific programming language.\n\n## Overview of ServiceNow Fluent\n\nServiceNow 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.\n\nDevelopers define this metadata in a few lines of code instead of through a form or builder tool user interface. Applications created or convertedwith the ServiceNow IDE or ServiceNow SDK support development in ServiceNow Fluent.\n\nServiceNow 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.\n\nTo get started using the ServiceNow IDE or ServiceNow SDK, see the [ServiceNow IDE](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-ide-family-release/servicenow-ide-landing.md) or [ServiceNow SDK](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/servicenow-sdk-landing.md) documentation.\n\n## ServiceNow Fluent APIs\n\nServiceNow 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](https://servicenow.github.io/sdk/) and [ServiceNow SDK examples repository](https://github.com/ServiceNow/sdk-examples) on GitHub.\n\n**Note:** A limited number of metadata types, such as Metadata Snapshots \\[sys\\_metadata\\_link\\] and UX Assets \\[sys\\_ux\\_lib\\_asset\\], can't be represented as ServiceNow Fluent code and aren't transformed. These metadata types remain as metadata XML files in the `metadata` directory of your application.\n\n## ServiceNow Fluent usage\n\nIn 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.\n\nThe 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.\n\n```javascript\nimport '@servicenow/sdk/global'\nimport { BusinessRule, ClientScript, DateColumn, StringColumn, Table } from '@servicenow/sdk/core'\nimport { showStateUpdate } from '../server/script.js'\n\n//creates todo table, with three columns (deadline, status and task)\nexport const x_snc_example_to_do = Table({\n    name: 'x_snc_example_to_do',\n    schema: {\n        deadline: DateColumn({ label: 'Deadline' }),\n        state: StringColumn({\n            label: 'State',\n            choices: {\n                ready: { label: 'Ready' },\n                completed: { label: 'Completed' },\n                inProgress: { label: 'In Progress' },\n            },\n        }),\n        task: StringColumn({ label: 'Task', maxLength: 120 }),\n    },\n})\n\n//creates a client script that pops up 'Table loaded successfully!!' message everytime todo record is loaded\nClientScript({\n    $id: Now.ID['cs0'],\n    name: 'my_client_script',\n    table: 'x_snc_example_to_do',\n    active: true,\n    appliesExtended: false,\n    global: true,\n    uiType: 'all',\n    description: 'Custom client script generated by Now SDK',\n    isolateScript: false,\n    type: 'onLoad',\n    script: Now.include\\('../client/client-script.js'\\),\n})\n\n//creates a business rule that pops up state change message whenever a todo record is updated\nBusinessRule({\n    $id: Now.ID['br0'],\n    action: ['update'],\n    table: 'x_snc_example_to_do',\n    script: showStateUpdate,\n    name: 'LogStateChange',\n    order: 100,\n    when: 'after',\n    active: true,\n})\n```\n\nThe client script referenced from the ClientScript object:\n\n```javascript\nfunction onLoad() {\n    g_form.addInfoMessage(\"Table loaded successfully!!\")\n}\n```\n\nThe JavaScript module referenced from the BusinessRule object:\n\n```javascript\nimport { gs } from '@servicenow/glide'\n\nexport function showStateUpdate(current, previous) { \n    const currentState = current.getValue('state')\n    const previousState = previous.getValue('state')\n\n    gs.addInfoMessage(`state updated from \"${previousState}\" to \"${currentState}\"`)\n}\n```\n\nAfter building the application, this source code generates the following application metadata files on the instance.\n\n\\[Omitted image \"servicenow-ide-app-files.png\"\\] Alt text: Application files generated from the example code.\n\n**Tip:** You can use the following directives in a code comment to help manage your code:\n\n-   `@fluent-ignore`: Suppresses ServiceNow Fluent diagnostic warnings and errors in the following line of code.\n-   `@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.\n-   `@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.\n\n**Related topics**  \n\n\n[ServiceNow Fluent API reference](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/servicenow-fluent-api-reference.md)\n\n[Define application metadata in code with ServiceNow Fluent in the ServiceNow IDE](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-ide-family-release/define-metadata-code-fluent-ide.md)\n\n[Define application metadata in code with ServiceNow Fluent and the ServiceNow SDK](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/servicenow-sdk/define-metadata-code-fluent-sdk.md)\n\n"}}