BizTalk–Looping through repeating message nodes in orchestrations

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:

Print | posted @ Wednesday, August 15, 2012 3:10 PM

Comments on this entry:

Gravatar # re: BizTalk–Looping through repeating message nodes in orchestrations
by Sarah K at 10/18/2012 8:54 AM

Worked well for me, thanks
Gravatar # re: BizTalk–Looping through repeating message nodes in orchestrations
by andyM at 4/14/2014 4:48 PM

Great post. Thank you
Gravatar # re: BizTalk–Looping through repeating message nodes in orchestrations
by babul reddy at 4/7/2015 10:42 PM

use these its working fine:

nRecordCount = (System.Int32)xpath(Msg_Input, "count(/*[local-name()='PaymentBankEOD' and namespace-uri()='http://Agility.BizTalk.PaymentGateway.FF_EODPayments']/*[local-name()='Details' and namespace-uri()='http://Agility.BizTalk.PaymentGateway.FF_EODPayments'])");
Gravatar # re: BizTalk–Looping through repeating message nodes in orchestrations
by vasu at 4/22/2015 11:08 PM

i am new to biztalk can any one suggest me how to learn biztalk or any websites for detailed procedures for biztalk
Gravatar # re: BizTalk–Looping through repeating message nodes in orchestrations
by Anand at 5/19/2015 3:16 AM

Hi all,
Am getting issue, here we are calling wcf by passing a value, we got response from wcf service , but at a time we are getting one record but bundle of records there, we want store those records into database. for this we took loop to get the total records and every time incremented the loop value.
the problem is where we have to store all records in loop . how to resolve pls help me
Post A Comment