<h2>Workflow scratchpad variables</h2><br/><div style="overflow-x:auto"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head><meta content="text/html; charset=UTF-8" /><meta name="copyright" content="(C) Copyright 2025" /><meta name="DC.rights.owner" content="(C) Copyright 2025" /><meta name="generator" content="DITA-OT" /><meta name="DC.type" content="concept" /><meta name="DC.title" content="Using variables in a workflow" /><meta name="abstract" content="Within workflow there are several different types of variables that are available." /><meta name="description" content="Within workflow there are several different types of variables that are available." /><meta name="DC.subject" content="Workflow, variables" /><meta name="keywords" content="Workflow, variables" /><meta name="DC.relation" scheme="URI" content="../../../administer/workflow-administration/concept/managing-workflows.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/general/concept/capabilities-bundle-landingpage.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/general/concept/now-platform-legacy-capabilities.html" /><meta name="DC.relation" scheme="URI" content="../../../administer/workflow/concept/c_WorkflowOverview.html" /><meta name="DC.creator" content="ServiceNow" /><meta name="DC.date.created" content="2023-08-03" /><meta name="DC.date.modified" content="2024-02-01" /><meta name="DC.format" content="XHTML" /><meta name="DC.identifier" content="c_UsingVariablesInAWorkflow" /><link rel="stylesheet" type="text/css" href="../../../CSS/commonltr.css" /><title>Using variables in a workflow</title></head><body> <div class="nested0" id="c_UsingVariablesInAWorkflow"> <h1 class="title topictitle1" id="ariaid-title1">Using variables in a workflow</h1> <div class="body conbody"><p class="shortdesc">Within workflow there are several different types of variables that are available.</p> <p class="p">Some variables define, describe, or compose the pieces of the workflow (such as <a class="xref" href="c_WorkflowActivities.html" title="Workflow activity properties reference">workflow activities</a>). Some variables are available specifically to facilitate the execution of a workflow (such as scratchpad variables). Some variables define, describe, or compose the data records being manipulated by the workflow process (for example, the elements of a Glide Record that are defined in a dictionary.xml file describing a record, such as an incident).</p> </div> <div class="related-links"> <div class="familylinks"> <div class="parentlink"><strong>Parent Topic:</strong> <a class="link" href="../../../administer/workflow-administration/concept/managing-workflows.html" title="Create, edit, validate, and publish workflows to automate multi-step processes across the platform. Understand workflow activities and variables and how to use them effectively. Take a deeper look at how workflows are constructed, validated, and used.">Workflow management</a></div> </div> </div><div class="topic task nested1" id="t_ActivityVariables"> <h2 class="title topictitle2" id="ariaid-title2">Activity variables</h2> <div class="body taskbody"><p class="shortdesc">Activity variables are properties specifically associated with a workflow activity.</p> <div class="section prereq p" id="t_ActivityVariables__prereq_pkm_gvz_nsb"> <p class="p">Role required: admin</p> </div> <div class="section context"> <p class="p">These are the inputs that appear on the activity definition when a specific activity is dragged onto the workflow canvas. These variables are properties that define, compose or describe the workflow activity, or the work it is designed to perform.</p> <p class="p">Activity variables are declared in a related list within the Workflow Activity Definition.</p> <p class="p">To add, view, or modify a variable:</p> </div> <ol class="ol steps"><li class="li step stepexpand"> <span class="ph cmd">Navigate to <span class="ph menucascade"><span class="ph uicontrol">All</span> > <span class="ph uicontrol">Workflow</span> > <span class="ph uicontrol">Activity Definitions</span></span>.</span> </li><li class="li step stepexpand"> <span class="ph cmd">Select a workflow activity, such as <span class="ph uicontrol">Approval - User</span>.</span> </li><li class="li step stepexpand"> <span class="ph cmd">In the <span class="ph uicontrol">Activity Variables</span> section or tab, add, view, or modify the variables.</span> <div class="itemgroup info"> Activity variables are defined using the same fields as <a class="xref" href="../administer/data-dictionary-tables/concept/c_SystemDictionary.html" target="_blank" rel="noopener noreferrer">System dictionary</a>.<div class="p"><div class="note"><span class="notetitle">Note:</span> If defining a new activity variable that is dependent on another activity variable, put the prefix "vars." before the name of the parent activity variable. For example, if the child field is dependent on a field named <span class="ph uicontrol">parent</span>, put the value vars.parent in the <span class="ph uicontrol">Dependent</span> field.</div> </div> <p class="p">To control the visibility of an activity variable on the workflow canvas after the activity has been dragged to the canvas, use a UI policy on the wf_activity table.</p> <div class="p">To access the variables or assign values to these variables within the workflow activity, use the <var class="keyword varname">vars</var> variable of the workflow activity.<pre class="pre codeblock"><code> activity.vars.variable_name = new_variable_value </code></pre></div> </div> </li></ol> </div> </div> <div class="topic task nested1" id="t_WorkflowInputVariables"> <h2 class="title topictitle2" id="ariaid-title3">Workflow input variables</h2> <div class="body taskbody"><p class="shortdesc">Workflow variables are external values that are passed into and referenced by a workflow during its execution.</p> <div class="section prereq p" id="t_WorkflowInputVariables__prereq_xxj_hvz_nsb"> <p class="p">Role required: admin</p> </div> <div class="section context"> <div class="p">Workflow variables can be declared externally in a script and passed into a main workflow or they can be declared within a main workflow and passed as inputs to a subflow.<div class="note"><span class="notetitle">Note:</span> There is a specific kind of input variable for any workflow that is declared against the table Requested Item [sc_request_item]. To understand the use of these variables see <a class="xref" href="c_WorkflowCatalogVariables.html" title="Workflows that are associated with the Requested Item [sc_req_item] table have a specific type of workflow input.">Workflow catalog variables</a>.</div> </div> <p class="p">Workflow variables are declared in a form available from the Workflow Editor gear menu.</p> <p class="p">To add, view, or modify a variable:</p> </div> <ol class="ol steps"><li class="li step stepexpand"> <span class="ph cmd">Navigate to <span class="ph menucascade"><span class="ph uicontrol">All</span> > <span class="ph uicontrol">Workflow</span> > <span class="ph uicontrol">Workflow Editor</span></span>.</span> </li><li class="li step stepexpand"> <span class="ph cmd">Edit and checkout a workflow.</span> </li><li class="li step stepexpand"> <span class="ph cmd">Select the Workflow Activities menu and select <span class="ph uicontrol">Edit Inputs</span>.</span> </li><li class="li step stepexpand"> <span class="ph cmd">Select an existing variable or click <span class="ph uicontrol">New</span>.</span> <div class="itemgroup info"> <p class="p">Note the <span class="ph uicontrol">Column name</span> field. Use this value when accessing the variable from a script.</p> <p class="p">Workflow variables do not appear on the workflow canvas, they are only available for view from the Workflow Activity menu.</p> The input variables are available to use in scripts. To access an input variable from a script:<pre class="pre codeblock"><code>var input = workflow. variables. variableName //<em class="ph i">get the workflow variable identified by column name</em>.</code></pre> </div> </li></ol> </div> </div> <div class="topic concept nested1" id="c_AssignValsToVarsFromOutsideWF"> <h2 class="title topictitle2" id="ariaid-title4">Assigning values to variables from outside of workflow</h2> <div class="body conbody"><p class="shortdesc">After being declared in a workflow, values can be assigned to the variables from where the workflow is kicked off.</p> <div class="p">The following example demonstrates how a workflow variable's value can be set in a script include, business rule, or UI action:<pre class="pre codeblock"><code> //Declare an instance of workflow.js var wf = new Workflow ( ) //Get the workflow id var wfId = wf. getWorkflowFromName ( "Workflow Name" ) ; //Start workflow, passing along name : value pair(s) for mapping to variable //where input_var_name is the name of the variable declared in gear menu //and input_var_value is whatever that value should be for this execution of //workflow wf. startFlow (wfId , null , "Workflow Name" , {input_var_name : input_var_value } ) ;</code></pre></div> <div class="p">Another example that demonstrates a more readable way of passing multiple variables involves declaring an object, adding the names/values to the object, and passing it:<pre class="pre codeblock"><code>//Declare an instance of workflow.js var wf = new Workflow ( ) //Get the workflow id var wfId = wf. getWorkflowFromName ( "Workflow Name" ) ; //Start workflow, passing along object containing name/value pairs mapping to inputs expected by the workflow var vars = { } ; vars. input_var_name1 = input_var_value1 ; vars. input_var_name2 = input_var_value2 ; // add as many variables as your workflow is expecting, then pass the object wf. startFlow (wfId , null , "Workflow Name" , vars ) ;</code></pre></div> <div class="section"><h3 class="title sectiontitle">Assigning Values for Subflow Inputs from inside of a Main Workflow</h3> <p class="p">If a workflow that accepts inputs is called from within another workflow, those input values can be set from the workflow canvas within the UI of the workflow activity. Each input variable declared for a subflow will have a text box input area displayed on the activity. Within these text boxes, the variables can be set with any value either from the scratchpad or from within the current record.</p> <p class="p">For more information, see <a class="xref" href="../../workflow-activities/reference/workflows-as-subflows.html" title="A workflow can launch another workflow as an activity.">Workflows used as subflows</a>.</p> </div> <div class="section"><h3 class="title sectiontitle">Reading the Value of a Workflow Input Variable</h3> <div class="p">The value of a Workflow input variable is accessible anywhere within the workflow that accepts javascript by dot walking the current workflow object:<pre class="pre codeblock"><code>var readValue = workflow.inputs.variable_name;</code></pre></div> </div> </div> </div> <div class="topic concept nested1" id="c_WorkflowScratchpadVariables"> <h2 class="title topictitle2" id="ariaid-title5">Workflow scratchpad variables</h2> <div class="body conbody"><p class="shortdesc">Store and share string-based variables as name-value pairs between workflow activities.</p> <p class="p">The scratchpad is global to the instance of the running workflow and as such, is available equally to all activities.</p> <div class="section"><h3 class="title sectiontitle">Declaration</h3> <div class="p">The scratchpad itself is automatically available to an executing workflow and requires no specific declaration. Variables are declared and stored in the scratchpad simultaneously by referencing it. For example:<pre class="pre codeblock"><code>workflow.scratchpad.variableName = variableValue;</code></pre></div> </div> <div class="section"><h3 class="title sectiontitle">Display</h3> <p class="p"><span class="ph">View activity scratchpad values from the Workflow Activity History Scratchpad [wf_history_scratchpad] table. Workflow Activity History Scratchpad is also an embedded list in Worfklow Activity History records.</span></p> <div class="p"><div class="fig fignone" id="c_WorkflowScratchpadVariables__fig_i22_pt5_b2b"><span class="figcap"><span class="fig--title-label">Figure 1. </span>Sample Workflow Activity History Scratchpad records</span> <img class="image" id="c_WorkflowScratchpadVariables__image_sth_zt5_b2b" src="../image/wf_history_scratchpad.png" alt="" /> </div> </div> </div> <div class="section"><h3 class="title sectiontitle">Access and Assignment</h3> <div class="p">Use standard JavaScript object notation to access or assign scratchpad values. For example, use this format to assign a particular scratchpad variable value.<pre class="pre codeblock"><code>workflow.scratchpad.variableName = variableValue;</code></pre></div> <div class="p">Use this format to assign a JavaScript variable to a scratchpad variable.<pre class="pre codeblock"><code>var myValue = workflow.scratchpad.variableName;</code></pre></div> </div> </div> </div> <div class="topic concept nested1" id="c_CurrentVariables"> <h2 class="title topictitle2" id="ariaid-title6">Current variables</h2> <div class="body conbody"><p class="shortdesc">Current is the database GlideRecord that kicked off the workflow, either by association to the table in the Workflow properties table or by being associated with a catalog item.</p> <div class="section"><h3 class="title sectiontitle">Declaration</h3> <p class="p">Variables in current are the columns that are defined in the <span class="ph filepath">dictionary.xml</span> file that support the database record. There is no way at runtime to add variables to the current record.</p> </div> <div class="section"><h3 class="title sectiontitle">Display</h3> <p class="p">Variables of a current record are displayed on the Glide Forms and Lists throughout the applications that use them.</p> </div> <div class="section"><h3 class="title sectiontitle">Access and Assignment</h3> <div class="p">To access the variables or assign values to the variables within the workflow activity, get the value from the current record by referencing the Glide Element.<pre class="pre codeblock"><code>var myVarValue = current.getElement(nameOfTheField);</code></pre></div> <div class="p">or<pre class="pre codeblock"><code>var myVarValue = current.fieldName;</code></pre></div> <div class="p">or<pre class="pre codeblock"><code>current.variableName.setValue( "A Variable Value");</code></pre></div> </div> </div> </div> </div> </body></html></div>