rcodein  = aEvent.message.hl7.segment("OBR",0).Element("4").AsString;

      tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein);

      tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0);
      tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code");
      tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn");

      aEvent.message.hl7.segment("OBR",i).element("4.1").AsString = tcodeout;
      aEvent.message.hl7.segment("OBR",i).element("4.2").AsString = tdescout;
      aEvent.message.hl7.segment("OBR",i).element("4.3").AsString = "HL7Connect";

Improving the script

Although the code above (from Part A) will work for the simple translation demonstrated, it is not good code as it does not take into account the errors that could be thrown by the various functions used.

For the time being we will preserve our assumption that the segment and element exist. This means that the first line is then still okay as is. However, it may simply be empty (resulting in an empty string being returned). In this case the javascript checks for this (as it is convenient to have the if block) and the VBscrript will simply let the next command throw an error.

The second line also needs alteration. If no match to rcodein is found within the terminology urn://www.hl7connect.com/SysCodes, then an exception will be raised.

Likewise, in LexTranslate. If no terms are found and no default is provided, or if multiple matches are found, then an exception will be raised.

LexGetTermRepresentation, as long as the Representation Type is valid, will not raise any exceptions.

The last three lines will only cause any problems if any of the Lex functions raised exceptions, or if you are trying to translate an element that is not a CE.

In this case, the situation is fairly easy to handle. In javascript we can simply wrap the Lex functions in a try .. catch block, and execute the last three lines only if no exception has been called. In VBscript it is a bit more tedious (as VBscript has poor error handling capabilities), but the solution is similar.

Create a new script window and copy/paste the relevant script. Save as lex8.vbs OR lex8.js as appropriate.

Language    Script
function translate(aEvent)

  rcodein  = aEvent.message.hl7.segment("OBR",0).Element("4").AsString

  While (Err.number = 0) and (count < 8)
    On Error Resume Next
      Select Case count
        Case 1
          tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein)
        Case 2
          tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0)
        Case 3
          tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code")
        Case 4
          tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn")
        Case 5
          aEvent.message.hl7.segment("OBR",0).element("4.1").AsString = tcodeout
        Case 6
          aEvent.message.hl7.segment("OBR",0).element("4.2").AsString = tdescout
        Case 7
          aEvent.message.hl7.segment("OBR",0).element("4.3").AsString = "HL7Connect"
      end Select
    count = count + 1

  If Err.number <> 0 Then
    Kernel.WriteToLog DBG_WARNING, "DBERROR: " & Err.Description
  End If

End function
function translate(aEvent)
    var rcodein;
    var tcodekeyin;
    var tcodekeyout;
    var tcodeout;
    var tdescout;

    rcodein  = aEvent.message.hl7.segment("OBR",0).Element("4").AsString;

    if (rcodein != "")
        tcodekeyin = Lex.LexGetTermByCode("urn://www.hl7connect.com/SysCodes", rcodein);

        tcodekeyout = Lex.LexTranslate(tcodekeyin, "urn://www.hl7connect.com/LOINC", 0);
        tcodeout = Lex.LexGetTermRepresentation(tcodekeyout, "Code");
        tdescout = Lex.LexGetTermRepresentation(tcodekeyout, "Defn");
        Kernel.WriteToLog(DBG_WARNING,'DBERROR: ' + LastErrorMessage);
        break translate_obr;

    aEvent.message.hl7.segment("OBR",0).element("4.1").AsString = tcodeout;
    aEvent.message.hl7.segment("OBR",0).element("4.2").AsString = tdescout;
    aEvent.message.hl7.segment("OBR",0).element("4.3").AsString = "HL7Connect";


Testing of Lexicon Server

MSH|^~\&|KESTPLS.LOCAPP^KESTVILLE PLS^L|KEST^Kestville Hospital.LOCHOSP^L|KESTREPOS.LOCAPP^KESTVILLE REPOSITORY^L|KEST^Kestville Hospital.LOCHOSP^L|20040119143926+1000||ORU^R01^ORU_R01|KEST_000401191439251|P|2.3.1|||AL|NE|AUS
PID|||134680^^^KEST&Kestville Hospital.LOCHOSP&L^MR~302533^^^KESTPLS.LOCAPP&KESTVILLE PLS&L^PI~6187/59213/4^^^AUSHIC^MC||Citizen^John||19730701|M|||1 SMITH STREET^^MELBOURNE^^3000||^PRN^PH^^^^0397111234
OBR|1||04P800000-1049151^KESTPLS.LOCAPP^KESTVILLE PLS^L|Fbc|||20040119112500+1000|20040119112500+1000||Hosp^Hospital Collection^^^^^^Collection^KESTPLS.LOCAPP&KESTVILLE PLS&L||""|? Kidney Failure|20040119130000+1000||1234567A^Kelly^Ned^^^^^AUSHICPR||||||20040119143900+1000||CH|R||^^^20040119113000+1000^^R|||||Manager&Manager&&&&&&&KESTPLS.LOCAPP
OBX|1|NM|Wcc||2.0||3.5-5.5|H|||R|||20040119142756+1000||Manager^Manager^^^^^^^KESTPLS.LOCAPP&KESTVILLE PLS&L
OBX|2|NM|AlbU||130||135-145|L|||R|||20040119142756+1000||Manager^Manager^^^^^^^KESTPLS.LOCAPP&KESTVILLE PLS&L
OBX|3|NM|Plat||80||95-110||||R|||20040119142756+1000||Manager^Manager^^^^^^^KESTPLS.LOCAPP&KESTVILLE PLS&L

To test the script take the following steps:

  1. Create a new Test Case for the OnSendMessage event and insert the above message into the upper message window.
  2. Choose the relevant script in the Script drop down box and type translate into the Procedure text entry box.
  3. Save the test case as lex8.tci.

Click on Go to test the script. Your screen should look something like this:

Translated OBR-4 Segment

© Kestral Computing P/L 2000-2010.