ServiceNow FluentServiceNow SDK

  • 릴리스 버전: Xanadu
  • 업데이트 날짜 2024년 08월 01일
  • 읽기6분
  • 을 사용하여 코드에서 애플리케이션 메타데이터 정의 ServiceNow Fluent및 .ServiceNow SDK

    시작하기 전에

    필요한 역할: admin

    이 태스크 정보

    ServiceNow Fluent 는 애플리케이션을 구성하는 메타데이터 파일[sys_metadata]을 정의하기 위한 TypeScript를 기반으로 하는 DSL(도메인별 언어)이며 테이블, 역할, ACL, 비즈니스 규칙 및 Automated Test Framework 테스트와 같은 다양한 유형의 메타데이터에 대한 API를 포함합니다. API 및 예제에 대한 ServiceNow Fluent 자세한 내용은 을 참조하십시오 ServiceNow Fluent API 참조.

    프로시저

    1. 에서 Visual Studio Code범위가 지정된 응용 프로그램 디렉터리를 엽니다.
    2. src/fluent 디렉터리에서 확장명이 .now.ts인 파일을 추가합니다.
      응용 프로그램 메타데이터를 단일 파일 또는 원하는 만큼 .now.ts 파일로 정의하고 fluent 디렉터리 내의 디렉터리에 파일을 구성할 수 있습니다.
      팁:
      fluent 디렉터리에서 index.now.ts라는 예제 파일을 참조할 수 있습니다.
    3. .now.ts 파일에서 API의 ServiceNow Fluent 개체를 사용하여 응용 프로그램의 메타데이터를 정의합니다.
      예를 들어 애플리케이션에서 테이블 [sys_db_object]을 만들려면 Table API를 사용합니다. 다음 예제에는 필요한 속성 및 값이 있는 Table 개체를 사용하는 할 일 목록에 대한 간단한 표가 포함되어 있습니다.
      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' },
                      in_progress: { label: 'In Progress' },
                  },
              }),
              task: StringColumn({ label: 'Task', maxLength: 120 }),
          },
      })
      주:
      Record API를 사용하여 전용 API가 없는 애플리케이션 메타데이터를 정의할 수 있습니다.
    4. @servicenow/sdk/core에서 API에 필요한 가져오기를 추가합니다.
      예를 들어 날짜 열과 문자열 열이 있는 테이블을 정의하려면 Table API에서 사용할 해당 개체를 임포트합니다.
      import { Table, DateColumn, StringColumn } from '@servicenow/sdk/core'
    5. 옵션: 비즈니스 규칙과 같은 서버 측 스크립트를 사용하는 API의 경우 ServiceNow Fluent JavaScript 모듈에서 코드를 가져와서 객체의 스크립트 속성에서 호출합니다.
      이 예제에서는 script 속성에서 참조할 수 있는 showStateUpdate 함수를 가져옵니다.
      import { showStateUpdate } from '../server/script.js'
    6. 변경 내용을 저장합니다.

    를 사용하여 소스 코드에서 애플리케이션 메타데이터 정의 ServiceNow Fluent

    .now.ts 확장명을 가진 파일에서 API의 ServiceNow Fluent 개체를 사용하여 응용 프로그램의 메타데이터를 정의합니다. @servicenow/sdk/core에서 API에 필요한 가져오기도 포함해야 합니다.

    다음 예시에는 애플리케이션의 테이블, 클라이언트 스크립트 및 비즈니스 규칙의 정의가 포함되어 있습니다. 비즈니스 규칙은 script.js JavaScript 모듈의 함수를 사용합니다.
    import '@servicenow/sdk/global'
    import { BusinessRule } from '@servicenow/sdk/core'
    import { ClientScript } from '@servicenow/sdk/core'
    import { Table, DateColumn, StringColumn } 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' },
                    in_progress: { 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,
        applies_extended: false,
        global: true,
        ui_type: 'all',
        description: 'Custom client script generated by Now SDK',
        messages: '',
        isolate_script: false,
        type: 'onLoad',
        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,
    })

    애플리케이션을 빌드하고 배포한 후 이 소스 코드는 인스턴스에 다음과 같은 애플리케이션 메타데이터 파일을 생성합니다.

    그림 1. 코드에서 ServiceNow Fluent 생성된 애플리케이션 메타데이터
    예제 코드에서 생성된 애플리케이션 파일입니다.

    다음에 수행할 작업

    애플리케이션을 빌드 및 배포하여 소스 코드를 애플리케이션 메타데이터로 컴파일하고 변경 내용을 인스턴스에서 사용할 수 있도록 합니다. 자세한 내용은 를 사용하여 응용 프로그램을 빌드하고 배포합니다. ServiceNow SDK 문서를 참조하십시오.