{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":48749,"path":"servicenow-platform/remote-tables/remote-table-script-def-example2.md","title":null,"bundle":null,"doc_type":null,"release":null,"body":"---\ntitle: Example 2: Retrieving specific records from a third-party source\ndescription: In this example, we create a script to retrieve specific types of incident records from a third-party source.\nlocale: en-US\ncanonical_url: https://www.servicenow.com/docs/r/australia/servicenow-platform/remote-tables/remote-table-script-def-example2.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 2: Retrieving specific records from a third-party source\n\nIn this example, we create a script to retrieve specific types of incident records from a third-party 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\tif (v_query.isGet()) {\n\t\tbySysId(v_table, v_query, v_query.getSysId());\n\t}\n\telse if (v_query.getParameter(\"caller_id\")) {\n\t\tbyCallerId(v_table, v_query, v_query.getParameter(\"caller_id\"));\n\t}\n\telse {\n\t\tfetchAllIncidents(v_table, v_query);\n\t}\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\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');\n\t\tloadData(v_table, v_query, restMessage);\n\t}\n​\n\t/**\n\t * fetches a specific record from the remote instance with Sys Id being `sys_id`\n\t */\n\tfunction bySysId(v_table, v_query, sys_id) {\n\t\t// Uses RestMessage with name 'Remote Instance Incidents' and function 'By SysId'\n\t\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By SysId');\n\t\t// sets variable 'sys_id'\n\t\trestMessage.setStringParameterNoEscape(\"sys_id\", sys_id);\n\t\tloadData(v_table, v_query, restMessage);\n\t}\n​\n\t/**\n\t * fetches records from the remote instance with Caller (caller_id) being `caller_id`\n\t */\n\tfunction byCallerId(v_table, v_query, caller_id) {\n\t\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By CallerId');\n\t\trestMessage.setStringParameterNoEscape(\"caller_id\", caller_id);\n\t\tloadData(v_table, v_query, restMessage);\n\t}\n​\n\t/**\n\t * Adds rows to `v_table` using `restMessage`\n\t */\n\tfunction loadData(v_table, v_query, restMessage) {\n\t\ttry {\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​\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\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'All Incidents');\n\t\tloadData(v_table, v_query, restMessage);\n\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```\n\tfunction bySysId(v_table, v_query, sys_id) {\n\t\t// Uses RestMessage with name 'Remote Instance Incidents' and function 'By SysId'\n\t\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By SysId');\n\t\t// sets variable 'sys_id'\n\t\trestMessage.setStringParameterNoEscape(\"sys_id\", sys_id);\n\t\tloadData(v_table, v_query, restMessage);\n\t}\n\n```\n\nWhen you query a specific record using `GlideRecord.get(“<sys_id>“)`, then `v_query.isGet()`` is ``true``. It fetches the specific record from the external service.\n\n```\nfunction byCallerId(v_table, v_query, caller_id) {\n\t\tvar restMessage = new sn_ws.RESTMessageV2('Remote Instance Incidents', 'By CallerId');\n\t\trestMessage.setStringParameterNoEscape(\"caller_id\", caller_id);\n\t\tloadData(v_table, v_query, restMessage);\n\t}\n\n```\n\nYou can also handle any other specific query conditions in the script, such as using `caller_id` in the preceding code snippet. The remainder of this script operates in a manner similar to Example 1.\n\n```\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​\n})(v_table, v_query);\n```\n\nIf no problems are encountered in the data retrieval, it gets the data body for the records. It 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`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 available in the remote table.\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"}}