This project has moved. For the latest updates, please go here.

Error when creating a 835 file

Jan 31 at 7:12 PM
I am currently working on a project that involves creating a x12 835 file. I experienced an issue when trying to add the segment N1 on the loop 1000 A:

//header code (message object)... etc
.
.
.
var transaction = group.AddTransaction("835", "0021");
.
.
.
var loop1000AStart = transaction.AddLoop(new TypedLoopN1());_

Is throwing the next error:

__"Loop N1* could not be added because it could not be found in the specification for ST"__

The problem is in the class 'LoopContainer.cs' in this control sentence:

if (matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId).Count() == 1)
                {
                    spec = matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId).First();
                }
The 835 specification involves N1 segment for loop1000A and 1000B, but in this condition we are forcing to retreieve only if the list contains one specification.

I fixed this adding the next code to the method 'private LoopSpecification GetLoopSpecification(string segmentString)' in LoopContainer.cs

private LoopSpecification GetLoopSpecification(string segmentString)
    {
        Segment segment = new Segment(this, _delimiters, segmentString);

        IList<LoopSpecification> matchingLoopSpecs = ((LoopContainer)this).AllowedChildLoops
                    .Where(cl => cl.StartingSegment.SegmentId == segment.SegmentId).ToList();

        if (matchingLoopSpecs == null || matchingLoopSpecs.Count == 0)
        {
            return null;
        }
        else if (segment.SegmentId == "NM1" || segment.SegmentId == "N1")
        {
            LoopSpecification spec = matchingLoopSpecs.Where(ls => ls.StartingSegment.EntityIdentifiers.Any(ei => ei.Code.ToString() == segment.GetElement(1) || ei.Code.ToString() == "Item" + segment.GetElement(1))).FirstOrDefault();
            if (spec == null)
            {
                if (matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId).Count() == 1)
                {
                    spec = matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId).First();
                }
                /*Added code Start*/
                else
                {
                    if (matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId).Count() >= 1 && ((Transaction)segment.Parent).IdentifierCode == "835" && segment.SegmentId == "N1")
                    {
                        if (this.Loops.Count() == 0)
                            spec = matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId && ls.LoopId == "1000A").First();
                        else
                            spec = matchingLoopSpecs.Where(ls => ls.StartingSegment.SegmentId == segment.SegmentId && ls.LoopId == "1000B").First();
                    }
                }
                /*Added code End*/
            }
            return spec;
        }
        else
        {
            return matchingLoopSpecs.FirstOrDefault();
        }
    }
I will appreciate you validate my solution or in better case, if you have any other more efficient solution please let me know it.

Regards.