Convert Strings to Numbers in a Functoid

Convert Strings to Numbers in a Functoid

As shown in my previous posts, I have been tackling some messy maps in Visual Studio for our BizTalk solution. I inherited some confusing and broken source code, and am just now getting it all to work!

One of the issues I was having was mapping both the begin dates and the end dates of a record, by using the cumulative minimum or the cumulative maximum of dates. The source system sends dates as a string, in the YYYYMMDD format. When I tried to take the cumulative max or min, I would consistently get no data. I could map without the cumulative functoids, but that does not follow our business rules.

Question: What was causing my cumulative max/min functoids to blow up?

Cause: Cumulative maximum and cumulative minimum functoids only consistently work with numeric values, and not strings. See: http://msdn.microsoft.com/en-us/library/aa561894(v=bts.10)

Solution: Custom string-to-numeric scripting functoid.

Scripting_Convert_Numeric

Here’s how it works. The scripting functoid gets the date from the source xml, converts it to a number, and then the cumulative max/min functoid works like a charm. Once the date is parsed via the string extract functoids, and reformatted in the string concatenate functoid, it is back in its string format. Here is the code in my scripting functoid:

public Int32 ConvertDatetoInt(string param1)
        {
                return System.Convert.ToInt32(param1);
        }

Pretty simple, quite nifty.

Creating Complicated Maps in Visual Studio

Perhaps you have a rather large task of creating a complicated map with countless functioids. Or, like me, you are inheriting horrifyingly messy maps from another person. On my first ever BizTalk deployment project, I inherited this:
BeforeTabs

One page, 150+ nodes, countless functoids. Now let’s make this point clear: this is my first ever BizTalk project. Other than doing MSDN’s EAI and EDI tutorials, a few articles, and hours in a conference room doing knowledge transfer with a coworker, my knowledge is limited to my google search proficiency.

The developer before me left our company about a month ago, without much to hand off besides broken code and missing documentation. As many of you know, this is actually not very uncommon. Because of this, let’s discuss a few ways you can make your (and your replacement’s) life much more stress-free:

1: Document Everything

This should be a given. No matter how smart you are, you will make mistakes. So why take the chance? Break out Excel, go through each element, know your source system, know your destination system, and document what needs to happen in-between.

2: Use your Documentation

If you document your mappings appropriately, you will have two major advantages. You will have a consistent playbook to reference throughout your development, and you will have a binding document to assure data consistency from the source and destination systems. The latter advantage is the one most don’t think about; however, when you get accusations about incorrect data during testing, you can reference your document, and be on your way.

3: Map on Multiple Pages

See how on the bottom of the window in Visual Studio, where it says “Page 1, Page 2…”? That means you can use multiple pages for a single map. This probably sounds patronizing, but I have already seen quite a few prod maps with a large headache on one single page. Below is an image of the same map, but with one tree split out to another page.

AfterTabs

4: Label your Functoids

Okay, at this point, you may think I am being a bit too anal. Who actually takes the time to label their functoids, anyway? To be honest, I am probably one of the most impatient developers you will meet. Documentation is painful, and writing pretty code was a forced adaptation of mine. So labeling functoids is just one extra step that can seem too meticulous. However, it is a lifesaver. When you look at a map in Visual Studio, you cannot see every bit of logic. You are missing the order of operations and the specific operations taking place.

For example, the String Concatenate Functoid:

Concatenate

From this menu, can you tell what is taking place? When multiple functoids lead into a string concatenate functoid, there is absolutely no way to know which input is Input[0], Input[1], or Input[2]. However, if you label the functoids leading into the String Concatenate Functoid, you can see exactly what operations are taking place.

Concatenate_Labeled

This very situation saved my rear when dealing with date formatting, and I have labeled my functoids ever since.

5: Don’t Rush

There is always stress to get a project done faster, but remember: a bit more time spent up front can save hours of frustration in the future. We all know that debugging is not the easiest thing to do in BizTalk, so let’s minimize those occurrences.

So grab some headphones, put on your favorite tunes, and spend some time with these maps. Use your documentation, think through the logic, and have fun!

Generate XSLT from BizTalk Map

Generate XSLT from BizTalk Map

Some programmers are much more comfortable reading XSLT over looking through the confusing webs of BizTalk maps. I found this article very helpful in helping generate to that XSLT in Visual Studio.

Open the Map in Visual Studio > Right click on the map to convert > Click on “Validate Map” > Control-click on the file location in the Output window.

After you validate a map, the XSLT should be available in your local app data. You can find the files at C:\Users\[username]\AppData\Local\Temp\_MapData

Quick, easy solution.

Maps can get messy