› Clovertech Forums › Cloverleaf › TCL Error
Hi,
I am novice tcl programmer.
Need help with the tcl error below:
proc tpsMissingMRN { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_3 [hl7::get_field hl7 MSH.3]
set MSH_5 [hl7::get_field hl7 MSH.5.0]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3_0 [hl7::get_field hl7 PID.3]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
#echo “MSH_3: $MSH_3”
#echo “MSH_5: $MSH_5”
#echo “PID_3_0: $PID_3_0”
#echo “PID_3.5: $PID_3_5”
# Handle the condition when PID.3.0 is empty and PID.3.5 is not equal to MR
#if {$PID_3_0 eq “” && $PID_3_5 ne “MR”} {
# set errormsg “Missing PID-3 with PID-3.5 not equal to MR.”
# } else {
# echo “PID segment not found.”
# }
# Initialize counter outside the loop
set pid_counter 0
#Increment counter inside the loop
for { set i 1 } { $i <= [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
#Grab the PID-3 iteration where PID-3.5= “MR” and Process PID.3 data using $pid_counter
if { [hl7::get_field hl7 PID.3($i).5] eq “MR” } {
set mrn [hl7::get_field hl7 PID.3($i).1]
}
}
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}
#Create the e-mail
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
set errorMsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3_1”
#echo “This is errorMsg: |$errorMsg|”
set emailbody $errorMsg
if {$errormsg eq “”} {
echo “Killing message with Message Control ID: |$MSH_10| because of $errorMsg”
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
#Continue Message
lappend dispList “KILL $mh”
} else {
#Continue Message
lappend dispList “CONTINUE $mh”
}
}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
return $dispList
}
}
I have not used the HL7 package and I’m notso great with namespaces, but maybe this line:
set hl7 [hl7::parse_msg [msgget $mh]]
it just makes me wonder if it is confusing that you are creating/updating a variable that has the same name as the namespace?
Maybe you use a different variable name:
set hl7msg [hl7::parse_msg [msgget $mh]]
and then change the lines that come after that to use the different variable name:
set MSH_3 [hl7::get_field hl7msg MSH.3]
Peter Heggie
What it’s complaining about is that the “level” variable is not set when running…
lindex { segment segrep field fieldrep component subcomponent } $level
… to select one of the six values from the list {segment segrep field fieldrep component subcomponent }. If level is zero, lindex returns “segment”. If level is 3, lindex returns “fieldrep”.
This is the command that’s generating the error:
set MSH_5 [hl7::get_field hl7 MSH.5.0]
Assuming it’s not a bug in the hl7 package code itself, the problem is most likely in how message element is specified – “MSH.5.0”. Perhaps the zero element is the issue.
Try “MSH.5.1” instead. If that doesn’t work, try eliminating the component altogether – using “MSH.5” to see if it retrieves the entire contents of MSH-5.
Good luck!
Jeff Dinsmore
Chesapeake Regional Healthcare
To go along with Jeff’s suggestion, I might also wonder if the get_field proc/command needs another argument to say what level you want(whole field, particular component, particular repetition).
Also, I’m thinking that it should be $hl7 in the call to get_field, not hl7. You want to pass the content of the variable, not the name of it.
Also, I’m wondering about the calls to get PID-3 and PID-3.5. Is that just to echo out their values during debugging? Otherwise, you’re looping through the repetitions of that field later on to find the MR, so the initial calls are unnecessary.
HTH,
TIM
Tim Pancost
Trinity Health
Tim
I think that passing the variable by name is correct since the command prior to the one generating the error would probably have barfed if it was sent a string (hl7) and it was expecting the value in that string.
Passing the name and then referencing it with upvar in the called procedure is fine – and the only way to do it if the passed variable is an array.
Jeff Dinsmore
Chesapeake Regional Healthcare
Thank you all for your suggestions.
My script is not erroring anymore after I edited however it is not giving the desired results, unsure why?
This is my test message:
MSH|^~\&||COCLB|||202406041326||ORM^O01|ORDERS-OUT.12.2431267|P|2.4|||AL|NE| PID|1||X1234785^^^^PI~123456^^^^MR||Test^Testing^^^^^L||19760101|M|||||.^PRN|||||E2911111| PV1|1|O|ECARD^^^^^^LBH Cardiology||||PAAAAA^Pllll^Suuuu^^^^^^^^^^XX|||||||||||CLI||U|||||||||||||||||||COCLB||CAN|||202408071500| ORC|NW|||||N|||202405211446|E.CAR.JJ||PAAAAA^Pllll^Suuuu||||||HCAD03167.1| OBR|1|CARD20240730-0002COCLB||CARD^EXECG^Exercise ECG||202407301430||||||||||PAAAAA^Pllll^Suuuu|||||||||||^^^^^R| OBX|1|TX|CVIS-TSTRS^Reason for Test?||.| ZMN||
I have attached the edited script and and seeing the below in the testing tool?
I get the same output whether I put the MRN or not?
How can I echo , 123456^^^^MR in my script?
I would expect the message to be killed only if my PID looks like this
PID|1||X1234785^^^^PI~^^^^MR
Is there something wrong with my PID iteration?
***********************************
Testing tool results:
I am here
MSH_3:
MSH_10: ORDERS-OUT.12.2431267
PID_3_1: X1234785
PID_3.5: PI
Killing message with Message Control ID: |ORDERS-OUT.12.2431267| because of This is the message ID: ORDERS-OUT.12.2431267
This is the reason why:
Patient X Number: (PID.3) X1234785
KILL: ‘MSH|^~\&||COCLB|||202406041326||ORM^O01|ORDERS-OUT.12.2431267|P|2.4|||AL|NE|
PID|1||X1234785^^^^PI~123456^^^^MR||Test^Testing^^^^^L||19760101|M|||||.^PRN|||||E2911111|
PV1|1|O|ECARD^^^^^^LBH Cardiology||||PAAAAA^Pllll^Suuuu^^^^^^^^^^XX|||||||||||CLI||U|||||||||||||||||||COCLB||CAN|||202408071500|
ORC|NW|||||N|||202405211446|E.CAR.JJ||PAAAAA^Pllll^Suuuu||||||HCAD03167.1|
OBR|1|CARD20240730-0002COCLB||CARD^EXECG^Exercise ECG||202407301430||||||||||PAAAAA^Pllll^Suuuu|||||||||||^^^^^R|
OBX|1|TX|CVIS-TSTRS^Reason for Test?||.|
ZMN||
‘
<No active handles>
Checking for leaked handles…
Long story short, it’s killing it because you told it to kill it. 😀
After going through the repetitions of PID-3 to (hopefully) find the MRN, you set a variable, errormsg to “”. Then it looks like you’re building an email, regardless if you’re going to be sending it or not. You then check if errormsg is “”, which it will always be, as you never change it after initializing it. And inside that if statement, you send the e-mail and kill the message, although your comment says to continue it.
I would think that somewhere you would need to check if the mrn variable is blank, and use that to determine if errormsg should be populated with something. Then, if it is populated, build and send the e-mail and kill the message, otherwise continue the message. So something along the lines of:
if {$mrn eq “”} {
set errormsg “This message has no MRN identifier included.”
} else {
set errormsg “”
}
Then:
if {$errormsg eq “”} {
lappend dispList “CONTINUE $mh”
} else {
…build/send e-mail section
lappend dispList “KILL $mh”
}
Or something to that effect.
HTH,
TIM
Tim Pancost
Trinity Health
Thanks Tim,
Let me revisit it.
I am not sure what I am doing wrong here:
Testing Tool output
Please Wait ……
Command Issued: hcitpstest -r run -x UTF-8 -L -f nl -c sms_ib_data -e “hcitpstestshowbydisp ” /cloverleaf/cis19.1/integrator/site_cvis/TestMessages/NO_MR_ORDERS-OUT.12.2431267.nl “tpsMissingMRN”
Command output:
I am here
MSH_3:
MSH_10: ORDERS-OUT.12.2431267
PID_3: X1234785^^^^PI~123456^^^^MR
PID_3_1: X1234785
PID_3.5: PI
[0:TEST] ‘0’ (returned by ‘tpsMissingMRN ‘) does not match { <key> <value> }
Checking for leaked handles…
Handle Allocated by
====== ============
message0 <global>
****************************************
Script:
######################################################################
# Name: tpsMissingMRN
# Purpose: This will pick up relevant details from the ADT and ORM message from Meditech and send an e-mail
###########################################################
proc tpsMissingMRN { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
echo “I am here”
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_3 [hl7::get_field hl7 MSH.3]
set MSH_5 [hl7::get_field hl7 MSH.5]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3 [hl7::get_field hl7 PID.3]
set PID_3_1 [hl7::get_field hl7 PID.3.1]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
echo “MSH_3: $MSH_3”
echo “MSH_10: $MSH_10”
echo “PID_3: $PID_3”
echo “PID_3_1: $PID_3_1”
echo “PID_3.5: $PID_3_5”
set found_mrn “”
for { set i 1 } { $i < [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
set mrn [hl7::get_field hl7 PID.3($i).1]
if { [string equal $mrn “”] eq 1} {
lappend dispList “CONTINUE $mh”
}
set found_mrn $mrn
}
if {$found_mrn eq “”} {
# Now errormsg is accessible here
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
} else {
lappend dispList “KILL $mh”
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
#echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3_1”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
return $dispList
}
}
Check what’s returned from your hl7::count command – [hl7::count hl7 PID.3.1]
I expect that’s counting the number of repeats in PID-3.1 and likely returning 1 rather than the number of repeats in all of PID-3.
If that’s the case, it would never get to the repeat with the “MR”
Try [hl7::count hl7 PID.3] instead
Jeff Dinsmore
Chesapeake Regional Healthcare
Thanks Jeff,
I change that too (as per your comment).
for { set i 1 } { $i < [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
if { [hl7::get_field hl7 PID.3($i).5] eq “MR” } {
set mrn [hl7::get_field hl7 PID.3($i).1]
}
}
Plus what Tim said…
If you only want to kill if there’s a missing MRN, you’ll need to put a condition in there to check the MRN – once you’ve found it – for validity.
You’ll also want to set mrn to “” prior to your loop so that the variable exists in case you don’t find a repeat that contains “MR”.
Once you have your variable, you can use it to choose whether or not to kill.
Something like
if { $mrn eq “” } {
puts “mrn is empty”
# kill message here
} else {
puts “mrn is not empty”
}
Also – a style pointer…
Using two variables that are spelled the same, but have different capitalization is confusing. Distinguishing between errormsg and errorMsg is harder than it needs to be.
Make it easy on yourself and whoever will maintain this after you and use something like “errormsg” and “emailErrorMsg”.
Jeff Dinsmore
Chesapeake Regional Healthcare
I am not sure what I am doing wrong here:
Testing Tool output
Please Wait ……
Command Issued: hcitpstest -r run -x UTF-8 -L -f nl -c sms_ib_data -e “hcitpstestshowbydisp ” /cloverleaf/cis19.1/integrator/site_cvis/TestMessages/NO_MR_ORDERS-OUT.12.2431267.nl “tpsMissingMRN”
Command output:
I am here
MSH_3:
MSH_10: ORDERS-OUT.12.2431267
PID_3: X1234785^^^^PI~123456^^^^MR
PID_3_1: X1234785
PID_3.5: PI
[0:TEST] ‘0’ (returned by ‘tpsMissingMRN ‘) does not match { <key> <value> }
Checking for leaked handles…
Handle Allocated by
====== ============
message0 <global>
****************************************
Script:
######################################################################
# Name: tpsMissingMRN
# Purpose: This will pick up relevant details from the ADT and ORM message from Meditech and send an e-mail
###########################################################
proc tpsMissingMRN { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
echo “I am here”
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_3 [hl7::get_field hl7 MSH.3]
set MSH_5 [hl7::get_field hl7 MSH.5]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3 [hl7::get_field hl7 PID.3]
set PID_3_1 [hl7::get_field hl7 PID.3.1]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
echo “MSH_3: $MSH_3”
echo “MSH_10: $MSH_10”
echo “PID_3: $PID_3”
echo “PID_3_1: $PID_3_1”
echo “PID_3.5: $PID_3_5”
set found_mrn “”
for { set i 1 } { $i < [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
set mrn [hl7::get_field hl7 PID.3($i).1]
if { [string equal $mrn “”] eq 1} {
lappend dispList “CONTINUE $mh”
}
set found_mrn $mrn
}
if {$found_mrn eq “”} {
# Now errormsg is accessible here
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
} else {
lappend dispList “KILL $mh”
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
#echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3_1”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
return $dispList
}
}
First thing I see is that the “return $dispList” is inside your switch, but not within one of the switch cases. I expect that would cause a Tcl error that would kill the script.
Are you wanting this script to email an alert and kill the message if it doesn’t have an MR in PID-3 and continue the message if you do find an MR?
Please confirm what you want it to do.
Jeff Dinsmore
Chesapeake Regional Healthcare
As it stands, if an MRN type id is found, you’re not updating dispList, so you have a message leak.
Looks like somewhere along the way you lost the check if the id retrieved is the MRN. The section:
set mrn [hl7::get_field hl7 PID.3($i).1]
if { [string equal $mrn “”] eq 1} {
lappend dispList “CONTINUE $mh”
}
set found_mrn $mrn
Should be more like:
if { [hl7::get_field hl7 PID.3.($1).5] eq “MR”} {
set found_mrn [hl7::get_field hl7 PID.3($i).1]
lappend dispList “CONTINUE $mh”
}
And then, later on, remove the “} else {” before the KILL, it’s not supposed to be there:
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
} else {
lappend dispList “KILL $mh”
Should be more like:
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
One more thing, your close brace after your sourceconn check should be after sending the email. As it stands, it’s always going to try to send an email, missing MRN or not:
if {$sourceconn eq “”} {
set sourceconn TestTool
}}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
#echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3_1”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
}
Should be more like:
if {$sourceconn eq “”} {
set sourceconn TestTool
}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
#echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3_1”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
}}
HTH,
TIM
Tim Pancost
Trinity Health
HI Tim,
Thank you, edited but still erroring?
*****************************************************************
Script:
######################################################################
# Name: tpsMissingMRN
# Purpose: This will pick up relevant details from the ADT and ORM message from Meditech and send an e-mail
###########################################################
proc tpsMissingMRN { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_3 [hl7::get_field hl7 MSH.3]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3 [hl7::get_field hl7 PID.3]
set PID_3_1 [hl7::get_field hl7 PID.3.1]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
echo “MSH_3: $MSH_3”
echo “MSH_10: $MSH_10”
echo “PID_3: $PID_3”
set pid_counter =0
set found_mrn “”
for { set i 1 } { $i < [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
if { [hl7::get_field hl7 PID.3.($1).5] eq “MR”} {
set found_mrn [hl7::get_field hl7 PID.3($i).1]
lappend dispList “CONTINUE $mh”
} else {
if {[$found_mrn eq “”]}
# Send an error message
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
}
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
#echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
}}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
return $dispList
}
}
Hi Tim ,
Please IGNORE the above,
I have added a new error below:
Error:
**********************************
SCRIPT:
######################################################################
# Name: tpsMissingMRN
# Purpose: This will pick up relevant details from the ADT and ORM message from Meditech and send an e-mail
# to people decodefined for Missing PID-3 in table Missing_MRN_Email.tbl
#############################################################
proc tpsMissingMRN { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3 [hl7::get_field hl7 PID.3]
set PID_3_1 [hl7::get_field hl7 PID.3.1]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
echo “MSH_10: $MSH_10”
echo “PID_3: $PID_3”
set pid_counter 0
set found_mrn “”
for { set i 1 } { $i < [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
if { [hl7::get_field hl7 PID.3.($1).5] eq “MR”} {
set found_mrn [hl7::get_field hl7 PID.3($i).1]
lappend dispList “CONTINUE $mh”
echo found mrn $found_mrn
} else {
if {[$found_mrn eq “”]}
# Send an error message
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
}
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
#echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
}}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
return $dispList
}
}
Shouldn’t
if { [hl7::get_field hl7 PID.3.($1).5] eq “MR”} {
Be
if { [hl7::get_field hl7 PID.3.($i).5] eq “MR”} {
With that said I don’t understand the repeated use of the hl7 package when it is so easy to simply split the message into segments and then into fields to get what you need. The package is not very ergonomic since with each call it needs to parse the message.
I guess to each his own 🙂
Finally works now!
Many thanks to everyone.
If anybody has any TCL with Cloverleaf training guide or good tcl example re-usable scripts, please let me know.
######################################################################
# Name: tpsMissingMRN
# Purpose: This will pick up relevant details from the ADT and ORM message from Meditech and send an e-mail
#############################################################
proc tpsMissingMRN { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3 [hl7::get_field hl7 PID.3]
set PID_3_1 [hl7::get_field hl7 PID.3.1]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
echo “MSH_10: $MSH_10”
echo “PID_3: $PID_3”
set pid_counter 0
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}
for { set i 1 } { $i <= [hl7::count hl7 PID.3] } { incr i } {
echo Hi entering IF statement
if { [hl7::get_field hl7 PID.3($i).5] eq “MR”} {
echo pid_counter before $pid_counter
incr pid_counter
echo pid_counter after $pid_counter
}
}
if {$pid_counter eq 0} {
# Send an error message
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
#set subject “$mailsubjectprefix Cloverleaf – Patient X Number: $PID_3_1 – THH message from $MSH_5 i.e. thread $sourceconn with message ID $MSH_10 needs manual intervention!”
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
#set errormsg “This is the message ID: $MSH_10 \nThis is the reason why:\n\n$errormsg \n Patient X Number: $PID_3_1 \n Minor Patient ID: $MRG_1_1 \n The Major UHB patient ID is: $PID_4_1 \n The minor UHB Patient ID is: $mrgid\n\n Please check that the UHB ID matches the correct HCA Patient ID.”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n Patient X Number: (PID.3) $PID_3”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
} else {
lappend dispList “CONTINUE $mh”
}
}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
}
return $dispList
}
You don’t need this loop:
for { set i 1 } { $i <= [hl7::count hl7 PID.3] } { incr i } {
With the library you can do this:
set mrn [hl7::get_field hl7 PID.3(5=MR).1]
Thanks David,
I will try it.
Hi David,
I replaced
for { set i 1 } { $i <= [hl7::count hl7 PID.3] } { incr i } {
with your suggested line,
set mrn [hl7::get_field hl7 PID.3(5=MR).1]
but it errored?
Try replacing all these lines:
set pid_counter 0
set found_mrn “”
for { set i 1 } { $i < [hl7::count hl7 PID.3] } { incr i } {
incr pid_counter
if { [hl7::get_field hl7 PID.3.($1).5] eq “MR”} {
set found_mrn [hl7::get_field hl7 PID.3($i).1]
lappend dispList “CONTINUE $mh”
echo found mrn $found_mrn
} else {
if {[$found_mrn eq “”]}
# Send an error message
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
}
with this:
set found_mrn [hl7::get_field hl7 PID.3(5=MR).1]
if { $found_mrn ne “” } {
lappend dispList “CONTINUE $mh”
echo found mrn $found_mrn
} else {
# Send an error message
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
}
proc tpsMissingMRN_1 { args } {
global HciConnName
keylget args MODE mode
set ctx “” ; keylget args CONTEXT ctx
set module “tpsMissingMRN/$HciConnName/$ctx”
set uargs {} ; keylget args ARGS uargs
set debug 0
catch {keylget uargs DEBUG debug}
set dispList {}
switch -exact — $mode {
start {}
run {
keylget args MSGID mh
package require hl7
set hl7 [hl7::parse_msg [msgget $mh]]
set MSH_10 [hl7::get_field hl7 MSH.10]
set PID_3 [hl7::get_field hl7 PID.3]
set PID_3_1 [hl7::get_field hl7 PID.3.1]
set PID_3_5 [hl7::get_field hl7 PID.3.5]
set OBR_2 [hl7::get_field hl7 OBR.2]
echo “MSH_10: $MSH_10”
echo “OBR_2: $OBR_2”
set found_mrn [hl7::get_field hl7 PID.3(5=MR).1]
if { $found_mrn ne “” } {
lappend dispList “CONTINUE $mh”
echo found mrn $found_mrn
} else {
# Send an error message
set errormsg “This message has no MRN identifier included.”
echo “Killing message with Message Control ID: |$MSH_10| because of $errormsg”
lappend dispList “KILL $mh”
}
#SOURCECONN is the name of the thread that initially receives the message (CARD_Orders_from_Meditech)
set sourceconn [msgmetaget $mh SOURCECONN]
set errormsg “”
if {$sourceconn eq “”} {
set sourceconn TestTool
}
#Create the e-mail
###echo “This is sourceconn: |$sourceconn|”
set mailto [tbllookup Missing_MRN_Email $sourceconn]
echo “This is mailto: |$mailto|”
set mailfrom [tbllookup Missing_MRN_Email “MailFrom”]
set mailhost [tbllookup Missing_MRN_Email “MailHost”]
set PORT [tbllookup Missing_MRN_Email “MailPort”]
set mailsubjectprefix [tbllookup Missing_MRN_Email “MailSubjectPrefix”]
set subject “$mailsubjectprefix Meditech sent an HL7 Order Message without the MRM number to Cloverleaf”
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n$errormsg \n\n The Order Id is : (OBR.2) $OBR_2”
#echo “This is errormsg: |$errormsg|”
set emailbody $errormsg
SendEmailAsAlert $mailto “” $emailbody $subject $mailfrom “” “” $mailhost $PORT “0” “0”
###echo “email should have been send now”
} else {
lappend dispList “CONTINUE $mh”
}
time {}
shutdown {}
default {
error “Unknown mode ‘$mode’ in $module”
}
}
return $dispList
}
Hi David,
Just to clarify,
(1) This line (.1):
set found_mrn [hl7::get_field hl7 PID.3(5=MR).1]
Does “.1” mean PID3.5.1 field value?
(2) I know I am not using the “for” look in the code above anymore but,
for { set i 1 } { $i <= [hl7::count hl7 PID.3] } { incr i } {
Does the above mean that loop through the repitions of PID-3 field and incremented by 1 after each loop i.e. after every “~” in PID-3?
(1) Yes.
(2) Yes. The “count” proc tells you how many repetitions of the field are present in the message.
(3) I think your brace alignment is off. You need to remove the brace on the line after lappend dispList “KILL $mh”. As it is right now, all the code below that is getting run regardless of whether or not there’s an MRN. And you need to replace these lines:
} else {
lappend dispList “CONTINUE $mh”
}
With two closing braces (}}). One brace to close the if statement and one to close the “run” section of the switch. The “CONTINUE” here is redundant with the one above it and shouldn’t be included.
Also, 1 silly qn please,
Qn #3:
Why is the “errormsg” set at the top does not get displayed in an email?
I tried changing the variable to an “error” but still the same?
set errormsg “This is the message ID: $MSH_10 \n\n This is the reason why:\n\n $errormsg \n\n The Order Id is : (OBR.2) $OBR_2”
*****************************************
The email only shows the details below:
“This is the message ID: ORDERS-OUT.12.2431267
This is the reason why:
The Order Id is : (OBR.2) CARD20240730-0002COCLB”
In addition to the above, I would also expect the line below to be displayed in my email:
“This message has no MRN identifier included.”
Thank you David.