Search code examples
mirth

Template string being written as literal in Mirth transformer mapping?


Template string being written as literal in Mirth (v3.10) transformer mapping? I am trying to fill the HL7 (v2.3) MSH.7 (date/time) field in a message from a Mirth Source Transformer by mapping the MSH[7] field to the javascript one-liner

`${new Date().getFullYear()}` + `${new Date().getMonth()+1}`.padStart(2, "0") + `${new Date().getDate()+1}`.padStart(2, "0") + `${new Date().getHours()}`.padStart(2, "0") + `${new Date().getMinutes()}`.padStart(2, "0") + `${new Date().getSeconds()}`.padStart(2, "0")

enter image description here (to get datetime as format YYYMMDDHHMMSS) but when looking at the result (in the resulting file that gets written at the Channel's Destination) I see the literal code as text rather than what it should evaluate to (even though it is my understanding that Mirth does support string templates now). The step successfully validates in the editor and errors out when I make the string wrong in a JS context, so I assume that (at least at that stage) it is being interpreted as JS code.

Have also tried

(`${new Date().getFullYear()}` + `${new Date().getMonth()+1}`.padStart(2, "0") + `${new Date().getDate()+1}`.padStart(2, "0") + `${new Date().getHours()}`.padStart(2, "0") + `${new Date().getMinutes()}`.padStart(2, "0") + `${new Date().getSeconds()}`.padStart(2, "0")).toString()

so in the Generated Code tab, I see

tmp['MSH'][i]['MSH.7']['MSH.7.1'] = validate('', `${new Date().getFullYear()}` + `${new Date().getMonth()+1}`.padStart(2, "0") + `${new Date().getDate()+1}`.padStart(2, "0") + `${new Date().getHours()}`.padStart(2, "0") + `${new Date().getMinutes()}`.padStart(2, "0") + `${new Date().getSeconds()}`.padStart(2, "0")).toString(), new Array());

but got similar results (even though both work as expected when just testing on the Node CLI interpreter).

Anyone with more experience know what could be going on here? (PS. is there an easier way to get the datetime into that field)?


Solution

  • Mozilla Rhino is the javascript engine used by (Mirth) Connect. Template literals are still unsupported at this time. See below for a listing of which features are present and missing in mirth. Check the server-lib directory in your mirth install directory and look for the rhino jar to see which version is packaged with your version of mirth. Only mirth versions 3.7 and higher allow you to set the language version required to enable some of the features. Older versions run at a language version of zero or "default".

    https://mozilla.github.io/rhino/compat/engines.html#ES2015-syntax-template-literals