If
  • Dark
    Light

If

  • Dark
    Light


If component

The If component can evaluate an expression involving variables and direct the flow of an Orchestration Job depending upon whether the expression evaluates to "true" or "false".

The If component has three output connectors. A blue connector is followed when the expression evaluates to "true". An orange connector is followed when the expression evaluates to "false". The 'Red' connector is followed if a runtime error occurs during evaluation.

Variables involved in the expression must be declared in advance. Some general help on defining and using variables is available here.

For similar flow components, see the And component and the Or component.


Properties

Property Setting Description
Name String A human-readable name for the component.
Mode Select Simple: uses the Condition and Combine Conditions properties to define an expression.
Advanced: provides a code editor to write your own JavaScript expression.
Condition (Simple Mode) Input Variable The variable to use in the comparison.
Qualifier Select Is or Not.
Not reverses the meaning of the comparator; for example, changing an Equals to a Not Equals expression.
Comparator Select the comparison operator. If the input variable is null, it will be considered blank.
Value Most commonly, a constant value, but can also be a variable reference or expression. The value is parsed according to the type of the input variable.
Combine Conditions Select Use the defined conditions in combination with one another according to either And or Or.
Condition (Advanced Mode Only) String A JavaScript expression, which will be evaluated to determine whether it is true or false. If the expression is not itself a true or false value, then any non-empty string is considered true, as is any non-zero number.

Important Information

  • JavaScript expressions are only available to use as parameter values within Matillion ETL components. Any valid single JavaScript expression may be used; however, it is recommended that only simple expressions are used. All variables defined in the job and/or environment should also be available to reference.
  • When entering values into a component's parameter editor, everything enclosed within ${ } of the literal string will be evaluated immediately. This validation process currently does not take variables into account and may assume the value is incorrect.

Example

This example loads data from S3 and checks that at least 1,000 rows were loaded. We define an expression that tests whether the number of loaded rows is greater than 1,000, and when this is false, an alert is sent via SNS (and the job is marked as failed).

A numeric variable called "loaded_rows" is defined, with a default value of 0.

The "Row Count" from the S3 Load is exported into the new "loaded_rows" variable. This is on the "Export" tab after selecting the S3 Load component.

The "If" component can then be configured.

The configuration is simple in this case. Notice we are checking that the loaded rows are greater than 1000—the flow shows that when this is false (the orange connector), then the alert is sent.



Example 2

In this example, we load data in from a Query component to perform some statistical analysis. However, this analysis is really only meaningful if we have over 200 rows of data to work with. In light of this, we use an If component to decide whether or not to transform our loaded data. The job is shown below.

We want to the If component to compare the number of rows to a value so our first action is to create a variable to hold the number of rows loaded. Navigate to Project→Manage Environment Variables and add a new variable of Numeric Type.

This variable can now be used to hold the row count in our Query component (in this case, Jira Query) by navigating to the component's 'Export' tab and clicking 'Edit'. Select the source as Row Count and the target variable is our newly created Numeric variable.

Our If component is set up as below. We have set the mode to Advanced.

Advanced mode allows us to write our own conditional statements in the "Condition" property. Shown below, we ensure our row count exceeds 200.

When this job is run, the Task panel will confirm that the If component has been evaluated as true.

For the sake of example, requiring over 1200 rows will result in a failure, since our Jira Query component has been limited to 1000 records. In this case, the Task panel will report that the condition has not been met.



Example 3

In this example, we use the If component to branch a job into one of several paths based on the state of a variable. The variable 'sftp_inbound_path' is a string that is 'data_google_campaign' if we're looking at data from Google or 'data_bing_campaign' if we're looking at data from Bing. We don't want to transform both data sets the same way, so this is an ideal method for creating a job that can handle both, and choose an appropriate course of action. The job is shown below.

The If component has three possible outcomes: (1.) our condition is met; (2.) it is valid but not met; (3.) the condition is invalid (an error occurs). The If component properties are shown below. Here, we have used the 'contains' java function to compare our variable string to 'google_campaign'.

So if our variable is 'data_google_campaign', this contains 'google_campaign' and will be true and lead to the GoogleTransform job. If our variable is 'data_bing_campaign', the If component will evaluate false and lead to the BingTransform job. However, if this variable is not defined, we will receive an error and the job will terminate gracefully by leading to the End Failure component. The Task log output for each outcome (Success, Failure, Error, from top to bottom) is shown below.



Example 4

Advanced mode allows users to write their own conditions in JavaScript that must evaluate to be true or false. This can be particularly useful for building highly specific conditions for your workflow. A common example of this is checking for specific filenames before entering a workflow. Below, we give the JavaScript condition for continuing only if the variable 'filename' holds one of three filenames, 'data2016.csv', 'data18.csv', 'data_current.csv'.

filename.indexOf('data2016.csv') > -1 
|| filename.indexOf('data18.csv') > -1
|| filename.indexOf('data_current.csv') > -1


Video


What's Next