Announcement

Collapse
No announcement yet.

Deleting subfields

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

  • Deleting subfields

    I have a need to remove all of the subfields past the 9th one in the OBR-28 segment.

    Tried this but it doesn't work. It says the length of OBR-28 == 1 but there are move subfields that that

    Code:
    logger.info('obr ' + msg['OBR'].length().toString());
    logger.info('28 length: ' + msg['OBR'][0]['OBR.28'].length().toString());
    
    for (var i = msg['OBR'].length()-1;i>0;i++)
    {
         for (var j = msg.children()[i]['OBR.28'].length() -1; j>9;j--)
         {
     
              delete msg.children()[i]['OBR.28'][j];
         }
    }
    message contains 1 obr :

    OBR|1|725885585^HNAM_ORDERID|850612^EPIC_Filler_Or der_Id|006127^Lytes|||20210331101552|||||||2021033 1101500|&Blood|||||000002021090000005^HNA_ACCN~111 86305^HNA_ACCNID||20210331101710|||C||1^^^20210331 101300^^S|1740387760^VOINESCU^CATALINA^GENTIANA^^M D^MD^^NPI^Personnel^^^External Identifier

    I need to remove everything past the OBR-28.9 (after 'NPI')


  • #2
    I don't think you can delete segments like that. Why don't you just specifically set those segments to empty spaces if you need to always 'remove' them.

    Comment


    • #3
      I won't always know how many I actually have

      Comment


      • #4
        There is only 1 OBR.28, because it is not repeating. You want to delete components (children) of OBR.28.

        This should work
        Code:
        for each (var obr28 in msg.OBR['OBR.28']) {
            var first9 = new XMLList();
            for (var i = 1; i <= 9 ; i++) {
                first9 += obr28['OBR.28.'+i];
            }
            obr28.setChildren(first9);
        }
        I accessed the children by name rather than index because it is possible that there are gaps depending on how the message was created. A node that doesn't exist will be omitted from the final result as well (resulting in fewer than 9 children, but still the output that you need.)
        Last edited by agermano; 04-06-2021, 01:49 PM.

        Comment


        • #5
          This effectively does the same thing if you just want to list them individually rather than building the list in a loop.

          Code:
          for each (var obr28 in msg.OBR['OBR.28']) {
              obr28.setChildren(<>
                  {obr28['OBR.28.1']}
                  {obr28['OBR.28.2']}
                  {obr28['OBR.28.3']}
                  {obr28['OBR.28.4']}
                  {obr28['OBR.28.5']}
                  {obr28['OBR.28.6']}
                  {obr28['OBR.28.7']}
                  {obr28['OBR.28.8']}
                  {obr28['OBR.28.9']}
              </>);
          }

          Comment


          • #6
            The below might be an option also:
            Code:
            function trimField(segmentField, endComponent) {
              var component = segmentField.name() + '.'
              for (var idx = segmentField.children().length(); idx > endComponent; idx--)
                delete segmentField[component + idx]
            }
            trimField(msg.OBR['OBR.28'], 9)
            Just include the indexes and it should scale for repeating segments/fields.
            Last edited by jkrebs; 04-06-2021, 07:19 PM.

            Comment

            Working...
            X