Tuesday, November 29, 2011

How to create a null destination node in a BizTalk Map

This is the second time recently I came across this requirement so I guess others need it too. This post describes how to set a destintation node to null based upon a condition.

My situation was the source node contained a DateTime which was either valid or set to a zero length string. As some of you will know setting a date to a zero length string will actually set the date to 0001-01-01.  That is the date when the world began, allegedly.  My destination node is a date field too so I want to ignore the invalid date.  But here's the thing - I need to set the target node to null because I need the node present in the output.

The solution is to use a logical operator (= or <>) and a value mapping functoid.  In my case I am using a Script functoid to convert the source node value from DateTime to Date.  So the Script output is a date string.  By the way if you are wondering why I just don't test for this in the Script and set the return parameter to null, its becasue this doesn't work!
The <> functoid checks if the output is 0001-01-01 and sends true or false to the Value Mapping Functoid. As you can see I added 0001-01-01 as a constant for the second parameter.  Remember this is a Not Equal operator so it will return true if its a valid date and false if it is 0001-01-01. 

The magic happens in the Value Mapping functoid.  The description explains what it does. If the value of the first input parameter is true then the value of the second input parameter is returned.  It should add if the first input parameter is false, then the destination node is set to null
So all you have to do is select the correct logical operator (either = or <>) so that its output is true if you want to pass the value to the destination and false when you want to set it to null.  But be sure you have the parameters the right way round.  The logical operator needs to be at the top, the value of the source node is below.

Update 24/04/2012: I also ran into a problem where the source node was a Record, that did not contain data but held other elements.  I found that for the child elements the trick I used above worked well but, the parent element always appeared in the output as  < ParentNode />

I struggled for a long while to get rid of the offending  < ParentNode /> but in the end found that simply using the Logical Existence (?) functoid on the ParentNode was enough.  The result was the ParentNode was completely absent from the output except when any of the child elements contained data.

2 comments:

Unknown said...

Charles, many thanks for blogging this. Had an issue where the parent node was being created as an empty tag when all children were null, you're last little nugget about the logical existence functoid saved me a lot of time and a headache.

Sailendra said...

Charles, this blog helped me in 2020. Very much thankful for posting this.

Regards,
Sailendra :)