Say that you have an incoming BizTalk message with many repeating nodes, and you want to process each one the same. The easiest thing to do would be to use the envelope to split the single message into multiple messages, then have multiple instances of your orchestration handle each one (similar to how the HIPAA “multiple” message schemas work). But what if you need to do some work after every individual message has been processed? I’ll show you how to use the Loop functoid along with xpath queries to loop through the message.
In my case, I’m receiving a flat file where each line is a response to a transaction we have submitted. After mapping the flat file to xml, I get something like this:
I want to insert each Record into the database, then do some work once they’ve all been inserted.
The first step is to create the single Record version of the message. It’ll be basically the same as the incoming Response message, just without the Response node at the top:
The schema needs to be created without a Target Namespace; you’ll see why later.
Next, create the orchestration, two messages:
- FullResponse (using the full flat file schema)
- SingleResponse (using the new single Record schema)
…and three variables:
- counter - int
- counterString – string
- responseCount - int
Drag various shapes onto your orchestration until you wind up with something that look like this:
I’ll start at the top and detail how each shape should be configured.
Receive – receives FullResponse message
Expression - here you’re going to use an xpath query to count how many Record nodes are in the FullResponse message:
responseCount = xpath(InstaMedResponse, "count(/*[local-name()='Response' and namespace-uri()='http://MyProject.Response_FF']/*[local-name()='Record' and namespace-uri()=''])");
counter = 1;
counterString = "1";
(Pay attention to the namespace, yours may be different)
Loop - what’s the loop condition?
counter <= responseCount
Message Assignment – here we’re grabbing a single Record node and assigning it to the SingleResponse message:
SingleResponse = xpath(InstaMedResponse, "/*[local-name()='Response' and namespace-uri()='http://AnciPay.InstaMedResponse_FF']/*[local-name()='Record' and namespace-uri()=''][" + counterString + "]");
Remember how I said the new schema needs to be created without a target namespace? Your xpath query will return the Record node without a namespace. Also, we have to (unfortunately) use counterString rather than just counter.ToString(), otherwise BizTalk will complain about an xpath error.
Map – this one is optional for the demo; in my case I’m mapping it to the Request Side of a generated schema.
Expression – here we need to increment the counter:
counter = counter + 1;
counterString = counter.ToString();
Send & Receive - I’m using a WCF port to save to a database (click here for a simple tutorial), but you could omit these steps if you want.
Expression – this can really be anything that you want to happen after every individual Record node has been processed – run a stored procedure, send email, whatever.
Compile, deploy and test. If you just drop the SingleResponse message to a send port (instead of the database), you should see your incoming message split into multiple messages.
Technorati Tags: BizTalk