Modify a Code
Contents
Now it’s time to satisfy our last requirement:
5. We need to use HL7 standard codes (“F”, “M” and “U”) for the ‘Sex’ field.
Using annotations to revisit the ‘Out’ variable’s node tree, you’ll notice that we have male represented as “male” and female as “female”. To correct this, we will use an if
statement to substitute “F”, “M” or “U”. We will also copy the ‘Sex’ field into a variable to simplify this logic.
Note: This procedure can be adjusted to fix many common interface requirements (like capitalizing names, converting dates, or reformatting addresses).
Add the following lines of code to your script:
What is happening here? We created the variable Sex
to contain whatever value is currently stored in the message’s ‘Sex’ field (Out.PID[8]). We use the nodeValue()
function to convert a leaf node value (in this case, the message’s ‘Sex’ field value) into a text value. We then plug the results into an if/else statement that checks for “male” and “female”, then returns “M”, “F” or “U” as needed.
Let’s examine the results! In the corresponding annotations, we can see that “female” has been changed to “F” successfully:
Tip: Want to learn more about nodeValue()
? Check out this article: Which string conversion should I use, S() or nodeValue()?
Sample Code
Here is a copy-and-paste version of the code so far:
function main(Data) -- (1) Parse the HL7 message local Msg, Name = hl7.parse{vmd = 'demo.vmd', data = Data} local Out = hl7.message{vmd = 'demo.vmd', name = Name} -- (2) Map the incoming message to the outgoing message Out.MSH:mapTree(Msg.MSH) Out.PID:mapTree(Msg.PID) -- (3) Alter the MSH segment data Out.MSH[3][1] = "First Steps" Out.MSH[4][1] = "Administration" trace(Out) -- (4) Modify the PID data -- modify sex coding to use HL7 standard codes "M", "F", & "U" local Sex = Out.PID[8]:nodeValue() if Sex == 'female' then Out.PID[8] = 'F' elseif Sex == 'male' then Out.PID[8] = 'M' else Out.PID[8] = 'U' -- Unknown end -- (5) Push the outgoing message into the Iguana queue end
The last thing we need to do to create a working channel is to push the data onto the Iguana Queue.