{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":1692,"path":"application-development/automated-test-framework-atf/atf-config-script.md","title":"Step execution scripts","bundle":null,"doc_type":null,"release":"australia","body":"\n# Step execution scripts\n\nIn a step configuration record, the step execution script field determines what a step with this configuration does when it runs.\n\n## Step inputs\n\nThe input variables to a step are determined by the inputs related list in the step configuration record. The **inputs** parameter to `executeScript()` gives the script access to these variables. For example, if the inputs related list contains two records, *var1* and *var2*, the script can reference *var1* with the expression `inputs.var1` and can reference *var2* with `inputs.var2`.\n\n## Step outputs\n\nThe output variables to a step are determined by the outputs related list in the step configuration record. The **outputs** parameter to `executeScript()` gives the script access to these variables. For example, if the outputs related list contains two records, *out1* and *out2*, the script can reference *out1* with the expression `outputs.out1` and can reference *out2* with `outputs.out2`.\n\n## Step result\n\nThe **stepResult** parameter provides access to an API that controls whether the step passes or fails. It also determines the message the step writes to the log.\n\nThe method `stepResult.setSuccess()` causes the step to succeed. The method `stepResult.setFailed()` causes the step to fail.\n\nThe method `stepResult.setOutputMessage()` sets the message to write to the log when the step succeeds or fails. It takes one parameter: the string to write to the log. If the script calls `stepResult.setOutputMessage()` more than once, the most recent value set overwrites any previous value.\n\n## Record Query step execution script\n\n```\n\n    (function executeStep(inputs, outputs, stepResult) {\n         if (gs.nil(inputs.table)) {\n            stepResult.setOutputMessage(gs.getMessage(\"The '{0}' input variable was not specified\",\n               'table'));\n         stepResult.setFailed();\n          return;\n    }\n    var query = new GlideRecord(inputs.table);\n    query.addEncodedQuery(inputs.field_values);\n    query.query();\n    if (!query.next()) {\n         stepResult.setOutputMessage(gs.getMessage(\"No records matching query:\\n{0}\",\n             inputs.field_values));\n          stepResult.setFailed();\n    } else {\n         stepResult.setSuccess();\n         outputs.table = inputs.table;\n         outputs.first_record = query.getUniqueValue();\n         stepResult.setOutputMessage(gs.getMessage(\"Found {0} {1} records matching query:\\n{2}\",\n                                                  [query.getRowCount(),\n                                                   inputs.table,\n                                                   inputs.field_values]));\n    }\n    }(inputs, outputs, stepResult));\n    \n   \n```\n\n## Custom scripted step configs\n\n```\n\n    ((function executeStep(inputs, outputs, stepResult, timeout) {\n\t// Waits up to the timeout for some asynchronous logic to finish\n\t// This script checks for completion once a second for up to 60 seconds\n\tvar counter = 1;\n\t// Try for up to 60 seconds\n\twhile (counter <= timeout) {\n\t\t// If the asynchronous logic is finished, return \"true\" to pass the step\n\t\t// isMyAsyncLogicFinished() can be replaced with any asynchronous event that needs to be tested\n\t\tif (isMyAsyncLogicFinished()) {\n\t\t\tstepResult.setOutputMessage(\"Success!\");\n\t\t\tstepResult.setSuccess();\n\t\t\treturn;\n\t\t}\n\t\t// Wait one second, and log the total number of seconds waited\n\t\tgs.info(\"Waited \" + counter + \" seconds for asynchronous logic to finish\");\n\t\tsn_atf.AutomatedTestingFramework.waitOneSecond();\n\t\tcounter++;\n\t}\n\t// If this point is reached, the retry loop ran out of tries; return false to fail the step\n\tstepResult.setOutputMessage(\"FAILURE: Timed out after waiting for \" + timeout + \" seconds\");\n\tstepResult.setFailed();\n}(inputs, outputs, stepResult, timeout));\n    \n   \n```\n\n**Note:** The above example can also be used for Run Server Side script by replacing `stepResult.setSuccess()`” and `stepResult.setFailed()` with `return true` and `return false`.\n\n**Parent Topic:**[Step configurations](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/application-development/automated-test-framework-atf/step-configurations-module.md)\n\n"}}