web stats
insertChildAfter do not look to work as i expect - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 06-20-2019, 08:02 AM
guiguidusud guiguidusud is offline
What's HL7?
 
Join Date: Jun 2019
Posts: 3
guiguidusud is on a distinguished road
Default insertChildAfter do not look to work as i expect

Hi
I have multiple HL7 like this

SPM||190106068302||Test|||||||P|||Quantitative|
SAC|||||||||||6|
OBR|22|||V6B|||20190301102600|||||||||||||Released WithoutSignature|Accepted||||F||||||||||
OBX|1|ST|V6B^^^Status||TargetNotDetected||||||F||| ||||0618313|
SID|artus HHV-6_ni RD72 UDT ID647|3110QK1804|
NTE|||ConcentrationIsTooLow|RE|


I'm using insertChildAfter function as this :
for each (obx in msg.OBX){
...
design testadd2 consisting on:
copy original OBX
modify this OBX and create 2 new OBX from it
find SID and NTE if they do exist
combine all in testadd2 variable leading to build for each OBX
OBX
SID
NTE
OBX
SID
NTE


...

msg.insertChildAfter(obx, testadd2)
}
this do work but it insert all content between original OBX and original SID/NTE . I would like to improve it and insert testadd2 below last SID NTE if they exist .

I manage to use

var sgmtsid = getSegmentsAfter(msg, obx, 'SID', true, 'SPM')
var SIDChInd = sgmtsid[0].childIndex();

to get the exact index where i want to insert testadd2 below but this does not work :

msg.insertChildAfter(msg.child(SIDChInd ), testadd2) => error is function not defined = no action in my message .

Is there a way to insert at specific index ?
Reply With Quote
  #2  
Old 06-20-2019, 04:17 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 1,027
agermano is on a distinguished road
Default

The following code worked for me. The main thing is that insertChildAfter is looking for a specific xml object. The child() method returns an XMLList, and this is one of the few cases where there is a difference between a single element and a list containing a single element.

Note that I changed how you were calling getSegmentsAfter to pick up the SID and NTE segments at the same time.

I also built newSegs the way I did so that it was easy to modify the two OBX child elements after it was created.


Code:
for each (var obx in msg.OBX) {
    var segs = getSegmentsAfter(msg, obx, /SID|NTE/, true);
    var lastSeg = segs.length > 0 ? segs[segs.length-1] : obx;
    
    var newSegs = <newSegs/>;
    
    for (var i = 0; i < 2; i++) {
        newSegs.appendChild(obx.copy());
        segs.forEach(function(seg) {newSegs.appendChild(seg)});
    }
    
    newSegs.OBX[0]['OBX.1']['OBX.1.1'] = 2;
    newSegs.OBX[1]['OBX.1']['OBX.1.1'] = 3;
    
    msg.insertChildAfter(msg.child(lastSeg.childIndex())[0], newSegs.*);
    // this would also work
    // msg.insertChildAfter(msg.children()[lastSeg.childIndex()], newSegs.*);
}
Reply With Quote
  #3  
Old 06-20-2019, 11:08 PM
guiguidusud guiguidusud is offline
What's HL7?
 
Join Date: Jun 2019
Posts: 3
guiguidusud is on a distinguished road
Default

Quote:
Originally Posted by agermano View Post
The following code worked for me. The main thing is that insertChildAfter is looking for a specific xml object. The child() method returns an XMLList, and this is one of the few cases where there is a difference between a single element and a list containing a single element.

Note that I changed how you were calling getSegmentsAfter to pick up the SID and NTE segments at the same time.

I also built newSegs the way I did so that it was easy to modify the two OBX child elements after it was created.


Code:
for each (var obx in msg.OBX) {
    var segs = getSegmentsAfter(msg, obx, /SID|NTE/, true);
    var lastSeg = segs.length > 0 ? segs[segs.length-1] : obx;
    
    var newSegs = <newSegs/>;
    
    for (var i = 0; i < 2; i++) {
        newSegs.appendChild(obx.copy());
        segs.forEach(function(seg) {newSegs.appendChild(seg)});
    }
    
    newSegs.OBX[0]['OBX.1']['OBX.1.1'] = 2;
    newSegs.OBX[1]['OBX.1']['OBX.1.1'] = 3;
    
    msg.insertChildAfter(msg.child(lastSeg.childIndex())[0], newSegs.*);
    // this would also work
    // msg.insertChildAfter(msg.children()[lastSeg.childIndex()], newSegs.*);
}
Whaouuu ! sometimes you meet with master Jedi !
I modify your code to match my needs and after 5 min it woks with less lines after days of unseccesfull try .

I would need to translate "var lastSeg = segs.length > 0 ? segs[segs.length-1] : obx;"
to something like: if (segs content exist ) {create sgs[ size]} but i didn't get "bx" .

Same for segs.forEach(function(seg) {newSegs.appendChild(seg)});
it is advanced notation my padawan level do not get yet :s

but it works perfectly so big up thanks for you ! and may the force keep being with you Master
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:19 PM.


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