{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":34777,"path":"mobile/data-source-script-examples.md","title":"Data source script examples","bundle":null,"doc_type":null,"release":"australia","body":"\n# Data source script examples\n\nUse data source scripts to map input values, descriptive elements, and input actions. Alternatively create a dedicated data source for each of these elements.\n\nThe data source script accepts two parameters: *valuesMapper* and *context*. The context parameter refers to the input form that the user is interacting with. The *valuesMapper* parameter is responsible for linking element identifiers to specific table columns via the *addRecordMapping* method. For example, the method call could look like this:\n\n```\nvaluesMapper.addRecordMapping(UNIQUE_ELEMENT_IDENTIFIER, GLIDE_RECORD_INSTANCE, COLUMN_NAME);\n```\n\n**Note:** All the scripts listed in this topic are configured within the data source record. For more information, see [Configure data sources](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/mobile/data-sources-config.md).\n\n## Default sample script\n\nThe following is the sample script provided within the Mobile App Builder. The script provides a template showing how to filter an existing table and to map specific fields to UI elements.\n\n```\n(function DataSource(valuesMapper, context) {\nvar gr = new GlideRecord(TABLE_NAME); // Could be any table within the Instance, including custom tables\ngr.addQuery('sys_id',context.getUniqueValue());\ngr.query();\nif (gr.next()) { // Could also be itetaring muliple records from the same table using the 'while' iterator\n\n    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.\n\n    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.\n\n}\n})(valuesMapper, context);\n\n```\n\nThis sample script contains:\n\n-   The following parameters that define the data source:\n    -   *valuesMapper*: This parameter is used to map the values from the data source to the UI elements.\n    -   *Context*: This parameter specifies the context in which the data source is being used, such as an incident, change request, or any other table.\n-   The *addRecordMapping* method is used to map the fields and consists of the following parameters:\n    -   Element identifier: The unique name you defined for the field you're mapping. In the example, this is `<short description>` and `<priority>`.\n    -   Glide record `<gr>`: This is used to reference any table within the instance, including custom tables.\n    -   Field name: This is the name of the field within the glide record. In the example, this is `<short description>` and `<priority>`.\n\n        **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.\n\n\n## Script to map inputs configured in the input form\n\nThe 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*.\n\n```\n(function DataSource(valuesMapper, context) {\nvar gr = new GlideRecord('incident');\ngr.addQuery('sys_id',context.getUniqueValue());\ngr.query();\nif (gr.next()) {\n// Map the column short_description from the incident table to the elementIdentifier \"input_1_short_description\".\nvaluesMapper.addRecordMapping('input_1_short_description', gr,'short_description'); \n\n// Map the column start_time from the incident table to the elementIdentifier \"input_2_start_time\".\nvaluesMapper.addRecordMapping('input_2_start_time',gr,'start_time'); \n\n// Map the column score from the incident table to the elementIdentifier \"input_3_score\".\nvaluesMapper.addRecordMapping('input_3_score', gr,'score'); \n\n// Map the column assigned_to from the incident table to the elementIdentifier \"input_4_assigned_to\".\nvaluesMapper.addRecordMapping('input_4_assigned_to',gr,'assigned_to');\n\n// Map the attachment from the record in the incident table to the elementIdentifier \"input_5_attachment_1\" (The column name must be “sys_id”).\nvaluesMapper.addRecordMapping('input_5_attachment_1',gr,'sys_id');\n\n }\n})(valuesMapper, context);\n\n```\n\nThis code defines the function *DataSource* that maps specific columns from the Incident \\[incident\\] table record to corresponding element identifiers. The following process occurs:\n\n-   Creates a *GlideRecord* object for the incident \\[incident\\] table.\n-   Queries the table for a record with a specific *sys\\_id* using a unique value from the *context*.\n-   If a record is found, it maps the following columns to their correlated unique element identifier:\n    -   *short\\_description* to *input\\_1\\_short\\_description*\n    -   *start\\_time* to *input\\_2\\_start\\_time*\n    -   *score* to *input\\_3\\_score*\n    -   *assigned\\_to* to *input\\_4\\_assigned\\_to*\n    -   *sys\\_id* to *input\\_5\\_attachment\\_1*\n\n## Script to map descriptive elements associated with inputs or input sections in the input form\n\nThe following is an example script demonstrating how to handle the *ElementIdentifier* attribute of a descriptive element, configured in the descriptive element \\[ sys\\_sg\\_descriptive\\_element\\] table.\n\n```\n(function DataSource (valuesMapper, context) {\nvar gr = new GlideRecord('incident');\ngr.addQuery('sys_id',context.getUniqueValue());\ngr.query();\nif (gr.next()) {\n// 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\"\nvaluesMapper.addRecordMapping('input_1_rich_text_descriptive_element_id',gr,'rich_text_1_col'); \n\n// 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)\"\nvaluesMapper.addRecordMapping('input_1_text_descriptive_element_id',gr,'plain_text_1_col'); \n\n// 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\"\nvaluesMapper.addRecordMapping('input_1_image_descriptive_element_id',gr,'image_1_col'); \n}\n})(valuesMapper, context);\n\n```\n\nThis code defines the function *DataSource* that maps specific columns from the Incident \\[incident\\] table record to corresponding element identifiers. The following process occurs:\n\n-   Creates a *GlideRecord* object for the incident \\[incident\\] table.\n-   Queries the table for a record with a specific *sys\\_id* using a unique value from the *context*.\n-   If a record is found, it maps the following columns to their correlated unique element identifier:\n    -   *rich\\_text\\_1\\_col* \\(HTML type\\) to *input\\_1\\_rich\\_text\\_descriptive\\_element\\_id*\n    -   *plain\\_text\\_1\\_col*\\(String type\\) to *input\\_1\\_text\\_descriptive\\_element\\_id*\n    -   *image\\_1\\_col* \\(Image type\\) to *input\\_1\\_image\\_descriptive\\_element\\_id*\n\n## Script to map input actions associated with inputs in the input form\n\nThe following is an example script demonstrating how to handle the *ElementIdentifier* attribute of input actions configured in the Input form action \\[sys\\_sg\\_parameter\\_action\\] table.\n\n```\n(function DataSource(valuesMapper, context) {\nvar gr = new GlideRecord('incident');\ngr.addQuery('sys_id',context.getUniqueValue());\ngr.query();\nif (gr.next()) {\n// 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.\nvaluesMapper.addRecordMapping('input_1_action_attachments',gr,'sys_id'); \n\n// 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.\nvaluesMapper.addRecordMapping('input_1_action_comment',gr,'comment_by_agent'); \n\n//Handle a navigation type input action with a record context.\nMap the elementIdentifier \"input_1_navigation\" to a record in the incident table by specifying the sys_id as the column in the table.\nvaluesMapper.addRecordMapping('input_1_navigation',gr,'sys_id'); \n}\n})(valuesMapper, context);\n\n```\n\nThis code defines the function *DataSource* that maps specific columns from an Incident \\[incident\\] table record to corresponding element identifiers. The following process occurs:\n\n-   Creates a GlideRecord object for the 'incident' table.\n-   Queries the table for a record with a specific *sys\\_id* using a unique value from the *context*.\n-   If a record is found, it maps the following columns to their correlated unique element identifier:\n    -   *sys\\_id* to *input\\_1\\_action\\_attachments* for handling attachment type input actions.\n    -   *comment\\_by\\_agent*to *input\\_1\\_action\\_comment*for handling comment type input actions.\n    -   *sys\\_id* to *input\\_1\\_navigation* for handling navigation type input actions with a record context.\n\n"}}