데이터 소스 스크립트 예시
데이터 소스 스크립트를 사용하여 입력 값, 설명 요소 및 입력 작업을 매핑합니다. 또는 이러한 각 요소에 대한 전용 데이터 소스를 생성합니다.
데이터 소스 스크립트에는 valuesMapper 및 context라는 두 개의 매개 변수를 사용할 수 있습니다. 컨텍스트 매개변수는 사용자가 상호작용하는 입력 양식을 참조합니다. valuesMapper 매개변수는 addRecordMapping 메서드를 통해 요소 식별자를 특정 테이블 열에 연결하는 역할을 합니다. 예를 들어, 메서드 호출은 다음과 같을 수 있습니다.
valuesMapper.addRecordMapping(UNIQUE_ELEMENT_IDENTIFIER, GLIDE_RECORD_INSTANCE, COLUMN_NAME);주:
이 주제에 나열된 모든 스크립트는 데이터 소스 기록 내에서 구성됩니다. 자세한 내용은 데이터 소스 구성 문서를 참조하십시오.
기본 샘플 스크립트
다음은 모바일 앱 작성기 내에서 제공되는 샘플 스크립트입니다. 이 스크립트는 기존 테이블을 필터링하고 특정 필드를 UI 요소에 매핑하는 방법을 보여주는 템플릿을 제공합니다.
(function DataSource(valuesMapper, context) {
var gr = new GlideRecord(TABLE_NAME); // Could be any table within the Instance, including custom tables
gr.addQuery('sys_id',context.getUniqueValue());
gr.query();
if (gr.next()) { // Could also be itetaring muliple records from the same table using the 'while' iterator
valuesMapper.addRecordMapping('short_description',gr,'short_description'); // Map the field short_description to a UI element that has element identifier of "short_description". The element identifier could be any String, it's easier to map the column's name as the element identifer.
valuesMapper.addRecordMapping('priority',gr,'priority'); // Map the field priority to a UI element that has element identifier of "priority". The element identifier could be any String, it's easier to map the column's name as the element identifier.
}
})(valuesMapper, context);
이 샘플 스크립트에는 다음이 포함됩니다.
- 데이터 소스를 정의하는 다음 매개변수:
- valuesMapper: 이 매개변수는 데이터 소스의 값을 UI 요소에 매핑하는 데 사용됩니다.
- 컨텍스트: 이 매개변수는 인시던트, 변경 요청 또는 기타 테이블과 같이 데이터 소스가 사용되는 컨텍스트를 지정합니다.
- addRecordMapping 메서드는 필드를 매핑하는 데 사용되며 다음 매개변수로 구성됩니다.
- 요소 식별자: 매핑하는 필드에 대해 정의한 고유 이름입니다. 이 예시에서는
<짧은 설명>및<우선순위>입니다. - Glide 기록
<gr>: 사용자 지정 테이블을 포함하여 인스턴스 내의 모든 테이블을 참조하는 데 사용됩니다. - 필드 이름: Glide 기록 내의 필드 이름입니다. 이 예시에서는
<짧은 설명>및<우선순위>입니다.주:일반적인 지침은 코드를 더 쉽게 유지 관리하고 이해할 수 있도록 요소 식별자의 이름을 필드 이름과 동일하게 지정하는 것입니다.
- 요소 식별자: 매핑하는 필드에 대해 정의한 고유 이름입니다. 이 예시에서는
입력 양식에 구성된 입력을 매핑하는 스크립트
다음은 속성 [sys_sg_input_attribute] 테이블에 구성된 입력의 ElementIdentifier 속성이 처리되는 방법을 보여 주는 예제 스크립트입니다. 이 스크립트에는 input_1, input_2, input_3, input_4input_5라는 5개의 입력으로 구성된 인시던트 테이블의 열에 입력 값을 매핑하는 예시가 포함되어 있습니다.
(function DataSource(valuesMapper, context) {
var gr = new GlideRecord('incident');
gr.addQuery('sys_id',context.getUniqueValue());
gr.query();
if (gr.next()) {
// Map the column short_description from the incident table to the elementIdentifier "input_1_short_description".
valuesMapper.addRecordMapping('input_1_short_description', gr,'short_description');
// Map the column start_time from the incident table to the elementIdentifier "input_2_start_time".
valuesMapper.addRecordMapping('input_2_start_time',gr,'start_time');
// Map the column score from the incident table to the elementIdentifier "input_3_score".
valuesMapper.addRecordMapping('input_3_score', gr,'score');
// Map the column assigned_to from the incident table to the elementIdentifier "input_4_assigned_to".
valuesMapper.addRecordMapping('input_4_assigned_to',gr,'assigned_to');
// Map the attachment from the record in the incident table to the elementIdentifier "input_5_attachment_1" (The column name must be “sys_id”).
valuesMapper.addRecordMapping('input_5_attachment_1',gr,'sys_id');
}
})(valuesMapper, context);
이 코드는 인시던트 [incident] 테이블 레코드의 특정 열을 해당 요소 식별자에 매핑하는 함수 DataSource 를 정의합니다. 다음 프로세스가 발생합니다.
- 인시던트 [incident] 테이블에 대한 GlideRecord 객체를 생성합니다.
- 컨텍스트의 고유 값을 사용하여 특정 sys_id 있는 기록에 대한 테이블을 쿼리합니다.
- 기록이 발견되면 다음 열을 상관 관계가 있는 고유 요소 식별자에 매핑합니다.
- input_1_short_description short_description
- input_2_start_time start_time
- input_3_score 점수
- input_4_assigned_to assigned_to
- input_5_attachment_1 sys_id
입력 양식의 입력 또는 입력 섹션과 연결된 설명 요소를 매핑하는 스크립트
다음은 설명 요소 [ sys_sg_descriptive_element] 테이블에 구성된 설명 요소의 ElementIdentifier 특성을 처리하는 방법을 보여 주는 예제 스크립트입니다.
(function DataSource (valuesMapper, context) {
var gr = new GlideRecord('incident');
gr.addQuery('sys_id',context.getUniqueValue());
gr.query();
if (gr.next()) {
// Handle descriptive element of type Rich Text. Map to a column named "rich_text_1_col" in the incident table to the elementIdentifier "input_1_rich_text_descriptive_element_id". The col type in the "incident" should be "HTML"
valuesMapper.addRecordMapping('input_1_rich_text_descriptive_element_id',gr,'rich_text_1_col');
// Handle descriptive element of type Text. Map to a column named "plain_text_1_col" in the incident table to the elementIdentifier "input_1_text_descriptive_element_id". The col type in the "incident" should be "String"/"String (Full UTF-8)"
valuesMapper.addRecordMapping('input_1_text_descriptive_element_id',gr,'plain_text_1_col');
// Handle descriptive element of type Image. Map to a column named "image_1_col" in the incident table to the elementIdentifier "input_1_image_descriptive_element_id". The col type in the "incident" should be "Image"
valuesMapper.addRecordMapping('input_1_image_descriptive_element_id',gr,'image_1_col');
}
})(valuesMapper, context);
이 코드는 인시던트 [incident] 테이블 레코드의 특정 열을 해당 요소 식별자에 매핑하는 함수 DataSource 를 정의합니다. 다음 프로세스가 발생합니다.
- 인시던트 [incident] 테이블에 대한 GlideRecord 객체를 생성합니다.
- 컨텍스트의 고유 값을 사용하여 특정 sys_id 있는 기록에 대한 테이블을 쿼리합니다.
- 기록이 발견되면 다음 열을 상관 관계가 있는 고유 요소 식별자에 매핑합니다.
- rich_text_1_col (HTML 유형)에서 input_1_rich_text_descriptive_element_id
- plain_text_1_col (문자열 유형)에서 input_1_text_descriptive_element_id
- image_1_col (이미지 유형)에서 input_1_image_descriptive_element_id
입력 양식의 입력과 연결된 입력 작업을 매핑하는 스크립트
다음은 입력 양식 작업 [sys_sg_parameter_action] 테이블에 구성된 입력 작업의 ElementIdentifier 속성을 처리하는 방법을 보여주는 예제 스크립트입니다.
(function DataSource(valuesMapper, context) {
var gr = new GlideRecord('incident');
gr.addQuery('sys_id',context.getUniqueValue());
gr.query();
if (gr.next()) {
// Handle an attachment type input action. Map the elementIdentifier "input_1_action_attachments" to a record in the incident table by specifying the sys_id as the column in the table.
valuesMapper.addRecordMapping('input_1_action_attachments',gr,'sys_id');
// Handle a comment type input action. Map the elementIdentifier "input_1_action_comment" to a column in the incident table where the comment is stored. In this example, the comment is stored in the //"comment_by_agent" column.
valuesMapper.addRecordMapping('input_1_action_comment',gr,'comment_by_agent');
//Handle a navigation type input action with a record context.
Map the elementIdentifier "input_1_navigation" to a record in the incident table by specifying the sys_id as the column in the table.
valuesMapper.addRecordMapping('input_1_navigation',gr,'sys_id');
}
})(valuesMapper, context);
이 코드는 인시던트 [incident] 테이블 레코드의 특정 열을 해당 요소 식별자에 매핑하는 DataSource 함수를 정의합니다. 다음 프로세스가 발생합니다.
- "인시던트" 테이블에 대한 GlideRecord 객체를 생성합니다.
- 컨텍스트의 고유 값을 사용하여 특정 sys_id 있는 기록에 대한 테이블을 쿼리합니다.
- 기록이 발견되면 다음 열을 상관 관계가 있는 고유 요소 식별자에 매핑합니다.
- 첨부 파일 유형 입력 작업을 처리하기 위한 input_1_action_attachments sys_id입니다.
- 댓글 유형 입력 작업을 처리하기 위한 input_1_action_comment comment_by_agent입니다.
- 기록 컨텍스트로 탐색 유형 입력 작업을 처리하기 위한 sys_id input_1_navigation입니다.