javascriptmirth

How do I get channel information in a Mirth Connect JavaScript Reader Source Connector?


I'm working with Mirth Connect Server 3.8.1 and I'm using a JavaScript Reader as a channel's Source Connector. I'm trying to get the current channel id, but it seems that that's not possible. After googling I've tried the following things to see if yielded any results but it did not:

logger.info(typeof channelId != 'undefined' ? ChannelUtil.getDeployedChannelName(channelId) : 'N/A');
logger.info(typeof connector != 'undefined' ? connector : 'N/A');
logger.info(typeof channel != 'undefined' ? channel : 'N/A');

This just produces three "N/A" in the logs. Obviously I could hard-code the channel id, but I want to export this config eventually and be able to import it to other environments without needing to edit anything. Plus it feels like a Source Connector piece of JS should know who it is working for.

How do I get this information from within the script?

Update: The answer "This should work" is essentially correct, because as it turns out this does work in a brand new channel. The channel I was working with was imported and although I could find no other scripts affecting that channel, it was clearly broken. So possible the real answer is "Mirth Connect Server 3.8.1 has a bug that causes this to sometimes not work and the workaround is creating a new channel."

Update 2: The culprit turned out to be me using the channelId variable in a local scope later, something along the lines of:

for (var i=0; i<allChannelIds.size(); i++) {
    var channelId = allChannelIds.get(i);
    // Other code
}

I wrote that before realizing there was a global scope channelId variable and before needing it. Apparently this resets it to undefined. If the global channelId was set each time the script executes, this should not be a problem, but apparently that is not the case.


Solution

  • You might be overcomplicating things.

    In Mirth 3.5.1 the following code worked fine for me in the Transformer on the source connector of a channel:

    logger.info("Channel ID: "+channelId);
    

    The result was something like this:

    [2020-02-25 13:03:30,033]  INFO  (transformer:?): Channel ID: 1122e1c7-34af-4141-9388-533005346d0b
    

    This ID matches the ID that appears in the Mirth dashboard and on the channel summary page.

    I also verified that the code worked in a Javascript Reader. In this case, the logged message was

    [2020-02-25 14:33:20,004]  INFO  (js-connector:?): Channel ID: 1122e1c7-34af-4141-9388-533005346d0b