web stats
Help with iteration using getString - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 08-08-2019, 08:30 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default Help with iteration using getString

Below code works only if 1 OBR exists.
If multiple OBR's exist then you can see I am only looking at the first OBR. I manipulated the code every which way I can think of with no luck.

Plan is to loop thru each OBR.4.1 to see if panelcode exist in table.
If panelcode exist in table then create new ORC/OBR and insert each test code and testname in message.

Code:
var panelcode = msg['OBR'][0]['OBR.4']['OBR.4.1'].toString();
var testcode;
var testname;
var obr_index;

var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = '" + panelcode + "';";
var result = dbConn.executeCachedQuery(query);

if(result.next())
{
	testcode = result.getString("TESTCODE");
	testname = result.getString("TESTNAME");
	msg['OBR'][0]['OBR.4']['OBR.4.1'] = testcode;
	msg['OBR'][0]['OBR.4']['OBR.4.2'] = testname;
	while(result.next())	{
		testcode = result.getString("TESTCODE");
		testname = result.getString("TESTNAME");
		msg.insertChildBefore(null,msg['ORC'][0]);
		msg.insertChildBefore(null,msg['OBR'][0]);
		obr_index = msg['OBR'].length();
		msg['OBR'][obr_index - 1]['OBR.4']['OBR.4.1'] = testcode;
		msg['OBR'][obr_index - 1]['OBR.4']['OBR.4.2'] = testname;
	}
}
Reply With Quote
  #2  
Old 08-08-2019, 09:02 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,242
cory_cole is on a distinguished road
Default

var obr_index = 0;
var testcode = '';
var testname = '';
for each(obr1 in msg['OBR'])
{
var panelcode = obr4['OBR.4']['OBR.4.1'].toString();


var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = '" + panelcode + "';";
var result = dbConn.executeCachedQuery(query);

if(result.next())
{
testcode = result.getString("TESTCODE");
testname = result.getString("TESTNAME");
msg['OBR'][obr_index]['OBR.4']['OBR.4.1'] = testcode;
msg['OBR'][obr_index]['OBR.4']['OBR.4.2'] = testname;

obr_index++;
}
}
Reply With Quote
  #3  
Old 08-08-2019, 09:18 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

typo = for each(obr1 in msg['OBR']) - i changed to obr4 in msg['OBR'])

Code only takes the first instance from database and inserts into message.
Database contains multiple rows of same panelcode with different testcode/testname. So each testcode/testname should show in message.
Reply With Quote
  #4  
Old 08-09-2019, 04:54 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,242
cory_cole is on a distinguished road
Default

var obr_index = 0;
var testcode = '';
var testname = '';
for each(obr1 in msg['OBR'])
{
var panelcode = obr4['OBR.4']['OBR.4.1'].toString();


var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = '" + panelcode + "';";
var result = dbConn.executeCachedQuery(query);

if(result.next())
{
testcode = result.getString("TESTCODE");
testname = result.getString("TESTNAME");
}
while(result.next())
{
testcode += '~' + result.getString("TESTCODE");
testname += '~' + result.getString("TESTNAME");
}
msg['OBR'][obr_index]['OBR.4']['OBR.4.1'] = testcode;
msg['OBR'][obr_index]['OBR.4']['OBR.4.2'] =testname;

obr_index++;
}
Reply With Quote
  #5  
Old 08-09-2019, 06:59 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Original code I posted will translate successfully if only 1 ORC/OBR exist. If multiple ORC/OBR exists this is where I'm having an issue.

Here is an example of what I'm trying to do.

TABLE
PANELCODE P657 has 3 test codes (11111, 22222, 33333)
PANELCODE P1247 has 2 test codes (77777, 88888)

HL7 ORIGINAL
MSH|^~\&|||||||ORM^O01|||
PID|1|8|8|8|Test^Patient||
ORC|NW|44561||
OBR|1|44561||P657^NUMBER1 PANEL||
ORC|NW|44561||
OBR|2|44561||P1247^NUMBER2 PANEL||

HL7 AFTER
MSH|^~\&|||||||ORM^O01|||
PID|1|8|8|8|Test^Patient||
ORC|NW|44561||
OBR|1|44561||11111^TEST1||
ORC|NW|44561||
OBR|2|44561||22222^TEST2||
ORC|NW|44561||
OBR|3|44561||33333^TEST3||
ORC|NW|44561||
OBR|4|44561||77777^TEST123||
ORC|NW|44561||
OBR|5|44561||88888^TEST456||
ORC|NW|44561||

Last edited by jg12345; 08-09-2019 at 09:30 AM.
Reply With Quote
  #6  
Old 08-09-2019, 10:30 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,242
cory_cole is on a distinguished road
Default

That is a lot more complicated because you will need to renumber the OBR segments. Though the code that I have there should work. Just merge your original code into it.
Reply With Quote
  #7  
Old 08-09-2019, 11:43 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Hey Cory.. so, I'm not worried about renumbering OBR's. I did try to incorporate my original code, but it's not working as expected.

Below is what I have now and code only works if only 1 ORC/OBR exist. When another set of ORC/OBR exist, code partially works (1 set of test codes/names does not show and the original OBR with panelcode remains).

Code:
var panelcode;
var testcode;
var testname;
var obr_index;

for each(obr4 in msg['OBR'])
{
panelcode = obr4['OBR.4']['OBR.4.1'].toString();

var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = '" + panelcode + "';";
var result = dbConn.executeCachedQuery(query);

if(result.next())
{
	testcode = result.getString("TESTCODE");
	testname = result.getString("TESTNAME");
	msg['OBR'][0]['OBR.4']['OBR.4.1'] = testcode;
	msg['OBR'][0]['OBR.4']['OBR.4.2'] = testname;

}
while(result.next())
{
	testcode = result.getString("TESTCODE");
	testname = result.getString("TESTNAME");
	msg.insertChildBefore(null,msg['ORC'][0]);
	msg.insertChildBefore(null,msg['OBR'][0]);

	obr_index = msg['OBR'].length();
	msg['OBR'][obr_index-1]['OBR.4']['OBR.4.1'] = testcode;
	msg['OBR'][obr_index-1]['OBR.4']['OBR.4.2'] = testname;

	obr_index++;
}
}
Reply With Quote
  #8  
Old 08-09-2019, 10:07 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 894
agermano is on a distinguished road
Default

Code:
var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = ?;";

for (var i = msg.ORC.length() - 1; i >= 0; i--) {
    var obr = msg.child(msg.ORC[i].childIndex() + 1);
    var panelCode = obr['OBR.4']['OBR.4.1'].toString();
    var result = dbConn.executeCachedQuery(query, [panelCode]);
    if (result.size() > 0) {
        var replacementList = <></>;
        while(result.next()) {
            replacementList += msg.ORC[i];
            testcode = result.getString("TESTCODE");
            testname = result.getString("TESTNAME");
            var newObr = obr.copy();
            newObr['OBR.4']['OBR.4.1'] = testcode;
            newObr['OBR.4']['OBR.4.2'] = testname;
            replacementList += newObr;
        }
        delete obr[0];
        msg.ORC[i] = replacementList;
    }
}

for (var i = 0; i < msg.OBR.length(); i++) {
    msg.OBR[i]['OBR.1']['OBR.1.1'] = i + 1;
}
Reply With Quote
  #9  
Old 08-12-2019, 06:30 AM
jg12345 jg12345 is offline
OBX.3 Kenobi
 
Join Date: Oct 2012
Posts: 105
jg12345 is on a distinguished road
Default

Thanks agermano.. this is beautiful! Can you please assist in one more issue.
If DG1 exists below OBR, it should copy to new segment as well.
I used your code and added two lines in red below which adds only the first DG1 below the new OBR, but if more then 1 DG1 exists it does not copy the others.

Code:
var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = ?;";

for (var i = msg.ORC.length() - 1; i >= 0; i--) {
	var obr = msg.child(msg.ORC[i].childIndex() + 1);
	var dg1 = msg.child(msg.OBR[i].childIndex() + 1);
	var panelCode = obr['OBR.4']['OBR.4.1'].toString();
	var result = dbConn.executeCachedQuery(query, [panelCode]);

	if (result.size() > 0) {
		var replacementList = <></>;
		while(result.next()) {
		replacementList += msg.ORC[i];
		testcode = result.getString("TESTCODE");
		testname = result.getString("TESTNAME");
		var newObr = obr.copy();
		newObr['OBR.4']['OBR.4.1'] = testcode;
		newObr['OBR.4']['OBR.4.2'] = testname;
		replacementList += newObr;
		}
	delete obr[0];
	msg.ORC[i] = replacementList;
	insertSegmentAfter(dg1,msg.OBR[i]);
	}
}

Last edited by jg12345; 08-12-2019 at 07:18 AM.
Reply With Quote
  #10  
Old 08-12-2019, 08:23 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 894
agermano is on a distinguished road
Default

I didn't test, but this should work. You can find the code template for the getSegmentsAfter function here: https://github.com/nextgenhealthcare...ular%20Segment


Code:
var dbConn = globalMap.get('gbdbConn');
var query = "SELECT cpanel.panel, cpanel.testcode AS TESTCODE, cpanel.testname AS TESTNAME FROM cpanel WHERE cpanel.panel = ?;";

for (var i = msg.ORC.length() - 1; i >= 0; i--) {
    var orderGroup = getSegmentsAfter(msg, msg.ORC[i], /OBR|DG1/, true);
    var obr = orderGroup[0]; // Assume there will always be an OBR segment immediately following each ORC
    var panelCode = obr['OBR.4']['OBR.4.1'].toString();
    var result = dbConn.executeCachedQuery(query, [panelCode]);
    if (result.size() > 0) {
        var replacementList = <></>;
        while(result.next()) {
            replacementList += msg.ORC[i];
            testcode = result.getString("TESTCODE");
            testname = result.getString("TESTNAME");
            var newObr = obr.copy();
            newObr['OBR.4']['OBR.4.1'] = testcode;
            newObr['OBR.4']['OBR.4.2'] = testname;
            replacementList += newObr;
            // Add the rest of the order group, skipping the first segment (OBR)
            orderGroup.slice(1).forEach(function(seg) {replacementList += seg.copy()});
        }
        // Delete everything from the order group since copies have been added to the replacement list
        orderGroup.forEach(function(seg) {delete seg[0]});
        msg.ORC[i] = replacementList;
    }
}

// Renumber OBR segments
for (var i = 0; i < msg.OBR.length(); i++) {
    msg.OBR[i]['OBR.1']['OBR.1.1'] = i + 1;
}
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 11:29 AM.


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