+

Search Tips   |   Advanced Search

Null Behavior: Dealing with missing Attributes/values

To deal with missing values we can either use the built-in Null Behavior feature of Attribute Maps, or we can detect and handle this yourself.

Let's start by configuring Null Behavior. Do this by right-clicking on the 'Read_CSV_File' Connector in the Attribute Maps panel (not in the AL components tree-view) and selecting Null Behavior from the context menu1. Null Behavior button for AL-level configuration

This brings up the Null Behavior dialog where we can configure both how null is defined – which can vary depending on the type of source you are reading from – as well as how this situation should be handled. By default, null means that an Attribute is missing, and the default handling is to remove this Attribute from the mapping operation. The end result of this is that no Attribute with the specified name will be found in the receiving Entry.

Once in the Null Behavior dialog, use the radio buttons on the right to define null as an empty string value, and then those on the left to specify a default value of "* missing *" to be returned in this case. Null Behavior configuration dialog

Re-run the AssemblyLine and refresh the browser window displaying the contents of Output.xml. The entry for 'Roger' should now have the special null value ("* missing *") for 'Title' and 'LastName'. Result in the XML output of Null Behavior settings

This is somewhat better – at least it's a conscious choice. However, sometimes an entry is just too incomplete to continue processing. In our scenario you need at least values for 'FirstName' and 'LastName' in order to compute 'FullName', so you will now add filtering logic to the AssemblyLine to ensure that all entries fulfill this requirement.

Start by clicking the Insert Component button again and then choosing Control/Flow Components from the radio buttons at the left. Then select 'IF' in the list, name it 'Incomplete data'2 and press Finish. Selecting the IF branch component

Now drag this IF branch above your output Connector. The IF branch editor area lets you add your conditions. Editing conditions for the IF branch

Here you have the option of adding simple conditions or writing a snippet of Script – or both. Note the Match All checkbox that decides whether your Conditions (simple and scripted) are evaluated with an implied OR between them when this is unchecked, or with AND when it is selected.

Add a simple condition by pressing the Add button. Then pick the 'First' Attribute from the leftmost drop-down and then the 'has value(s)' operator. Negate this condition by toggling the not column value. Nothing needs to be specified in the right-most field when using the 'has value(s)' operator. Now add a similar has no values condition for the Attribute named 'Last' as well. Finally, make sure the Match All checkbox is unchecked – which implies Match Any – so that the lack of values for either Attribute will trigger this branch. Adding simple Conditions to the IF branch

An IF Branch diverts the execution flow of the AssemblyLine whenever the specified Conditions evaluate to true. In your case, processing will continue to those components placed under the branch if either the 'First' or 'Last' Attributes do not have values assigned to them – or even if either does not exist in the Work Entry. In other words, the 'has value(s)' operator also includes the check for 'exists'.

Now expand the IF branch and double-click on the placeholder displayed under it to insert a component here. In the Choose Component dialog, click on the radio button for Scripts, select the one called 'Script' and press Finish. Rename this Script component (also called an 'SC') to 'Write to log' and then enter the following snippet of JavaScript in it3:

task.logmsg("*** Skipping incomplete entry");

The task variable used here references the AssemblyLine itself, and it provides you with a number of useful functions like the logmsg() method. This scripted call causes the specified text message to be written to your log output.

To make log output even more informative, let's include the current contents of the Work Entry as well. Do this by right-clicking on the IF branch, selecting Add Component... and again choosing the radio button for Scripts. This time select the pre-defined Script component labeled 'Dump Work Entry'.

Finally, you must instruct the AL to stop the current cycle at this point and return control to the Iterator Connector so that it can read in the next CSV line, effectively filtering the current Entry from the XML output. Unless you specify this behavior yourself then control will continue to the first component after the IF branch. So once again you must right-click on your IF branch and then on Add Component... Choose Scripts and then select the SC called 'Exit Flow'. Now your AL should look like this: The first complete AssemblyLine

Before running the AssemblyLine again you will need to open the Null Behavior dialog once more and restore both the default definition and behavior selections – otherwise your IF Branch conditions will never evaluate to true. Resetting Null Behavior for the AssemblyLine

Now when you run the AssemblyLine again you will see your message followed by the Work Entry dump: Log output with your messages and Work Entry dump

From the statistics we can see that your IF 'Data incomplete' branch was true once, resulting in only six nodes being added to your XML output. If you again refresh the Data Browser window then you will see that 'Roger' is indeed gone.


Parent topic:

Introducing SDI

1 Or we can select the Connector itself in the AL component list; press the More... button at the top of the Input Map and choose 'Null Behavior' there.2 You were previously encouraged to name Connectors as you would a script variable. This also applies to Function components. However, it is less important for Attribute Map components, Branches, Loops and Scripts, since these are seldom accessed directly from script. In this guide higher priority has been given to making the AssemblyLine easy to read.3 The Script editor provides a feature called code-completion that shows which options you have. For example, type "tas" in the Script editor and then press the Ctrl + Space key combination to open the code-completion drop-down, which should provide a single option: task. If you press Enter then this choice is selected and entered in your script. Now type the period key (.) so your script becomes "task." and wait just a moment; You will see a new code-completion drop-down appear, this time with a list of all the methods and properties that we can access in the task object.