{"componentChunkName":"component---src-templates-default-template-tsx","path":"/ki-syntax/functions/javascript/","result":{"data":{"asciidoc":{"id":"13865894-88d9-54f1-8070-f0170522b622","html":"<div id=\"toc\" class=\"toc\">\n<div id=\"toctitle\">Table of Contents</div>\n<ul class=\"sectlevel1\">\n<li><a href=\"#definition\">Definition</a></li>\n<li><a href=\"#usage\">Usage</a></li>\n<li><a href=\"#example\">Example</a>\n<ul class=\"sectlevel2\">\n<li><a href=\"#multiple-return-values\">Multiple return values</a></li>\n<li><a href=\"#directly-map-values-and-keys\">Directly map Values and Keys</a></li>\n<li><a href=\"#parsing-json-data-into-variables\">Parsing JSON data into variables</a></li>\n<li><a href=\"#caveats\">Caveats</a></li>\n</ul>\n</li>\n</ul>\n</div>\n<div class=\"sect1\">\n<h2 id=\"definition\">Definition</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>Use JavaScript expressions to assign values to variables.</p>\n</div>\n<div class=\"paragraph\">\n<p>HIRO contains a full JavaScript engine that can be used to manipulate data.</p>\n</div>\n<div class=\"admonitionblock important\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-important\" title=\"Important\"></i>\n</td>\n<td class=\"content\">\nThe JavaScript interpreter <strong>does not support</strong> the use of external 3rd party libraries.\n</td>\n</tr>\n</table>\n</div>\n<table class=\"tableblock frame-all grid-all stretch\">\n<caption class=\"title\">Table 1. Technical Details</caption>\n<colgroup>\n<col style=\"width: 50%;\">\n<col style=\"width: 50%;\">\n</colgroup>\n<tbody>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">JavaScript Engine</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Google V8 (Version 7.7.299.13-node.16)</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Script Standard</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">ECMAScript 2019</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Executed on</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Node 12</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"usage\">Usage</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>The JavaScript element allows processing data within a Knowledge Item.\nThe final \"result\" returned by the element will be stored in the Variable specified in the <code>StoreTo</code> field.</p>\n</div>\n<div class=\"paragraph\">\n<p>If the JavaScript expression returns, for example, a list of filenames it will store this list as a single long value. You can use further\nprocessing of the output to store the results as separate values for each line/each entry in an array or a map of \"Key:Value\" pairs (see examples below).</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">exit: LOCAL::EXIT\nresult: LOCAL::RESULT = javascript(\"&lt;js-script&gt;\")</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>Default timeout for javascript execution is 5 seconds, but can be extended upto 600 seconds by providing the 2nd optional argument:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">exit: LOCAL::EXIT\nresult: LOCAL::RESULT = javascript(\"&lt;js-script&gt;\", 20)</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>Also named options are supported:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">exit: LOCAL::EXIT\nresult: LOCAL::RESULT = javascript(timeout: 30, code: \"&lt;js-script&gt;\")</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>If specified timeout is more than 10 minutes, timeout is reset back to default (5 seconds).</p>\n</div>\n<table class=\"tableblock frame-all grid-all stretch\">\n<caption class=\"title\">Table 2. Arguments</caption>\n<colgroup>\n<col style=\"width: 25%;\">\n<col style=\"width: 25%;\">\n<col style=\"width: 25%;\">\n<col style=\"width: 25%;\">\n</colgroup>\n<thead>\n<tr>\n<th class=\"tableblock halign-left valign-top\">Position</th>\n<th class=\"tableblock halign-left valign-top\">Keyword</th>\n<th class=\"tableblock halign-left valign-top\">Required</th>\n<th class=\"tableblock halign-left valign-top\">Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">1</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">:code</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">X</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Script to be evaluated</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">2</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">:timeout</p></td>\n<td class=\"tableblock halign-left valign-top\"></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Timeout in seconds (max: 600, default: 5)</p></td>\n</tr>\n</tbody>\n</table>\n<div class=\"paragraph\">\n<p>&#160;<br></p>\n</div>\n<table class=\"tableblock frame-all grid-all stretch\">\n<caption class=\"title\">Table 3. Return Values</caption>\n<colgroup>\n<col style=\"width: 50%;\">\n<col style=\"width: 50%;\">\n</colgroup>\n<thead>\n<tr>\n<th class=\"tableblock halign-left valign-top\">Tag</th>\n<th class=\"tableblock halign-left valign-top\">Description</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">result</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Result of the script evaluation</p></td>\n</tr>\n<tr>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">exit</p></td>\n<td class=\"tableblock halign-left valign-top\"><p class=\"tableblock\">Numeric Exit-Code of the operation, 0 = success, 1 = failure</p></td>\n</tr>\n</tbody>\n</table>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"example\">Example</h2>\n<div class=\"sectionbody\">\n<div class=\"sect2\">\n<h3 id=\"multiple-return-values\">Multiple return values</h3>\n<div class=\"sect3\">\n<h4 id=\"as-separate-values\">as separate values</h4>\n<div class=\"paragraph\">\n<p>You can use this functionality to write multiple return values as values into a variable by simply splitting your output.</p>\n</div>\n<div class=\"paragraph\">\n<p>For example: You return a list of filenames as single lines that is stored in the variable \"result\":</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-bash\" data-lang=\"bash\">/opt/myfiles/abc.txt\n/opt/myfiles/def.txt\n/opt/myfiles/hij.txt</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>You would then split this output using the \"newline separator\" (<code>\\n</code>)\nwith the following JavaScript snippet:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">result: LOCAL::OutputVariable = javascript(\"${result}.split('\\n')\")</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>This will split the contents of <code>result</code> after each \"newline\"-character\n(<code>\\n</code>) and save it into separate values of \"OutputVariable\". You would\nthen see something like this in your Issue:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-yaml\" data-lang=\"yaml\">\"/OutputVariable\": [\"/opt/myfiles/abc.txt\", \"/opt/myfiles/def.txt\", \"/opt/myfiles/hij.txt\"]</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>You can use CSV input by using <code>\"${result}\".split(\",\")</code> in your call in\na similar way.</p>\n</div>\n</div>\n<div class=\"sect3\">\n<h4 id=\"convert-to-json-map\">Convert to JSON map</h4>\n<div class=\"paragraph\">\n<p>If you require multiple return values, these can be encoded in a simple\nJSON map and afterwards be accessed by the JSON map accessor from a\n<a href=\"/7.0/ki-syntax/definitions/varstrings/\">VarString</a>:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-javascript\" data-lang=\"javascript\">result: OutputVariable = javascript('\n  var result = {};\n  result[\"ValueOne\"] = new Date;\n  result[\"ValueTwo\"] = \"this is just some string\";\n  JSON.stringify(result);\n')</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>This way you end up with a JSON object looking like this:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-json\" data-lang=\"json\">  {\"ValueOne\":\"Wed Dec 09 2015 14:11:45 GMT+0100 (CET)\",\"ValueTwo\":\"this is just some string\"}</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>Which you can then freely access by using VarString in the format <code>${VARIABLE#value_in_object}</code> like in this example <code>${OutputVariable#ValueOne}</code>.\nPlease note that <a href=\"/7.0/ki-syntax/definitions/varstrings/\">VarString</a> only allows accessing the object directly,\nif you have a JSON structure that contains lists or other objects as values, you need to parse the structure to be able to easily access all elements.</p>\n</div>\n</div>\n</div>\n<div class=\"sect2\">\n<h3 id=\"directly-map-values-and-keys\">Directly map Values and Keys</h3>\n<div class=\"paragraph\">\n<p>If you wish to set Values and their respective keys at the same time you need to directly map them. To do so you will use a JavaScript call to\nset a list of \"Key:Value\" pairs which then are stored as separate entries in a variable.</p>\n</div>\n<div class=\"paragraph\">\n<p>Here we store two values with different keys to the variable <code>SampleMap</code>.</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-javascript\" data-lang=\"javascript\">result: SampleMap = javascript('\n  var result = {};\n  result[\"SomeKey\"] = \"SomeValue\"\n  result[\"2ndKey\"] = \"OtherValue\"\n  result\n')</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>Note that we did not use a JSON.stringify() this time but actually return the map directly. This will create a data structure consisting of the specified variable containing \"Key:Value\" pairs as individual values and will look like this in the Issue:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-json\" data-lang=\"json\">{\n  \"/SampleMap\": [\n    {\n      \"value\": \"SomeValue\",\n      \"key\": \"SomeKey\"\n    },\n    {\n      \"value\": \"OtherValue\",\n      \"key\": \"2ndKey\"\n    }\n  ]\n}</code></pre>\n</div>\n</div>\n</div>\n<div class=\"sect2\">\n<h3 id=\"parsing-json-data-into-variables\">Parsing JSON data into variables</h3>\n<div class=\"admonitionblock tip\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-tip\" title=\"Tip\"></i>\n</td>\n<td class=\"content\">\n\"Escaped characters\" The JavaScript handling for HIRO automatically escapes special characters for Variables.\nThis means JSON data in a variable will be \"invalid\" because of the escaped quotes (<code>\"</code>) to use JSON data stored this way you must use ''JSON.parse()''\nto read the Variable and transform it back to \"proper\" JSON.\n</td>\n</tr>\n</table>\n</div>\n<div class=\"paragraph\">\n<p>Here we have a simplified version of a Zendesk ticket in JSON (retrieved via API).\nWe want to extract the description and store it in a variable to react further.</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-json\" data-lang=\"json\">{\n  \"ticket\": {\n    \"url\": \"https://itautopilot.zendesk.com/api/v2/tickets/2289.json\",\n    \"id\": 2289,\n    \"external_id\": null,\n    \"via\": {\n      \"channel\": \"web\",\n      \"source\": {\n        \"from\": {},\n        \"to\": {},\n        \"rel\": null\n      }\n    },\n    \"created_at\": \"2016-01-14T13:19:35Z\",\n    \"updated_at\": \"2016-01-14T14:03:34Z\",\n    \"type\": null,\n    \"subject\": \"Test Ticket - Please ignore and don't close\",\n    \"raw_subject\": \"Test Ticket - Please ignore and don't close\",\n    \"description\": \"Just testing some stuff.\",\n  }\n}</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>We have already retrieved the JSON data and stored it in a variable <code>ZendeskTicketJSON</code>.\nWe then parse the Variable into an object called <code>obj</code>. Then we access the value in the JSON structure.</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-javascript\" data-lang=\"javascript\">const obj = JSON.parse('${ZendeskTicketJSON}');\nobj.ticket.description</code></pre>\n</div>\n</div>\n<div class=\"paragraph\">\n<p>This will return the value of \"description\" as a string and store it to\nthe specified \"StoreTo\" Variable.</p>\n</div>\n</div>\n<div class=\"sect2\">\n<h3 id=\"caveats\">Caveats</h3>\n<div class=\"admonitionblock important\">\n<table>\n<tr>\n<td class=\"icon\">\n<i class=\"fa icon-important\" title=\"Important\"></i>\n</td>\n<td class=\"content\">\n<div class=\"paragraph\">\n<p>HIRO uses Javascript <strong>ES 2019</strong>. Make sure the elements you are trying to use are compatible with this script version.</p>\n</div>\n</td>\n</tr>\n</table>\n</div>\n</div>\n</div>\n</div>","document":{"main":"javascript","title":"javascript","subtitle":""},"fields":{"toc":true,"location":["ki-syntax","functions","javascript"]}},"sidebarYaml":{"id":"6f28bdca-d703-5e79-ae00-b0508f8ee82a","showIndex":true}},"pageContext":{"id":"13865894-88d9-54f1-8070-f0170522b622","parent":"ki-syntax"}},"staticQueryHashes":["1010459453","1010459453","2356112386","2356112386","2603905930","2603905930","3026652197","3026652197","3167850324","3167850324","63159454","63159454"]}