Click here to Skip to main content
15,881,715 members
Articles / Productivity Apps and Services / Biztalk
Technical Blog

BizTalk - Looping through repeating message nodes in orchestrations

Rate me:
Please Sign up or sign in to vote.
4.50/5 (2 votes)
28 Jan 2013CPOL2 min read 37.1K   1   5
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:

<Response>      
   <Record>       
      <RecordId>xxx</RecordId>       
      <SubmitterId>xxx</SubmitterId>       
      <Status>xxx</Status>       
   </Record>       
   <Record>       
      <RecordId>xxx</RecordId>       
      <SubmitterId>xxx</SubmitterId>       
      <Status>xxx</Status>       
   </Record>       
</Response>

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:

<Record>      
   <RecordId>xxx</RecordId>       
   <SubmitterId>xxx</SubmitterId>       
   <Status>xxx</Status>       
</Record>

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:

LoopingPost

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 c<code>ounterString 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, 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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBizTalk Loop and process each node in xml Pin
bicobielich15-Aug-18 10:04
bicobielich15-Aug-18 10:04 
QuestionGood Article Pin
ClarenceD22-May-15 10:03
professionalClarenceD22-May-15 10:03 
GeneralExcellent Article Helped Me a Lot Pin
Praveen P N20-Nov-14 21:55
Praveen P N20-Nov-14 21:55 
GeneralCannot view the screenshot Pin
kunal_g18-Jul-13 19:43
kunal_g18-Jul-13 19:43 
GeneralRe: Cannot view the screenshot Pin
BillOsuch19-Jul-13 3:55
BillOsuch19-Jul-13 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.