A Mirth Connect JavaScript transformer is where a channel changes a message, and it is where teams quietly paste a patient name into the server log. The script runs on the server, on every message, which makes both the edit and the log line durable.
This deep dive is for hospital CTOs, CIOs and digital health founders who own interface quality, and for the engineers who write and review channel scripts. You get how a transformer reads and sets fields, what belongs in a map or a code template, how to keep PHI out of logs, and how to prove the script worked.
Where does a JavaScript transformer step run in Mirth Connect?
A JavaScript transformer step in Mirth Connect runs on the server, inside the channel, on the source connector or on a destination connector. Agnotic Technologies treats the script as production code: it runs for every message that reaches that step, not as a scratch pad on the analyst's desktop.
Review a Mirth Connect transformer script
Our Mirth Connect page covers the channel scripts we write and review for hospital interface teams.
NextGen Healthcare's Mirth Connect documentation on custom Java says that code can be called from any JavaScript context, including filters and transformers, and its example writes to the server log with logger.info.
Filters and transformers are both JavaScript contexts. They are not the same step. The filter has already accepted or dropped the message by the time a transformer step is the right tool. Put field edits in the transformer. Put the accept-or-drop decision in the filter that runs before it.
If you step through a script with a debugger, do it on a non-production server. The deployed script still transforms each message when nobody is watching it.
How do you read a field?
Read a field from the message object the engine has already parsed. For an HL7 v2 data type, address the segment, the field and the component. For JSON, the same step sees msg as a JavaScript object, and NextGen Healthcare's 3.3 notes say msg and tmp are converted to and from JavaScript objects for that data type. Read the data type of the channel before you copy a line from another channel.
On an HL7 v2 message, the first component of PID field 3 is the path msg['PID']['PID.3']['PID.3.1']. Convert it to a string before you compare it. A missing segment throws, or yields an empty value, depending on how you address it, so test the segment you require and fail the message in a way your agreement allows when the segment is absent.
- Prefer one component over the whole field when the field repeats or has several components. Field 3 of PID often has more than an id.
- Trim only what the agreement says to trim. Stripping characters because they 'look wrong' will drop a legitimate identifier.
- Do not read a field by splitting the raw message on the pipe character unless you are outside the parsed message on purpose. The parsed object already handles separators.
- When a destination uses an outbound template, read what you need from msg and write the outbound message on tmp. tmp is the template, not a second name for the inbound message.
How do you set a field?
Set a field by assigning the same path you would read, on msg, or on tmp when an outbound template is in use. The step keeps the message object you edited. Do not rely on a return value from the script to replace the message.
- Assign a string or a value the data type accepts. An object dumped into a component becomes an unreadable blob downstream.
- If the agreement says a missing value must clear the destination's field, assign the clear value the agreement names. An empty assignment and a clear are different, and the agreement has to pick one.
- Set only the fields this destination needs. Copying the whole inbound message and then 'fixing a few fields' hides the edits.
- After you assign, read the path back in a test and compare it to the expected outbound message. The assignment you typed is not proof the encoder kept it.
What should go in a map instead of the message?
Put a value in a map when a later step in the same channel needs it and it is not a field of the outbound message. Leave the message itself for fields the destination is supposed to receive.
- channelMap: values that travel with this one message through this channel, then go away with the message.
- globalChannelMap: values that must survive from one message to the next on this channel. They live in memory on that server. Do not put a patient identifier there.
- configurationMap: settings an administrator should change without editing the script, such as a destination code or a feature flag for this environment.
- responseMap: what a destination answered. Read it when a later step must react to that answer. Do not log the response if the destination echoed the patient.
A map is not a document store and not a queue. If the value has to survive a restart, write it to a database the channel already uses for that purpose, or keep it in the message. A global channel map emptied by a restart will look like a logic bug at 2 a.m.
What should go in a code template?
Put a function in a code template when more than one channel needs it, or when the same channel would otherwise paste the function into several steps. A template library can be included on the channels that need it and left off the ones that do not.
NextGen Healthcare's Mirth Connect 3.3 release notes describe code template libraries that can be included or excluded on a specific channel, and say a channel export can include the libraries that channel uses.
- A function that reads one identifier from PID, with the assigning authority passed in, belongs in a template.
- A function that knows one destination's private codes belongs in a template included only on that destination's channels.
- A one-line assignment used in one step can stay in the step. Moving it to a template does not make it clearer.
- When you export a channel for another environment, include the libraries that channel uses, which is the export option the 3.3 notes describe.
Name the template for what it returns. Keep patient values out of its comments, because comments ship with the library.
How do you keep PHI out of the script log?
Keep protected health information out of the script log by never passing a patient identifier, a name, an address, a document body or the whole message into logger. The logger writes to the server log, which is the wrong cupboard for a chart.
The HIPAA Privacy Rule's minimum necessary standard requires covered entities to limit uses and disclosures of protected health information to the minimum needed for the purpose.
- Allowed in a log line: the channel name, the destination name, and the message control id from the message header.
- Not allowed: PID, patient name, medical record number, account number, address, date of birth, or any free-text note.
- Not allowed: logger of the entire msg object 'just this once' while you debug. That line will still be in the log after you forget it.
- If you need to inspect a real message, do it on a non-production server with a fictional sample, or inside a debugger session you close.
The minimum necessary standard is the reason the log gets a control id and not a chart. A control id lets you find the message in the engine. It does not belong in an email, a ticket or a chat paste either, once the message itself holds the identifiers.
How do you know the script did what you intended?
You know the script worked when a fictional sample goes in and the outbound fields match a file you wrote down in advance. Run that check on a non-production engine. Reading the script is not the same check.
- One sample with the field present, and the expected outbound value.
- One sample with the field missing, and the failure or the default your agreement names.
- One sample where a repeating field has two repetitions, if the script claims to pick one of them.
- A comparison of the whole outbound message, so an edit in a field you did not mean to touch still fails the check.
A debugger session on a non-production server can help you see why a field came out wrong. It is optional. The expected outbound file is not optional. Keep that file with the channel, and run it again when the script changes.
When is the script too large?
The script is too large when it encodes a table of codes that an administrator should edit, when it handles more than one message type's rules, or when a reader cannot name the one field it is responsible for. Split it.
- Code tables go to configuration or to a template the channels share, not to a hundred-line ladder of conditions.
- A second message type's rules go to the channel that owns that type, not into a branch at the top of this step.
- Logging, mapping and routing are three steps. A transformer that also decides the destination has taken on a second job.
- If you cannot describe the step in one sentence, it is more than one step.
A named template that reads one identifier is easier to review than a compact line. Reviewers should see every field the script writes by reading it once.
How should a transformer handle an exception?
Catch only the errors you can name, and let the rest fail the message so it reaches the error path with its original bytes. A catch block that swallows everything turns a mapping fault into a quietly wrong outbound field.
Should a transformer call an external service?
Avoid it. A network call per message adds a dependency the step cannot queue around. Put lookups in a table the script reads, or in a destination that can retry.
Where should date and time conversions happen?
In one code template that takes the agreed input and output formats, so every channel converts the same way. Conversions hand-written in each step drift on time zones and empty values.
How do you review a transformer change?
- List every path the change writes.
- Confirm no new logger call carries a patient field.
- Run the fictional samples and read the full outbound diff.
What are the common mistakes with Mirth Connect JavaScript transformers?
- Logging the whole msg object while debugging and leaving the line in the deployed channel.
- Returning a value from the script and expecting it to replace the message.
- Pasting the same helper function into several channels instead of a shared code template.
- Keeping a patient identifier in globalChannelMap, where it sits in server memory between messages.
- Catching every exception, so a mapping fault becomes a quietly wrong outbound field.
How can Agnotic help you write Mirth Connect transformer scripts?
Agnotic writes the transformer step, the template library it shares, and the fictional samples that prove the outbound fields. We read the logger calls in the same review and take patient identifiers out of them before the channel is deployed.
Have a transformer script reviewed
We will trace the fields the script reads and sets, and the lines it logs.
Related reading on Agnotic
Channel shape, tests and the HL7 channels these scripts sit inside.


