Custom ACKs

Query Response Example

This is a great technique for implementing a query response interface. Say you have an incoming message that looks like:


For which you need to send back an ADR^A19 message giving patients matching the given ID.

Here’s an example. As you can see the principle is simple. The query comes in. It’s parsed and used to generate a SQL query against the database. The data from the SQL query is used to populate the HL7 message that is returned. The logic can be made more elaborate but the principle is the same.

Note: We set live = true to execute SQL queries interactively in the Iguana script editor.

Tip: For a production version it is recommended best practice to escape the Id to avoid a SQL injection attack, see escaping values correctly for SQL.

Here’s the vmd query.vmd and copy of the code used for this example:

-- main() is given the original HL7 message.
function main(Data)
   local Msg = hl7.parse{vmd='query.vmd', data=Data}
   local R = hl7.message{vmd='query.vmd', name='Response'}

   if Msg:nodeName() == 'Unrecognized' then
      ack.send('Unrecognized message')

   MakeMSH(R.MSH, Msg)
   FindPatient(R, Msg.QRD[8][1][1])

function MakeMSH(MSH, Q)  
   MSH[3][1] = Q.MSH[5][1]
   MSH[4][1] = Q.MSH[6][1]
   MSH[5][1] = Q.MSH[3][1]
   MSH[6][1] = Q.MSH[4][1]
   MSH[10] = Q.MSH[10]
   MSH[9][1] = 'ADR'
   MSH[9][2] = 'A19'
   MSH[11][1] = 'P'
   MSH[12][1] = '2.6.1'
   MSH[7] ='%Y%m%d%h%M%S')
   return MSH

function FindPatient(R, Id)
   local Q = db.query{api=db.MY_SQL, name='test',
      user='root', password='password', live=true,
      sql = 'SELECT * FROM Patient WHERE Id ="'..Id..'"'}
   for i=1,#Q do
      AddPatient(R.Group1[i].PID, Q[i])      

function AddPatient(PID, R) 
   PID[5][1] = R.LastName
   PID[5][2] = R.GivenName
   PID[2][1] = R.Id
   PID[8] = R.Sex
   return PID

Leave a Reply