Announcement

Collapse
No announcement yet.

Creating PV1 segment after NK1

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Creating PV1 segment after NK1

    If there are multiple NK1 segments in a message what change is needed to code below without breaking it when there is only one NK1 segment?

    if (msg['MSH']['MSH.9']['MSH.9.2'].toString() == 'A28') if (!msg['PV1'][0])
    {
    //no PV1 then construct
    var newpv = createSegmentAfter('PV1', msg.NK1);

    newpv['PV1.3']['PV1.3.1'] = 'LMG';
    newpv['PV1.7']['PV1.7.1'] = 'LMG';
    }

    if (msg['MSH']['MSH.9']['MSH.9.2'].toString() == 'A31') if (!msg['PV1'][0])
    {
    //no PV1 then construct
    var newpv = createSegmentAfter('PV1', msg.NK1);

    }

  • #2
    I believe that this should work;
    var lastSegment = 'PV1';

    for each(nk1 in msg.NK1)
    {
    var newpv = createSegmentAfter(lastSegment, nk1);
    lastSegment = nk1;
    }

    Comment


    • #3
      This will give you the last NK1 segment, which you should be able to use in createSegmentAfter instead of msg.NK1. I can help with another solution if it doesn't work. I never use createSegmentAfter.

      Code:
      var lastNK1 = msg.NK1[msg.NK1.length()-1];
      This will throw an exception if there are no NK1 segments in the message (i.e. msg.NK1.length() == 0) On a related note, you probably want to use the length() method in a similar manner to test if there is a PV1 segment present.

      Comment


      • #4
        I will need to create the PV1 segment after the last NK1 segment if there are multiple NK1 segment.

        Comment


        • #5
          Originally posted by math1964 View Post
          I will need to create the PV1 segment after the last NK1 segment if there are multiple NK1 segment.
          Right. The code I gave you will get the last NK1 segment as long as there is 1 or more of them. If there aren't any NK1 segments it will throw an error.

          Then when you create your segment you should be able to do:
          Code:
          var newpv = createSegmentAfter('PV1', lastNK1);

          Comment


          • #6
            If there is no NK1 segment how would the code below address it?

            Comment


            • #7
              It won't. It will throw an error. You didn't specify what you want to happen if there is no NK1 segment in the message.

              Comment


              • #8
                The code will need also need to work if there is not a NK1 segment in the message.

                Comment


                • #9
                  just check for the presence of an NK1 section:
                  if (msg['NK1']) {
                  // do stuff
                  }

                  Comment


                  • #10
                    Originally posted by math1964 View Post
                    The code will need also need to work if there is not a NK1 segment in the message.
                    You haven't defined what "working" looks like if you always insert after the last NK1 segment, and there aren't any NK1 segments.

                    Originally posted by stormcel View Post
                    just check for the presence of an NK1 section:
                    That won't work. msg['NK1'] will return an empty XMLList, which is an object, which always evaluate to true. Typically you'll want to check (msg['NK1'].length() == 0) to see if none exist.

                    Comment

                    Working...
                    X