web stats
Loop through the same segment in a mirth filter - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-23-2019, 07:44 AM
Mark Sull Mark Sull is offline
Mirth Newb
 
Join Date: Apr 2018
Location: Ireland
Posts: 24
Mark Sull is on a distinguished road
Default 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.
Reply With Quote
  #2  
Old 05-23-2019, 10:10 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 806
agermano is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 05-24-2019, 05:06 AM
Mark Sull Mark Sull is offline
Mirth Newb
 
Join Date: Apr 2018
Location: Ireland
Posts: 24
Mark Sull is on a distinguished road
Default

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:
Quote:
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
Reply With Quote
  #4  
Old 05-24-2019, 05:36 AM
bhesler bhesler is offline
Mirth Newb
 
Join Date: Jul 2015
Posts: 11
bhesler is on a distinguished road
Default

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;
}
Reply With Quote
  #5  
Old 05-24-2019, 06:50 AM
Mark Sull Mark Sull is offline
Mirth Newb
 
Join Date: Apr 2018
Location: Ireland
Posts: 24
Mark Sull is on a distinguished road
Default

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;
}
Reply With Quote
  #6  
Old 05-29-2019, 10:35 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 806
agermano is on a distinguished road
Default

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;
Reply With Quote
Reply

Tags
loop segments repeating

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 08:42 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation