{"revision":"c10276e953456835cdbb1f3a60d58ae1980be353","document":{"id":3739,"path":"build-workflows/legacy-workflow/c_UsingVariablesInAWorkflow.md","title":"Using variables in a workflow","bundle":null,"doc_type":null,"release":"australia","body":"\n# Using variables in a workflow\n\nWithin workflow there are several different types of variables that are available.\n\nSome variables define, describe, or compose the pieces of the workflow \\(such as [workflow activities](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/build-workflows/workflow-activities/c_WorkflowActivities.md)\\). 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\\).\n\n**Parent Topic:**[Workflow management](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/build-workflows/legacy-workflow/managing-workflows.md)\n\n## Activity variables\n\nActivity variables are properties specifically associated with a workflow activity.\n\n### Before you begin\n\nRole required: admin\n\n### About this task\n\nThese 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.\n\nActivity variables are declared in a related list within the Workflow Activity Definition.\n\nTo add, view, or modify a variable:\n\n### Procedure\n\n1.  Navigate to **All** &gt; **Workflow** &gt; **Activity Definitions**.\n\n2.  Select a workflow activity, such as **Approval - User**.\n\n3.  In the **Activity Variables** section or tab, add, view, or modify the variables. Activity variables are defined using the same fields as [System dictionary](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/platform-administration/c_SystemDictionary.md).\n\n    **Note:** 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 **parent**, put the value `vars.parent` in the **Dependent** field.\n\n    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.\n\n    To access the variables or assign values to these variables within the workflow activity, use the *vars* variable of the workflow activity.\n\n    ```\n     activity.vars.variable_name = new_variable_value \n    ```\n\n\n## Workflow input variables\n\nWorkflow variables are external values that are passed into and referenced by a workflow during its execution.\n\n### Before you begin\n\nRole required: admin\n\n### About this task\n\nWorkflow 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.\n\n**Note:** 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 [Workflow catalog variables](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/build-workflows/legacy-workflow/c_WorkflowCatalogVariables.md).\n\nWorkflow variables are declared in a form available from the Workflow Editor gear menu.\n\n### Procedure\n\n1.  Navigate to **All** &gt; **Workflow** &gt; **Workflow Editor**.\n\n2.  Edit and check out a workflow.\n\n3.  Select the Workflow Activities menu and select **Edit Inputs**.\n\n4.  Select an existing variable or select **New**.\n\n    Note the **Column name** field. Use this value when accessing the variable from a script.\n\n    Workflow variables do not appear on the workflow canvas, they are only available for view from the Workflow Activity menu.\n\n    The input variables are available to use in scripts. To access an input variable from a script:\n\n    ```\n    var input  = workflow. variables. variableName //*get the workflow variable identified by column name*.\n    ```\n\n\n## Assigning values to variables from outside of workflow\n\nAfter being declared in a workflow, values can be assigned to the variables from where the workflow is kicked off.\n\nThe following example demonstrates how a workflow variable's value can be set in a script include, business rule, or UI action:\n\n```\n \n//Declare an instance  of workflow.js \nvar wf  = new Workflow ( ) //Get the workflow id \nvar  wfId  = wf. getWorkflowFromName ( \"Workflow Name\" ) ; \n//Start workflow, passing along name : value pair(s) for mapping to variable \n//where input_var_name is the name of the variable declared in gear menu \n//and input_var_value is whatever that value should be for this execution of \n//workflow\nwf. startFlow (wfId , null , \"Workflow Name\" , {input_var_name : input_var_value } ) ;\n```\n\nAnother 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:\n\n```\n//Declare an instance  of workflow.js \nvar wf  = new Workflow ( ) \n//Get the workflow id \nvar  wfId  = wf. getWorkflowFromName ( \"Workflow Name\" ) ; \n//Start workflow, passing along object containing name/value pairs mapping to inputs expected by the workflow \nvar vars  = { } ;\nvars. input_var_name1 = input_var_value1 ;\nvars. input_var_name2 = input_var_value2 ; \n// add as many variables as your workflow is expecting, then pass the object\nwf. startFlow (wfId , null , \"Workflow Name\" , vars ) ;\n```\n\n### Assigning Values for Subflow Inputs from inside of a Main Workflow\n\nIf 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.\n\nFor more information, see [Workflows used as subflows](https://raw.githubusercontent.com/ServiceNow/ServiceNowDocs/australia/markdown/australia/build-workflows/legacy-workflow/workflows-as-subflows.md).\n\n### Reading the Value of a Workflow Input Variable\n\nThe value of a Workflow input variable is accessible anywhere within the workflow that accepts javascript by dot walking the current workflow object:\n\n```\nvar readValue = workflow.inputs.variable_name;\n```\n\n## Workflow scratchpad variables\n\nStore and share string-based variables as name-value pairs between workflow activities.\n\nThe scratchpad is global to the instance of the running workflow and as such, is available equally to all activities.\n\n### Declaration\n\nThe 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:\n\n```\nworkflow.scratchpad.variableName = variableValue;\n```\n\n### Display\n\nView 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.\n\n\\[Omitted image \"wf\\_history\\_scratchpad.png\"\\] Alt text:\n\n### Access and Assignment\n\nUse standard JavaScript object notation to access or assign scratchpad values. For example, use this format to assign a particular scratchpad variable value.\n\n```\nworkflow.scratchpad.variableName = variableValue;\n```\n\nUse this format to assign a JavaScript variable to a scratchpad variable.\n\n```\nvar myValue = workflow.scratchpad.variableName;\n```\n\n## Current variables\n\nCurrent 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.\n\n### Declaration\n\nVariables in current are the columns that are defined in the `dictionary.xml` file that support the database record. There is no way at runtime to add variables to the current record.\n\n### Display\n\nVariables of a current record are displayed on the Glide Forms and Lists throughout the applications that use them.\n\n### Access and Assignment\n\nTo 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.\n\n```\nvar myVarValue = current.getElement(nameOfTheField);\n```\n\nor\n\n```\nvar myVarValue = current.fieldName;\n```\n\nor\n\n```\ncurrent.variableName.setValue( \"A Variable Value\");\n```\n\n"}}