web stats
Iteration from ~ to new line - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 06-19-2019, 06:48 PM
Golfmachine Golfmachine is offline
What's HL7?
 
Join Date: Jun 2019
Posts: 2
Golfmachine is on a distinguished road
Default Iteration from ~ to new line

Hi! I'm fairly new to Mirth but have been able to use it quite successfully. I currently have an OBR line as such:

OBR|1|LB00000022||FE^Fe(TIBC)~FECAL^FECALYSIS~URIN ES^URINALYSIS (HMO)~CBCP^CBC PLATELET|R|20190614142600||||||||||^|OPD^|LB|OPD^| 3654335~3654336~3654337~3654338||||||||||

I plan on making it so instead of ~ separating the various OBR4 and OBR20, they will make new OBRs like so:
OBR|1|LB00000022||FE^Fe(TIBC)|R|20190614142600|||| ||||||^|OPD^|LB|OPD^|3654335||||||||||
OBR|2|LB00000022||FECAL^FECALYSIS|R|20190614142600 ||||||||||^|OPD^|LB|OPD^|3654336||||||||||
OBR|3|LB00000022||URINES^URINALYSIS (HMO)|R|20190614142600||||||||||^|OPD^|LB|OPD^|365 4337||||||||||
OBR|4|LB00000022||CBCP^CBC PLATELET|R|20190614142600||||||||||^|OPD^|LB|OPD^| 3654338||||||||||

I'm sure this should be a fairly easy thing to do but I am finding it hard to look for a javascript that will help iterate this. Scoured the forums and all I was able to find was a code similar to this:

for each (obr4 in msg.PID['OBR.4']) {
logger.info(obr4['OBR.4.1'].toString());
logger.info(obr4['OBR.4.2'].toString());
}

Not sure where to go from there honestly.

Thank you all in advance!

P.S. I may also need to figure out how to make OBR1 change numbers depending on line number. By that I mean line 1 its value = 1, line 2 value =2, etc. It's actually not as big a priority as iterating ~ to new lines though hehe
Reply With Quote
  #2  
Old 06-20-2019, 06:43 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,228
cory_cole is on a distinguished road
Default

First you need to store OBR.2 and OBR.3

var OBR2 = msg['OBR']['OBR.2'].toString();
var OBR3 = msg['OBR']['OBR.3'].toString();

Then you need a counter

var OBRCount = 0;

then...

for each(obr4 in msg['OBR']['OBR.4'])
{
msg['OBR'][OBRCount++]['OBR.4'] = obr4.toString();
}
Reply With Quote
  #3  
Old 06-20-2019, 01:26 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 802
agermano is on a distinguished road
Default

This assumes there won't be more than 1 OBR segment in a message to begin with. If that's a possibility it will have to be adjusted.

Code:
// store original OBR
var obr = msg.OBR;

// get number of repetitions in OBR-4. Assume same number of repetitions in OBR-20
var numRepetitions = obr['OBR.4'].length();

// nothing to do if not at least 2 repetitions
if (numRepetitions > 1) {
    // create new empty XMLList to hold results
    var obrList = <></>;
    for (var i = 0; i < numRepetitions; i++) {
        // copy original OBR segment
        var newOBR = obr.copy();
        
        // update setID
        newOBR['OBR.1']['OBR.1.1'] = i + 1;
        
        // replace repeating OBR fields with value for current iteration
        newOBR['OBR.4'] = newOBR['OBR.4'][i];
        newOBR['OBR.20'] = newOBR['OBR.20'][i];
        
        // add to result list
        obrList += newOBR;
    }
    
    // replace original obr with new list
    msg.OBR = obrList;
}
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 -8. The time now is 07:09 AM.


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