Announcement

Collapse
No announcement yet.

Loop through the same segment in a mirth filter

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

  • Loop through the same segment in a mirth filter

    Hi All,

    I'm trying to add a filter at source to loop through each custom ZDC segment and if any ZDC segment does not include the value 'GP' in ZDC-7-1 then stop processing the message.



    Code:
    for each (var ZDCrep in msg.ZDC)
    	{
    	var ZDCStatus= ZDCrep['ZDC.7']['ZDC.7.1'].toString().toUpperCase();
    	if (ZDCStatus!= 'GP') {
    		return true;
    	}
    }
    return false;
    Any idea where im going wrong, the message just fires through to the next transformer step.

  • #2
    I think you have your true and false values reversed if I understand what you're trying to do. When a filter returns true it means to accept the message, and false means to reject it.

    Comment


    • #3
      Hi Agermano,

      Yes that was a typo, its now running through fine but its not a robust enough solution.

      If GP (ZDC-7-1) is contained in the first repetition of the ZDC segment the message is sent through. However, there maybe multiple ZDC segments where GP is contained in a latter segment. If GP is contained in any one of the multiple ZDC segments then i want it to continue onto the destination. Any idea why its not checking all ZDC segments?

      Nacks the below message:
      MSH|^~\&|||||20190320155728.4147||MDM^T02^MDM_T02| ||2.4
      TXA||^Clinic||20190306094500|013661^^^^^^^^^CONLT| 20190320155630|||Admin^SOL Administrator|||1|a75e7a9b-666e-4774-abcf-297b2b2cae31|O005206014||||U|||||Main|1
      OBX|1|ED|||^^Pdf^Base64^|||N
      ZDC|1|ID^^^Assign^GP|^^^^||||PT
      ZDC|1|ID^^^Assign^GP|^^^^||||NK
      ZDC|1|ID^^^Assign^GP|^^^^||||GP

      Comment


      • #4
        Mark,
        The reason is as soon as it hits that first 'PT' it executes the return true and leaves the filter. Try the code below. Basically it sets a variable to where it should send by default. Then if it is ever = 'GP' it sets it to false. The final if/else should correctly filter the message.


        var sendMessage = "true";

        for each (var ZDCrep in msg.ZDC)
        {
        var ZDCStatus= ZDCrep['ZDC.7']['ZDC.7.1'].toString().toUpperCase();
        if (ZDCStatus== 'GP') {
        sendMessage = "false";
        }
        }

        if (sendMessage == 'true')
        {
        return true;
        }
        else
        {
        return false;
        }

        if (sendMessage == 'true')
        {
        return true;
        }
        else
        {
        return false;
        }

        Comment


        • #5
          Hi Beshler, thanks for putting me on the right path. I made a slight amendment and it now works, code below. cheers


          Code:
          var sendMessage = "false";
          
          for each (var ZDCrep in msg.ZDC)
          {
          var ZDCStatus= ZDCrep['ZDC.7']['ZDC.7.1'].toString().toUpperCase();
          if (ZDCStatus== 'GP') {
          sendMessage = "true";
          }
          }
          
          if (sendMessage == 'true')
          {
          return true;
          }
          else
          {
          return false;
          }

          Comment


          • #6
            When you said "if any ZDC segment does not include the value 'GP' in ZDC-7-1 then stop processing the message" I thought that meant that they all had to have the value GP to continue. Switching your true and false values would have done that.

            If you want to continue processing if any of the segments have a value of GP then all you needed to do was change
            Code:
            	if (ZDCStatus!= 'GP') {
            to
            Code:
            	if (ZDCStatus== 'GP') {
            in your original code.

            There's no reason to use strings for "true" and "false." That's what booleans are designed for. If you wanted to use a flag and single return statement, it should likely look like this:

            Code:
            var sendMessage = false;
            
            for each (var ZDCrep in msg.ZDC)
            {
                var ZDCStatus= ZDCrep['ZDC.7']['ZDC.7.1'].toString().toUpperCase();
                if (ZDCStatus == 'GP') {
                   sendMessage = true;
                   break; // no need to check the rest after you've found one. This is similar to what happens when a return is used here.
                }
            }
            
            return sendMessage;

            Comment

            Working...
            X