EDIFACT UNB and UNG Segment Fetcher Helper Class

{tocify} $title={Table of Contents}

Introduction


Many times there is requirement to pass or store the values from UNB(Interchange Control Header) and UNG (Functional group) segments. This helper class can be called from map and individual values can be fetched.


How To Do


To create helper class do following:

1. Open Visual Studio
2. Add new C# library project
3. Add below code


Code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EDIFACTSegmentValueFetcher

{
    public class SegmentValueFetcher
    {
        public string fetchvalue(string segment, string elemsptr, string subelemsptr, int pos, int subpos)
        {
            string subsegment;
            char esptr = elemsptr[0];
            char sesptr = subelemsptr[0];

            var lst = segment.Split(esptr);

            subsegment = lst[pos];
            if (subsegment.Contains(subelemsptr))
            {
                var sublst = subsegment.Split(sesptr);
                return sublst[subpos - 1].Replace("'", "");
            }
            else
            {
                return lst[pos].Replace("'","");
            }
        }
    }
}

4. Sign the project
5. Add it to GAC


Add to Gac


How It Works


EDIFACT  messages are wrapped with envelope, at header it has UNB segment (Interchange control header - Mandatory), UNG (Functional group - Conditional) and UNH (Transaction Set) and at the trailer it has UNZ, UNE and UNT. Enveloping segments work in pairs, UNB-UNZ represents an interchange, UNG-UNE is a functional group inside of the interchange and UNH-UNT is a transaction inside the group.


Many times there is required to pass or store the values from UNB (Interchange Control Header) and UNG (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 data elements which are separated by element separator, also sub data elements which are separated by sub-element separator.

This helper class helps in fetching the individual segment and subsegment values. You need to provide the whole segment, element separator, subelement separator and the number of field you want and number of subfield.

Example(Header segments of INVOIC):


UNB+UNOA:1+BTS-SENDER:ZZZ+RECEIVE-PARTNER:ZZZ+141024:2231+201410242231'

UNG+INVOIC+BTS-SENDER+RECEIVE-PARTNER+141024:2231+201410242231+UN+D:96A'

Suppose from above sample,
1. If UNG7.1 value is to be fetched, in that case UNG segment, element separator, subelement separator, field value as "7" and subfield value as "1" will be provided as input and the output will be "D"
2. If UNG7.2 value is to be fetched, in that case UNG segment, element separator, subelement separator, field value as "7" and subfield value as "2" will be provided as input and the output will be "96A"


Post a Comment

If you have any suggestions or questions or want to share something then please drop a comment

Previous Post Next Post