Testing for Empty/Nil/Missing Source Nodes in BizTalk Maps
November 24, 2014 Leave a comment
Re-posted from another blog – original publish date 27 Aug 2012
Scenario: You are mapping an optional node in your source schema to an optional node in your target schema. But… the target schema has stricter validation rules and cannot accept empty or “nil” nodes, whereas the source schema can.
By default, if the source node doesn’t exist, it won’t be output in your target. No problems there. But… what if the source node is empty or null?
Let’s look at this mapping example:
Here’s a source instance:
What will be output? You might be dismayed to find out it is this:
Both the nil and the empty source nodes output empty target nodes, which will be invalid for the target system. The only way to stop the empty nodes from being created is to map a “false” value from a logical functoid to the node. So if you’re a thorough programmer like myself, you might be tempted to do something like this:
This will work great, covering every possible scenario for the NillableField1:
- Field does not exist
- Field is marked nil
- Field is an empty string (e.g. size == 0).
But…six functoids!! And that’s just for the one node mapping – you’d have to repeat this group for every other optional node, including the ZeroLengthField1 node.
Unless… one day you happen to discover these innocuous looking functoids sitting in the Logical section of your toolbox:
Looking at the documentation for the “Logical String” functoid reveals this:
Use the Logical String functoid to return the value “true” if the input parameter is a string.
While not immediately obvious, the fact is that this single functoid performs the same tests that the entire group above does. If the source string node is either nil or empty, it returns false. As expected, the Logical Date and Logical Numeric functoids work in a similar way, respective to their particular data types.
So now your map can look like this:
… and still produce the desired output when using the instance above:
I’ll be covering items like this in my first Pluralsight course, “Mapping in BizTalk 2013”, which I’m tirelessly working on at the moment (hence the reason for so few blog posts lately). It will hopefully be published before the end of this year.
UPDATE: The Pluralsight course is now live and can be accessed here!