Data source script examples
Summarize
Summary of Data source script examples
Data source scripts in ServiceNow enable you to map input values, descriptive elements, and input actions from database records to UI elements in forms or mobile app interfaces. These scripts use thevaluesMapperandcontextparameters to dynamically link data fields from specific tables to unique element identifiers defined in the UI, facilitating seamless data integration and user interaction.
Show less
Key Features
- Parameters:
valuesMapper: Maps element identifiers to table columns using theaddRecordMappingmethod.context: Represents the current input form context, providing access to the unique record identifier.
- Mapping Method: The
addRecordMapping(elementIdentifier, glideRecord, columnName)method links a UI element (by its identifier) to a specific field in a GlideRecord instance. - GlideRecord Usage: Scripts create GlideRecord objects to query relevant tables (e.g., Incident table) using the record’s
sysidfrom the context. - Element Identifier Naming: Best practice is to name element identifiers the same as the corresponding field names for maintainability.
- Script Variants Provided:
- Default sample script filtering by
sysidand mapping fields likeshortdescriptionandpriority. - Script to map input form fields configured in the
syssginputattributetable to incident table columns. - Script to map descriptive elements (rich text, plain text, images) from the
syssgdescriptiveelementtable. - Script to map input actions defined in the
syssgparameteractiontable, such as attachments, comments, and navigation actions.
- Default sample script filtering by
Practical Use for ServiceNow Customers
By implementing these data source scripts, ServiceNow customers can:
- Efficiently bind backend table data to front-end input fields, descriptive elements, and action triggers within forms or mobile applications.
- Customize data presentation and interaction based on dynamic user context, enhancing user experience and data accuracy.
- Leverage out-of-the-box templates to accelerate development and ensure consistent data mapping across custom and standard tables.
- Maintain clear and manageable code by aligning element identifiers with database field names.
These scripts are configured within data source records, enabling flexible and scalable integration for various data-driven UI components in your ServiceNow environment.
Use data source scripts to map input values, descriptive elements, and input actions. Alternatively create a dedicated data source for each of these elements.
valuesMapper.addRecordMapping(UNIQUE_ELEMENT_IDENTIFIER, GLIDE_RECORD_INSTANCE, COLUMN_NAME);Default sample script
(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);
- The following parameters that define the data source:
- valuesMapper: This parameter is used to map the values from the data source to the UI elements.
- Context: This parameter specifies the context in which the data source is being used, such as an incident, change request, or any other table.
- The addRecordMapping method is used to map the fields and consists of the following parameters:
- Element identifier: The unique name you defined for the field you're mapping. In the example, this is
<short description>and<priority>. - Glide record
<gr>: This is used to reference any table within the instance, including custom tables. - Field name: This is the name of the field within the glide record. In the example, this is
<short description>and<priority>.Note:A general guideline is to name the element identifier the same as the field name, to make the code easier to maintain and understand.
- Element identifier: The unique name you defined for the field you're mapping. In the example, this is
Script to map inputs configured in the input form
The following is an example script demonstrating how the ElementIdentifier attribute of inputs configured in the Attributes [sys_sg_input_attribute] table is processed. The script contains examples of mapping an input value to a column in the incident table of five inputs named input_1, input_2, input_3, input_4, input_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);
- Creates a GlideRecord object for the incident [incident] table.
- Queries the table for a record with a specific sys_id using a unique value from the context.
- If a record is found, it maps the following columns to their correlated unique element identifier:
- short_description to input_1_short_description
- start_time to input_2_start_time
- score to input_3_score
- assigned_to to input_4_assigned_to
- sys_id to input_5_attachment_1
Script to map descriptive elements associated with inputs or input sections in the input form
(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);
- Creates a GlideRecord object for the incident [incident] table.
- Queries the table for a record with a specific sys_id using a unique value from the context.
- If a record is found, it maps the following columns to their correlated unique element identifier:
- rich_text_1_col (HTML type) to input_1_rich_text_descriptive_element_id
- plain_text_1_col (String type) to input_1_text_descriptive_element_id
- image_1_col (Image type) to input_1_image_descriptive_element_id
Script to map input actions associated with inputs in the input form
(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);
- Creates a GlideRecord object for the 'incident' table.
- Queries the table for a record with a specific sys_id using a unique value from the context.
- If a record is found, it maps the following columns to their correlated unique element identifier:
- sys_id to input_1_action_attachments for handling attachment type input actions.
- comment_by_agent to input_1_action_comment for handling comment type input actions.
- sys_id to input_1_navigation for handling navigation type input actions with a record context.