A variable in a workflow model is a way to store a value based on its data type. You can use the name of the variable in any workflow step to retrieve the value stored in the variable. You can also use variable names to define expressions for taking routing decisions.
In AEM Workflow models, you can:
The following video demonstrates how you can create, set, and use variables in AEM Workflow models:
Variables are an extension of the existing MetaDataMap interface. You can use MetaDataMap in ECMAScript to access metadata saved using variables.
You create variables using the Variables section available in the sidekick of the workflow model. AEM Workflow variables support the following data types:
Workflows support only ISO8601 format for Date type variables.
Use ArrayList data type to create variable collections. You can create ArrayList variable for all primitive and complex data types. For example, create an ArrayList variable and select String as subtype to store multiple string values using the variable.
To create a variable:
On an AEM instance, navigate to Tools > Workflow > Models.
Tap Create and specify the title and an optional name for the workflow model. Select the model and tap Edit.
Tap the variables icon available in the sidekick of the workflow model and tap Add Variable.
On the Add Variable dialog, specify the name and select the type of the variable.
Select the data type from the Type drop-down list and specify the following values:
Specify an optional description for the variable and tap to save the changes. The variable displays in the list available in the left pane.
When you create variables, consider the following practices:
You can use the Set Variable step to set value of a variable and define the order in which the values are set. The variable is set in the order the variable mappings are listed in the set variable step.
Changes to variable values affect only the instance of the process in which the change occurs. For example, when a workflow is initiated and variable data changes, the changes affect only that instance of the workflow. The changes do not affect other instances of the workflow that were initiated previously or are initiated later.
Depending on the data type of the variable, you can use the following options to set value of a variable:
Literal: Use the option when you know the exact value to specify. You can also use the option to specify a JSON in the form of a string.
Expression: Use the option when the value to use is calculated based on an expression. The expression is created in provided expression editor.
JSON Dot Notation: Use the option to retrieve a value from a JSON or FDM type variable.
XPATH: Use the option to retrieve a value from an XML type variable.
Relative to payload: Use the option when the value to be saved to variable is available at a path relative to payload.
Absolute path: Use the option when the value to be saved to variable is available at an absolute path.
You can also update specific elements of a JSON or XML type variable using JSON DOT Notation or XPATH notation.
To add mapping between variables:
Select a variable of XML type to store an XML file. Query the XML variable to set the value for a string variable for the property available in the XML file. Use Specify XPATH for the XML variable field to define the property to store in the string variable.
In this example, select a formdata XML variable to store the cc-app.xml file. Query the formdata variable to set the value for the emailaddress string variable to store the value for the emailAddress property available in the cc-app.xml file.
Use an expression to calculate the sum of the variables and store the result in a variable.
In this example, use the expression editor to define an expression to calculate the sum of assetscost and balanceamount variables and store the result in totalvalue variable.
You also use expressions to calculate value of a variable on the runtime. Variables provide an expression editor to define expressions.
Use the expression editor to:
It is based on Adaptive Forms rule editor with following changes. Rule editor in variables:
For more information, see Adaptive Forms rule editor.
You can use variables to retrieve inputs and output or save the result of a step. The workflow editor provides two types of workflow steps:
The Go To step, OR Split step, and all AEM Forms Workflow steps support variables.
The OR Split creates a split in the workflow, after which only one branch is active. This step enables you to introduce conditional processing paths into your workflow. You add workflow steps to each branch as required.
You can define routing expression for a branch using a rule definition, ECMA script, or an external script.
You can use variables to define the routing expression using the expression editor. For more information on using routing expressions for the OR Split step, see OR Split step.
In this example, before defining the routing expression, use example 2 to set the value for the totalvalue variable. Branch 1 is active if the value of the totalvalue variable is greater than 50000. Similarly, you can define a rule to make the Branch 2 active if the value of the totalvalue variable is less than 50000.
Similarly, select an external script path or specify the ECMA script for routing expressions to evaluate the active branch. Tap Rename Branch to specify an alternate name for the branch.
The Goto Step allows you to specify the next step in the workflow model to execute, dependent on the result of a routing expression.
Similar to the OR Split step, you can define routing expression for Goto step using a rule definition, ECMA script, or an external script.
You can use variables to define the routing expression using the expression editor. For more information on using routing expressions for the Goto step, see Goto Step.
In this example, the Goto step specifies the Review Credit Card Application as the next step if the value for the actiontaken variable is equal to Need more info.
For more examples on using rule definition in the Goto step, see Simulating a For loop.
All AEM Forms Workflow steps support variables. For more information, see Forms-centric workflow on OSGi.
You can use MetaDataMap interface to access variables in workflow steps that do not support variables.
Use the following APIs in the ECMA Script to retrieve values for existing variables based on the data type:
Variable data type | API |
---|---|
Primitive (Long, Double, Boolean, Date, and String) | workItem.getWorkflowData().getMetaDataMap().get(variableName, type) |
Document | Packages.com.adobe.aemfd.docmanager.Document doc = workItem.getWorkflowData().getMetaDataMap().get(“docVar”, Packages.com.adobe.aemfd.docmanager.Document.class); |
XML | Packages.org.w3c.dom.Document xmlObject = workItem.getWorkflowData().getMetaDataMap().get(variableName, Packages.org.w3c.dom.Document.class); |
Form Data Model | Packages.com.adobe.aem.dermis.api.FormDataModelInstance fdmObject = workItem.getWorkflowData().getMetaDataMap().get(variableName, Packages.com.adobe.aem.dermis.api.FormDataModelInstance.class); |
JSON | Packages.com.google.gson.JsonObject jsonObject = workItem.getWorkflowData().getMetaDataMap().get(variableName, Packages.com.google.gson.JsonObject.class); |
Example
Retrieve the value of string data type using the following API:
workItem.getWorkflowData().getMetaDataMap().get(accname, Packages.java.lang.String)
Use the following API in the ECMA Script to update the value of a variable:
workItem.getWorkflowData().getMetaDataMap().put(variableName, value)
Example
workItem.getWorkflowData().getMetaDataMap().put(salary, 50000)
Updates the value for the salary variable to 50000.
You can use an API to set variables and pass them to invoke workflow instances.
workflowSession.startWorkflow uses model, wfData, and metaData as arguments. Use MetaDataMap to set value for the variable.
In this API, the variableName variable is set to value using metaData.put(variableName, value);
import com.adobe.granite.workflow.model.WorkflowModel;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.aemfd.docmanager.Document;
/*Assume that you already have a workflowSession and modelId along with the payloadType and payload*/
WorkflowData wfData = workflowSession.newWorkflowData(payloadType, payload);
MetaDataMap metaData = wfData.getMetaDataMap();
metaData.put(variableName, value); //Create a variable "variableName" in your workflow model
WorkflowModel model = workflowSession.getModel(modelId);
workflowSession.startWorkflow(model, wfData, metaData);
Example
Initialize the doc document object to a path (“a/b/c”) and set the value of the docVar variable to the path stored in the document object.
import com.adobe.granite.workflow.WorkflowSession;
import com.adobe.granite.workflow.exec.WorkflowData;
import com.adobe.granite.workflow.model.WorkflowModel;
import com.adobe.granite.workflow.metadata.MetaDataMap;
import com.adobe.aemfd.docmanager.Document;
/*This example assumes that you already have a workflowSession and modelId along with the payloadType and payload */
WorkflowData wfData = workflowSession.newWorkflowData(payloadType, payload);
MetaDataMap metaData = wfData.getMetaDataMap();
Document doc = new Document("/a/b/c");// initialize a document object
metaData.put("docVar",doc); //Assuming that you have created a variable "docVar" of type Document in your workflow model
WorkflowModel model = workflowSession.getModel(modelId);
workflowSession.startWorkflow(model, wfData, metaData);
Before deleting the variable, remove all the references of the variable from the workflow. Ensure that the variable is not used in the workflow.
To delete a variable:
For more examples on using variables in AEM Forms Workflow steps, refer to Variables in AEM workflows.