web stats
Looping through FT1 - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 01-04-2013, 04:15 PM
cmcwhorter cmcwhorter is offline
OBX.2 Kenobi
 
Join Date: Apr 2008
Location: Texas
Posts: 55
cmcwhorter
Cool Looping through FT1

Could someone smarter than me look at my js and tell me why this code works in multiple transformers, but does not work if I setup a "for each(seg" loop?

//This works in multiple transformers,
//in each transformer "i" is set to equal a diff #: 0,1,2,3,etc
var i = 0;
var str = msg['FT1'][i]['FT1.7']['FT1.7.1'].toString();
if (str == ""){
var x = 0;
} else {
var x = str.indexOf("000");
}
msg['FT1'][i]['FT1.3']['FT1.3.1'] = x;
msg['FT1'][i]['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) {
delete msg['FT1'][i];
}

//this doesn't work, no error just deletes all FT1 segments
for each (seg in msg..FT1) {
if (seg.name().toString() == "FT1") {
var str = msg['FT1']['FT1.7']['FT1.7.1'].toString();
if (str == ""){
var x = 0;
} else {
var x = str.indexOf("000");
}
//msg['FT1']['FT1.3']['FT1.3.1'] = x;
//msg['FT1']['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) {
delete msg['FT1'];
}
}
}

//THIS ALSO DOESN'T WORK, gives undefined error
for (var i = 0;i<50;i++) {
var str = msg['FT1'][i]['FT1.7']['FT1.7.1'].toString();
if (str == ""){
var x = 0;
} else {
var x = str.indexOf("000");
}
msg['FT1'][i]['FT1.3']['FT1.3.1'] = x;
msg['FT1'][i]['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) {
delete msg['FT1'][i];
}
}

Thanks for any/all responses!
__________________
cmc
Reply With Quote
  #2  
Old 01-04-2013, 04:52 PM
narupley's Avatar
narupley narupley is offline
Mirth Employee
 
Join Date: Oct 2010
Posts: 3,529
narupley is on a distinguished road
Default

That's because in the second example, even though you're iterating through msg using a "seg" variable, you don't use seg at all... you're still using "msg['FT1']", which is not a specific segment but rather an XMLList of all the FT1 segments in the message.
__________________
Step 1: JAVA CACHE...DID YOU CLEAR

Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


- How do I foo?
- You just bar.
Reply With Quote
  #3  
Old 01-04-2013, 09:18 PM
ViSolve_Interoperability ViSolve_Interoperability is offline
OBX.2 Kenobi
 
Join Date: Sep 2012
Posts: 51
ViSolve_Interoperability is on a distinguished road
Thumbs up

Code:
//this doesn't work, no error just deletes all FT1 segments
for each (seg in msg..FT1) {
if (seg.name().toString() == "FT1") {
var str = msg['FT1']['FT1.7']['FT1.7.1'].toString();
if (str == ""){
var x = 0;
} else {
var x = str.indexOf("000");
}
//msg['FT1']['FT1.3']['FT1.3.1'] = x;
//msg['FT1']['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) {
delete msg['FT1'];
}
}
}
As naureply suggested, you should have used seg['FT1.7']['FT1.7.1'].toString() instead of msg['FT1']['FT1.7']['FT1.7.1'].toString(), because seg['FT1.7']['FT1.7.1'].toString() would point to different FT1 segments during each iteration but msg['FT1']['FT1.7']['FT1.7.1'].toString() would always point to the first FT1 segment and only if the message contains only one FT1 segment. delete msg['FT1'] will delete all the FT1 segments in your message regardless of your if condition. Instead use delete seg, which will delete the specified segment when the constraint you have set is met. Try the following.
Code:
for each (seg in msg..FT1)  //Iterates only with FT1 segments so no need to check the segment name again
 {
var str = seg['FT1.7']['FT1.7.1'].toString();
if (str == "")
{ var x = 0; }
else 
{ var x = str.indexOf("000"); }
//msg['FT1']['FT1.3']['FT1.3.1'] = x;
//msg['FT1']['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) 
delete seg;
}
Code:
//THIS ALSO DOESN'T WORK, gives undefined error
for (var i = 0;i<50;i++) {
var str = msg['FT1'][i]['FT1.7']['FT1.7.1'].toString();
if (str == ""){
var x = 0;
} else {
var x = str.indexOf("000");
}
msg['FT1'][i]['FT1.3']['FT1.3.1'] = x;
msg['FT1'][i]['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) {
delete msg['FT1'][i];
}
}
This code will work only if your message contains 50 FT1 segments else it would throw an undefined error.

So always code generically identifying the length of FT1 segments. You can try this one too..
Code:
var len=msg['FT1'].length();
for (var i = 0;i<len;i++)
{
 var str = msg['FT1'][i]['FT1.7']['FT1.7.1'].toString();
 if (str == ""){
 var x = 0;
 } else {
 var x = str.indexOf("000");
 }
 msg['FT1'][i]['FT1.3']['FT1.3.1'] = x;
 msg['FT1'][i]['FT1.2']['FT1.2.1'] = i;
 if (x !=0 ) {
 delete msg['FT1'][i];
 }
 }
__________________
ViSolve Interoperability Team,
Visolve Inc,
E-Mail: visolve_interoperability@visolve.com
Web: http://www.visolve.com

Last edited by ViSolve_Interoperability; 01-04-2013 at 09:22 PM.
Reply With Quote
  #4  
Old 01-07-2013, 06:31 AM
cmcwhorter cmcwhorter is offline
OBX.2 Kenobi
 
Join Date: Apr 2008
Location: Texas
Posts: 55
cmcwhorter
Default

This code gives the error that follows
code:
var len=msg['FT1'].length();
for (var i = 0;i<len;i++)
{
var str = msg['FT1'][i]['FT1.7']['FT1.7.1'].toString();
if (str == ""){
var x = 0;
} else {
var x = str.indexOf("000");
}
msg['FT1'][i]['FT1.3']['FT1.3.1'] = x;
msg['FT1'][i]['FT1.2']['FT1.2.1'] = i;
if (x !=0 ) {
delete msg['FT1'][i];
}
}

Error:
17: var str = msg['FT1'][i]['FT1.7']['FT1.7.1'].toString();
18: if (str == ""){
19: var x = 0;
20: } else {
21: var x = str.indexOf("000");
LINE NUMBER: 17
DETAILS: TypeError: Cannot read property "FT1.7" from undefined
__________________
cmc

Last edited by cmcwhorter; 01-07-2013 at 06:45 AM.
Reply With Quote
  #5  
Old 01-07-2013, 06:38 AM
cmcwhorter cmcwhorter is offline
OBX.2 Kenobi
 
Join Date: Apr 2008
Location: Texas
Posts: 55
cmcwhorter
Default

This code did not delete any FT1's
code:
for each (seg in msg..FT1) //Iterates only with FT1 segments so no need to check the segment name again
{
var str = seg['FT1.7']['FT1.7.1'].toString();
if (str == "")
{ var x = 0; }
else
{ var x = str.indexOf("000"); }
//msg['FT1']['FT1.3']['FT1.3.1'] = x;
//msg['FT1']['FT1.2']['FT1.2.1'] = i;
if (x !=0 )
delete seg;
}
__________________
cmc
Reply With Quote
  #6  
Old 01-07-2013, 06:42 AM
cmcwhorter cmcwhorter is offline
OBX.2 Kenobi
 
Join Date: Apr 2008
Location: Texas
Posts: 55
cmcwhorter
Default

Thank you both for your input, but it seems for now I am stuck with setting up multiple transformers instead of a loop to accomplish what I am trying to do. I still do not understand why either code does not work, why the "undefined" error when I try to loop through the FT1[i], but no error if I specify i=0, 1, 2 etc. Perhaps it is a bug in the version of Mirth I am using, Mirth Connect Administrator 2.1.0.5389
__________________
cmc

Last edited by cmcwhorter; 01-07-2013 at 06:51 AM.
Reply With Quote
  #7  
Old 01-07-2013, 07:16 AM
plew plew is offline
Mirth Newb
 
Join Date: Oct 2011
Posts: 13
plew is on a distinguished road
Default

It looks like you want to remove the FT1 if it has the string '000' in FT1.7.1
Try this instead
Code:
for each(seg in msg..FT1)
	if (seg['FT1.7']['FT1.7.1'].toString().match('000')) 
		delete seg;
Or if it always starts with '000' and you only want to delete it if it's more than 3 0s or there is an instance of 000 later in the string
Code:
for each(seg in msg..FT1)
	if (seg['FT1.7']['FT1.7.1'].toString().lastIndexOf('000') > 0) 
		delete seg;

Last edited by plew; 01-07-2013 at 07:20 AM.
Reply With Quote
  #8  
Old 01-07-2013, 07:25 AM
cmcwhorter cmcwhorter is offline
OBX.2 Kenobi
 
Join Date: Apr 2008
Location: Texas
Posts: 55
cmcwhorter
Default

I have two scenarios, but I was just trying to achieve the first.
1-If the first 3 characters are not '000' then delete the FT1.
2-If the first 3 characters are '000' then delete the FT1.

I figured if I could get a loop working for one scenario, the other would be easy.
__________________
cmc
Reply With Quote
  #9  
Old 01-07-2013, 07:42 AM
cmcwhorter cmcwhorter is offline
OBX.2 Kenobi
 
Join Date: Apr 2008
Location: Texas
Posts: 55
cmcwhorter
Default

This code did not delete any FT1's
Code:
for each(seg in msg..FT1)
if (seg['FT1.7']['FT1.7.1'].toString().match('000'))
delete seg;

Example of ft1 hl7 data:
FT1|0|||20130102125525|20130103100413|CG|3601519|F empop Unil Stent/Ath w/PTA Lt.super Fem Art|1|1
FT1|1|||20130102125525|20130103100413|CG|3230002|A bd Aorta /w Bilat IlioFem Runoff|1|1
FT1|2|||20130102125525|20130103100413|CG|0008549|M edrad Syringe|1|1
FT1|3|||20130102125525|20130103100413|CG|0007695|G lidewire Standard .035/260cm Angled/Stiff|1|1
FT1|4|||20130102125525|20130103100413|CG|0008429|M anifold Kit|1|1
FT1|5|||20130102125525|20130103100413|CG|0008587|T ransducer|1|1
FT1|6|||20130102125525|20130103100413|CG|0007683|E ngage 5Fr 12cm Sheath|1|1
FT1|7|||20130102125525|20130103100413|CG|0007690|S tiff Micropuncture Set NAVALYST|1|1
FT1|8|||20130102125525|20130103100413|CG|0007691|. 035-145 3mm J Guidewire|1|1
FT1|9|||20130102125525|20130103100413|CG|0007321|C ath Pack/Cardiac|1|1
FT1|10|||20130102125525|20130103100413|CG|0004701| Bag-o-Jet|1|1
FT1|11|||20130102125525|20130103100413|CG|0008668| 60 inch pressure tubing|1|1
FT1|12|||20130102125525|20130103100413|CG||30'' Contrast Tubing|1|1
FT1|13|||20130102125525|20130103100413|CG|0004857| Chloraprep 26ml Orange|1|1
FT1|14|||20130102125525|20130103100413|CG|0007711| Chloraprep 10.5 ml teal|1|1
FT1|15|||20130102125525|20130103100413|CG|0008742| C02 Detector|1|1
FT1|16|||20130102125525|20130103100413|CG|0008724| Banded Bag - Equipment Cover|1|1
FT1|17|||20130102125525|20130103100413|CG|0007685| Engage 7Fr 12cm Sheath|1|1
FT1|18|||20130102125525|20130103100413|CG|0008672| Steering Handle - Green|1|1
FT1|19|||20130102125525|20130103100413|CG|0008542| Inflation Device Kit|1|1
FT1|20|||20130102125525|20130103100413|CG|0007643| HT FLOPPY J 300cm|1|1
FT1|21|||20130102125525|20130103100413|CG|0007658| HI-TORQUE PILOT 150 300cm|1|1
FT1|22|||20130102125525|20130103100413|CG|0007643| HT FLOPPY J 300cm|1|1
FT1|23|||20130102125525|20130103100413|CG|0008687| 4FR OMNI FLUSH CATH|1|1
FT1|24|||20130102125525|20130103100413|CG|0008431| Glow and Tell Tape|1|1
FT1|25|||20130102125525|20130103100413|CG|0008431| Glow and Tell Tape|1|1
FT1|26|||20130102125525|20130103100413|CG|0008607| 7FR/55cm/RAABE Guiding Sheath/Straight|1|1
FT1|27|||20130102125525|20130103100413|CG|0008568| SPider FX 5mm Filter 320cm|1|1
FT1|28|||20130102125525|20130103100413|CG|0007546| ViperSlide Lubricant 100ml bags|1|1
FT1|29|||20130102125525|20130103100413|CG|0007545| VIPERWIRE ADV .014|1|1
FT1|30|||20130102125525|20130103100413|CG|0007539| Stealth Catheter 2.0 solid|1|1
FT1|31|||20130102125525|20130103100413|CG|0008604| Lifestent 6x200x130cm|1|1
FT1|32|||20130102125525|20130103100413|CG|0008860| POWERFLEX PRO .035 6X150|1|1
FT1|33|||20130102125525|20130103100413|CG|0008151| LifeStent 6x80x130cm|1|1
__________________
cmc
Reply With Quote
  #10  
Old 01-07-2013, 07:54 AM
plew plew is offline
Mirth Newb
 
Join Date: Oct 2011
Posts: 13
plew is on a distinguished road
Default

If you're sending tmp and not msg you may try adding this after that loop:
tmp.FT1=msg.FT1

or you could do:
for each (seg in tmp..FT1)
instead of
for each (seg in msg..FT1)
Reply With Quote
Reply

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 -7. The time now is 08:59 PM.


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