BizTalk EDI 856 Mapping

I have been doing a fair amount of EDI mapping on my latest gig, and I
wanted to share a neat trick I figured out for mapping 856 Advanced
Shipping Notice
transactions. What makes an 856 special is that all of
the action takes place in one looping segment that contains a number of
child segments. The fun part is that you need to use a different subset
of those child segments in each loop iteration. For example, the first
iteration you need to map data to the PRF, DTM and N1 segments. The
second and third iterations map data to the LIN, SN1 and PID segments.
The final iteration maps data to the TD1, TD5 and SAC segments. Anybody
who has used the BizTalk mapper realizes that to map something like this
you can not just drag a link from the source schema to the destination
schema in the designer.

My solution to this problem came to me when I figured out that you can
declare variables inside of one scripting functoid, and manipulate them
from within another scripting functoid. I use two scripting functoids
along with a looping functoid to control the number of loops that are
created in the destination instance. The first scripting functoid
contains the following C# code:

int _counter = 1;
public string Increment()
_counter += 1; return "";

This code declares the loop counter, and increments it for each loop
iteration. The second scripting functoid contains this code:

public int GetCounter()
return _counter;

This code returns the current value of the loop counter to the other
mapping elements. Now you might be wondering why I did not combine the
code into a single functoid. That is what I initially tried to do, but I
discovered that for every output link, the scripting functoid evaluates
its code. This causes all kinds of strange things to happen inside of
the map. By separating the code into two functoids, I can control when
the counter gets incremented.

To finish up our 856 map, we combine the loop counter with a set of
logical functoids that will determine which nodes get mapped during each
loop iteration. The logical functoids can be used to test for a specific
loop iteration, or used to test for a certain range within the loop
iterations. By dragging output links from the logical functoids to the
segments that need to be mapped for the respective iteration, the
segments will be created when the functoid evaluates to true and will be
suppressed when the functoid evaluates to false. From there you can map
the rest of the data using whatever combination of links and functoids
works best. In the end you end up with a map that looks something like
the image below. (Click on the image for a bigger version.)

Do you have questions? A better way of solving this problem? Please
share it by leaving a comment.