Introduction

To accept/process an EDI X12 file, EDI Receive Pipeline is used and in it the actual data (message -- Single ST to SE) are extracted after detaching envelope part (header and trailer) and added to message context. But,  ISA06, ISA08, ISA15, GS01, GS02, GS03, GS08, ST03 and ST0 are the only properties which are promoted, whereas ISA_Segment and GS_Segment are written, rest properties are not promoted due to performance reason.

This article intends to show how to fetch the ISA and GS segment values using helper class and mapping those to the destination structure.

Prerequisites

Scenario

EDI X12 850 files are received and data from it has to be mapped to the destination structure, also Interchange Control and Functional Group Header values. Destination structure can be XML, Flat file or schema generated from database.

In this article destination structure is XML.

Input


EDI X12 messages are wrapped with envelope, at header it has ISA segment (Interchange control header), GS (Functional group) and ST (Transaction Set) and at the trailer it has IEA, GE and SE. Enveloping segments work in pairs. ISA-IEA represents an interchange. GS-GE is a functional group inside of the interchange and ST-SE is a transaction inside the group.

Many times there is required to pass or store the values from ISA (Interchange Control Header) and GS (Functional group) segments. But this is not part of the data and not all properties from Envelope are promoted or written in the context of the message. Segments have qualifier and its value (Identifier) which is separated by element separator.

In the input sample shown below "*" is the element separator and "~" is the segment seperator.

                                                                                                                     Figure 1: Input sample

How To Do

Step 1: Create BizTalk Project


  • Add EDI X12 850 schema (EDI schema is shipped in as a self-extracting executable and when extracted, schemas can be found at drive:\Program Files\Microsoft BizTalk Server 2010\XSD_Schema\EDI).
  • Add/Create Output schema XMLOrder as shown below.

 Figure 2: Target structure (XMLOrder Schema)

Step 2: Helper class which will split the segment and return individual value


  • Create a C# class library project and add code given in "Code block 1".
  • Sign and Build the project.
  • Add it to GAC.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
 
namespace SegmentValueFetcher
{
    public class FetchSegment
    {
        public static string fetchvalue(string segstr, int value)
        {
            char elemsptr;
            elemsptr = segstr[3];
            if (segstr.Substring(0,2) == "GS")
            {
                elemsptr = segstr[2];
            }
            var lst = segstr.Split(elemsptr);
            return lst[value];
        }
    }
}
                                                                                                   Code block 1:  Code in helper class (FetchSegment)

Step 3: Context Schema which will hold the ISA and GS segment 

  • Add a schema to project (EDIContext).
  • Add two elements of type string (ISASegment and GSSegment).
  • Make both the elements as Distinguished field.

 Figure 3: Context Message structure (EDIContext Schema)


Step 4: Add reference to two assemblies

  • SegmentValueFetcher.dll (Helper class) : This will help in using the method to split the segment and return individual values.
  • Microsoft.BizTalk.Edi.BaseArtifacts.dll  : This will help in accessing the EDIContext properties in Orchestration.
Note: SegmentValueFetcher is created as an independent project so that it can be used with other projects wherever required.


Step 5: Orchestration to receive EDI 850 message and sent an XML order having all values mapped along with envelope segment values

  • Add three messages 
    1. EDImsg with Message Type based on EDI X12 850 schema
    2. Contextmsg  with Message Type based on EDIContext schema
    3. XMLmsg with Message Type based on XMLOrder schema
  • Add a variable
    1. xmlDoc of Type System.Xml.XmlDocument
  • Bind Receive shape to EDImsg
  • Drop a Construct Shape 
  • Add Message Assignment shape followed by Transform shape in the construct shape (Two messages are constructed using single Construct shape)
  • And set its Messages Constructed property as XMLmsg;Contextmsg
  • In Message Assignment Shape add following code
xmlDoc.LoadXml("<ns0:Context xmlns:ns0='http://demoedi.edicontext/'><ISASegment>ISASegment_0</ISASegment><GSSegment>GSSegment_0</GSSegment></ns0:Context>");
Contextmsg = xmlDoc;
  • Above added code constructs a message of type Contextmsg by loading the instance of the EDIContext schema into xmlDoc variable and assigning it to Contextmsg.
  • Whole ISA and GS segment  are written in context by EDIReceive pipeline and are available in orchestration with the help of Microsoft.BizTalk.Edi.BaseArtifacts assembly.
  • This values are assigned to the Contextmsg elements (distinguished fields)
Contextmsg.ISASegment = EDImsg(EDI.ISA_Segment);
Contextmsg.GSSegment = EDImsg(EDI.GS_Segment);

  • In Transform Shape,  two source are used 
    1. EDImsg (Input received)
    2. ContextMsg (Constructed in preceding Message Assignment shape)
  • Destination is XMLmsg
  • Configure as shown below



 Figure 4:  Transform shape configuration
  • Map the data from EDI 850 schema to XMLOrder schema accordingly (Header,Details and Items)
  • Map the elements under Interchange node

 Figure 5: ISA segment mapping
  • Map the elements under Functional node

    Figure 6: GS segment mapping

  • Scripting functoid is used to call an external assembly which accepts a string (which is splitted) and an integer value (to choose splitted value based on it) and returns the value from splitted list.
  • Below is configuration of first Scripting functoid (linked to ISA01 element of target schema), it is provided with ISASegment as first input and the second input is "1" as ISA01 value is to be fetched. And this inputs is passed to fetchvalue method in FetchSegment class which is part of  SegmentValueFetcher assembly (This assembly is created Step 2 and is available here as it is added in Reference)

 Figure 7: Scripting Functoid configuration
  • Same way, for all the individual values Scripting functoid is configured
  • Bind Send Shape to XMLmsg


 Figure 8: Orchestration (FetchData)

Output

Output can be in two variations depending upon selection made in Agreement-->Local Host Settings--> Inbound Message Processing Option 


 Figure 9: Mask Security Information 

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Order xmlns:ns0="http://DemoEDI.XMLOrder">
<Header>
  <OrderId>42222001</OrderId>
  <Currency>USD</Currency>
  <BillTo>
     <Name>DemoPartner1 International</Name>
     <AdressLine1>Development</AdressLine1>
      <AddressLine2>PO Box 2158</AddressLine2>
      <City>BillTo City</City>
      <State>NJ</State>
      <PostalCode>07096</PostalCode>
      <Country>US</Country>
  </BillTo>
  <ShipTo>
      <Name>DemoPartner1 INC</Name>
      <AddressLine1>Development</AddressLine1>
      <AddressLine2>Po Box 820O</AddressLine2>
      <City>ShipTo City</City>
      <State>CA</State>
      <PostalCode>930128557</PostalCode>
      <Country>US</Country>
  </ShipTo>
</Header>
<Details>
  <Item>
     <ItemId>28-9323-60</ItemId>
     <ItemDescription>C</ItemDescription>
     <Quantity>2</Quantity>
     <UnitPrice>107.1</UnitPrice>
     <LineNumber>00001</LineNumber>
     <UOM>PK</UOM>
   </Item>
   <Item>
      <ItemId>28-9322-25</ItemId>
      <ItemDescription>C</ItemDescription>
      <Quantity>3</Quantity>
      <UnitPrice>82.82</UnitPrice>
      <LineNumber>00002</LineNumber>
      <UOM>PK</UOM>
   </Item>
 </Details>
 <Interchange>
   <ISA01>00</ISA01>
   <ISA02> </ISA02>
   <ISA03>00</ISA03>
   <ISA04> </ISA04>
   <ISA05>ZZ</ISA05>
   <ISA06>Partner1 </ISA06>
   <ISA07>ZZ</ISA07>
   <ISA08>Partner2 </ISA08>
   <ISA09>130903</ISA09>
   <ISA10>1705</ISA10>
   <ISA11>U</ISA11>
   <ISA12>00503</ISA12>
   <ISA13>000141425</ISA13>
   <ISA14>0</ISA14>
   <ISA15>T</ISA15>
 </Interchange>
<Functional>
  <GS01>PO</GS01>
  <GS02>BTS-SENDER</GS02>
  <GS03>RECEIVE-APP</GS03>
  <GS04>20130903</GS04>
  <GS05>1856</GS05>
  <GS06>31300</GS06>
  <GS07>X</GS07>
  <GS08>00503</GS08>
</Functional>
</ns0:Order>
                                                 Code Block 2: Output when Mask Security option is unchecked


<?xml version="1.0" encoding="UTF-8"?>
<ns0:Order xmlns:ns0="http://DemoEDI.XMLOrder">
<Header>
  <OrderId>42222001</OrderId>
  <Currency>USD</Currency>
  <BillTo>
     <Name>DemoPartner1 International</Name>
     <AdressLine1>Development</AdressLine1>
      <AddressLine2>PO Box 2158</AddressLine2>
      <City>BillTo City</City>
      <State>NJ</State>
      <PostalCode>07096</PostalCode>
      <Country>US</Country>
  </BillTo>
  <ShipTo>
      <Name>DemoPartner1 INC</Name>
      <AddressLine1>Development</AddressLine1>
      <AddressLine2>Po Box 820O</AddressLine2>
      <City>ShipTo City</City>
      <State>CA</State>
      <PostalCode>930128557</PostalCode>
      <Country>US</Country>
  </ShipTo>
</Header>
<Details>
  <Item>
     <ItemId>28-9323-60</ItemId>
     <ItemDescription>C</ItemDescription>
     <Quantity>2</Quantity>
     <UnitPrice>107.1</UnitPrice>
     <LineNumber>00001</LineNumber>
     <UOM>PK</UOM>
   </Item>
   <Item>
      <ItemId>28-9322-25</ItemId>
      <ItemDescription>C</ItemDescription>
      <Quantity>3</Quantity>
      <UnitPrice>82.82</UnitPrice>
      <LineNumber>00002</LineNumber>
      <UOM>PK</UOM>
   </Item>
 </Details>
 <Interchange>
   <ISA01>##</ISA01>
   <ISA02>########## </ISA02>
   <ISA03>##</ISA03>
   <ISA04>########## </ISA04>
   <ISA05>ZZ</ISA05>
   <ISA06>Partner1 </ISA06>
   <ISA07>ZZ</ISA07>
   <ISA08>Partner2 </ISA08>
   <ISA09>130903</ISA09>
   <ISA10>1705</ISA10>
   <ISA11>U</ISA11>
   <ISA12>00503</ISA12>
   <ISA13>000141425</ISA13>
   <ISA14>0</ISA14>
   <ISA15>T</ISA15>
 </Interchange>
<Functional>
  <GS01>PO</GS01>
  <GS02>BTS-SENDER</GS02>
  <GS03>RECEIVE-APP</GS03>
  <GS04>20130903</GS04>
  <GS05>1856</GS05>
  <GS06>31300</GS06>
  <GS07>X</GS07>
  <GS08>00503</GS08>
</Functional>
</ns0:Order>
                                                 Code Block 3: Output when Mask Security option is checked

Download Sample

You can find the source code belonging to this article on MSDN code Gallery at :

Author

Maheshkumar S Tiwari|User Page
http://tech-findings.blogspot.com/

See Also

Another important place to find an extensive amount of BizTalk related articles is the TechNet Wiki itself. The best entry point is BizTalk Server Resources on the TechNet Wiki.  

This article participates in the 
TechNet Guru for November competition.