Creating a condition on a target node, referred to as conditional mapping, lets you specify if a source record being processed should be output to the target based on a defined condition. The condition determines (yes or no) if the record is mapped. As part of that determination, you can include logic to set what values are used in the mapping. For example, a record might be used only if the size of a text field is less than a specified maximum.
When using a condition script on a target node, be aware of these notes:
- Conditions can be added to target nodes on any level of the structure depending on the multiplicity as defined by the cardinality key.
- Target nodes that can have a condition can have only one condition for each node. Thus, all logic for the condition must reside in that condition.
- The condition script is always evaluated before any target fields are evaluated. This means that all logic for creating or determining the values for those target fields must be in the condition script if the condition script requires those values in order to complete its determination.
- If multiple conditions are present, they are evaluated in the order from outer node to inner node; if they are on the same level, then they are evaluated in the order from top to bottom.
Adding a Condition
Conditions can be added as scripts on target nodes in any level of a data structure. From either mapping mode or script mode, hover over the node name and click the actions menu icon to open the actions menu. From the menu, select Add condition to node:
Upon selecting this option, if you are not already in script mode, you will be brought into script mode to create a condition by entering a script on the selected node:
The contents of the script specifies the condition for generating the record. A condition must return a
true (1) or
false (0) value. It can contain other logic, and a side effect of that logic can be to set the value of other nodes in the transformation.
The script below requires that the source object
CleanStatus evaluates to
true in order for the record to be processed. If
CleanStatus evaluates to
false, then the node is skipped and this particular record will not be processed:
You can also use the
== equals operator as shorthand to write condition scripts. For example, the same condition used in Example 1 could simply be written as this:
To set a condition such that a record is used only if its flag to publish it was set, you could use a script such as this:
To set a condition such that a record is used only if it is less than a maximum size, assuming that the article text is in the element
json$results$item.body$view$value$, you could use a script such as this:
A condition script can also set values used in other nodes. Building off of Example 4, the condition node can, as part of its logic, set the intended contents of a field to a global variable:
The global variable is then used to set the value in the desired target field
Editing a Condition
Or, hover over the node name and click the actions menu icon to open the actions menu. From the menu, select Edit condition:
If you are not already in script mode, you will be brought into script mode to edit the condition by editing the script on the selected node.
Removing a Condition
The contents of the script will be cleared, removing the condition.
You can also remove a condition by manually editing the condition script to clear the script of its contents.
Last updated: Jul 15, 2019
- No labels