{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":48748,"path":"servicenow-platform/remote-tables/remote-table-script-def-example1.md","title":null,"bundle":null,"doc_type":null,"release":null,"body":"---\ntitle: Example 1: Retrieving all incident records from an external source\ndescription: These are examples of script definitions you might create for retrieval and caching of data from sources external to your current instance. In this first example, we create a script to load all incident records from an external source.\nlocale: en-US\ncanonical_url: https://www.servicenow.com/docs/r/australia/servicenow-platform/remote-tables/remote-table-script-def-example1.html\nrelease: australia\nproduct: Remote Tables\nclassification: remote-tables\ntopic_type: reference\nlast_updated: \"2026-03-12\"\nreading_time_minutes: 4\nbreadcrumb: [Create script definitions, Remote tables, Manage instance data sources, Extend ServiceNow AI Platform capabilities]\n---\n\n# Example 1: Retrieving all incident records from an external source\n\nThese are examples of script definitions you might create for retrieval and caching of data from sources external to your current instance. In this first example, we create a script to load all incident records from an external source.\n\nFor Remote Table API information, refer to:\n\n-   [v\\_query – Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/v_queryAPI.md)\n-   [v\\_record - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/v_recordAPI.md)\n-   [v\\_table – Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/v_tableAPI.md)\n\n```javascript\n/**\n * Using `v_query`, add the rows to `v_table`\n */\n(function executeQuery(v_table, v_query) {\n​\n\tfetchAllIncidents(v_table, v_query);\n​\n\t/**\n\t * fetch all incidents records from the remote instance\n\t */\n\tfunction fetchAllIncidents(v_table, v_query) {\n\t\t// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'\n\t\t// Create a RestMessage first which calls an external REST service\n\t\ttry {\n\t\t\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');\n\t\t\tvar response = restMessage.execute();\n\t\t\tvar responseBody = response.getBody();\n\t\t\t\n\t\t\t// if REST call ends up in an error, set the last error message which shows up\n\t\t\t// at the bottom of the list view\n\t\t\tif (response.haveError()) {\n\t\t\t\tv_query.setLastErrorMessage(response.getErrorMessage());\n\t\t\t\t// can use gs.error() or gs.addErrorMessage() while debugging\n\t\t\t\t// gs.debug() messages visible in session debugger\n\t\t\t\t// gs.debug(response.getErrorMessage());\n\t\t\t\treturn;\n\t\t\t}\n\t\t} catch (ex) {\n\t\t\tv_query.setLastErrorMessage(ex.message);\n\t\t\t// gs.debug(ex.message);\n\t\t\treturn;\n\t\t}\n​\n\t\tvar transformerDefinition = getTransformerDefinition();\n\t\tvar transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody);\n\t\t// transformer parses the responseBody and extracts rows\n\t\twhile (transformer.transform()) {\n\t\t\t// row is field-value map e.g. { active:\"true\", number: \"INC0000001\"}\n\t\t\tvar row = transformer.getRow();\n\t\t\t// you may do any additional transformations to the row like GlideDuration, GlideDataTime etc. For example,\n\t\t\t// row.duration = new GlideDuration(row.duration);\n​\n\t\t\t// finally add the row to the remote table\n\t\t\tv_table.addRow(row);\n\t\t}\n\t}\n​\n\t/**\n\t * returns a sn_tfrm.TransformerDefinition, which defines the mapping of the table fields and elements in the response body\n\t */\n\tfunction getTransformerDefinition() {\n\t\t// create a rule list to map a field to its element path\n\t\tvar ruleList = new sn_tfrm.TransformerRuleList()\n\t\t\t.fromJSON() // the response body is a JSON\n\t\t\t// 'active' field maps to path '$.active'\n\t\t\t.addRule(\"active\", \"$.active\")\n\t\t\t.addRule(\"caller_id\", \"$.caller_id.value\")\n\t\t\t.addRule(\"number\", \"$.number\")\n\t\t\t.addRule(\"short_description\", \"$.short_description\")\n\t\t\t.addRule(\"sys_id\", \"$.sys_id\")\n\t\t\t.addRule(\"updates\", \"$.sys_mod_count\");\n​\n\t\tvar recordPath = \"$.result\";\n\t\treturn new sn_tfrm.TransformerDefinition(ruleList, recordPath);\n\t}\n\t\n})(v_table, v_query);\n```\n\nThese code snippets are of note in this script:\n\n```\nfunction fetchAllIncidents(v_table, v_query) {\n\t\t// Uses RestMessage with name 'Remote Instance Incidents' and function 'All Incidents'\n\t\t// Create a RestMessage first which calls an external REST service\n\t\ttry {\n\t\t\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');\n\t\t\tvar response = restMessage.execute();\n\t\t\tvar responseBody = response.getBody();\n\t\t\t\n\t\t\t// if REST call ends up in an error, set the last error message which shows up\n\t\t\t// at the bottom of the list view\n\t\t\tif (response.haveError()) {\n\t\t\t\tv_query.setLastErrorMessage(response.getErrorMessage());\n\t\t\t\t// can use gs.error() or gs.addErrorMessage() while debugging\n\t\t\t\t// gs.debug() messages visible in session debugger\n\t\t\t\t// gs.debug(response.getErrorMessage());\n\t\t\t\treturn;\n\t\t\t}\n\t\t} catch (ex) {\n\t\t\tv_query.setLastErrorMessage(ex.message);\n\t\t\t// gs.debug(ex.message);\n\t\t\treturn;\n\t\t}\n```\n\nYou can create a RestMessage and directly use it in the script. In this example, it uses a RESTMessageV2 API with the name of `Remote Instance Incidents`, and the function `All Incidents`, which gets all incident data. Once a response is returned from the server, an error message appears if problems are encountered in the data retrieval.\n\n**Note:** To learn more about use of RESTMessageV2 and how to define a direct message, see [RESTMessageV2 - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/c_RESTMessageV2API.md) and [Direct RESTMessageV2 example](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/web-services/r_DirectRESTMessageV2Example.md).\n\nIf no problems are encountered in the data retrieval, it gets the data body for the records.\n\n```\n\t\tvar transformerDefinition = getTransformerDefinition();\n\t\tvar transformer = new sn_tfrm.Transformer(transformerDefinition, responseBody);\n\t\t// transformer parses the responseBody and extracts rows\n\t\twhile (transformer.transform()) {\n\t\t\t// row is field-value map e.g. { active:\"true\", number: \"INC0000001\"}\n\t\t\tvar row = transformer.getRow();\n\t\t\t// you may do any additional transformations to the row like GlideDuration, GlideDataTime etc. For example,\n\t\t\t// row.duration = new GlideDuration(row.duration);\n​\n\t\t\t// finally add the row to the remote table\n\t\t\tv_table.addRow(row);\n\n```\n\nIt then uses the Transformer API to perform any required data transformations, extracts rows, and then adds a row for each record to the remote table.\n\n```\n/**\n\t * returns a sn_tfrm.TransformerDefinition, which defines the mapping of the table fields and elements in the response body\n\t */\n\tfunction getTransformerDefinition() {\n\t\t// create a rule list to map a field to its element path\n\t\tvar ruleList = new sn_tfrm.TransformerRuleList()\n\t\t\t.fromJSON() // the response body is a JSON\n\t\t\t// 'active' field maps to path '$.active'\n\t\t\t.addRule(\"active\", \"$.active\")\n\t\t\t.addRule(\"caller_id\", \"$.caller_id.value\")\n\t\t\t.addRule(\"number\", \"$.number\")\n\t\t\t.addRule(\"short_description\", \"$.short_description\")\n\t\t\t.addRule(\"sys_id\", \"$.sys_id\")\n\t\t\t.addRule(\"updates\", \"$.sys_mod_count\");\n​\n\t\tvar recordPath = \"$.result\";\n\t\treturn new sn_tfrm.TransformerDefinition(ruleList, recordPath);\n\t}\n\t\n})(v_table, v_query);\n\n```\n\n`getTransformerDefinition` defines the schema of the record in the external API response body. It maps each of the fields in the table script to an element in the external record. Any external data elements outside of this mapping are not supported or retrieved.\n\n**Note:** You must map a sys\\_id in the transformer definition to an element in the external data. In this case, the sys\\_id is mapped to the external incident sys\\_id. The maximum length of the sys\\_id is 32 characters. You do this sys\\_id mapping so forms that use the external data can operate properly.\n\n**Parent Topic:**[Create script definitions for a remote table](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/remote-tables/create-remote-table-script.md)\n\n**Related topics**  \n\n\n[Debugging remote table script definitions](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/servicenow-platform/remote-tables/debugging-remote-table-scripts.md)\n\n[TransformerDefinition API - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/TransformerDefinitionAPI.md)\n\n[TransformerRuleList API - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/TransformerRuleListAPI.md)\n\n[TransformerScripted API - Scoped, Global](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/api-reference/server-api-reference/TransformerScriptedAPI.md)\n\n"}}