title: How do I get channel information in a Mirth Connect JavaScript Reader Source Connector?
body: <p>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:</p>
  
  <pre><code>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');
  </code></pre>
  
  <p>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.</p>
  
  <p>How do I get this information from within the script?</p>
  
  <p><strong>Update:</strong> 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."</p>
  
  <p><strong>Update 2:</strong> The culprit turned out to be me using the <code>channelId</code> variable in a local scope later, something along the lines of:</p>
  
  <pre><code>for (var i=0; i&lt;allChannelIds.size(); i++) {
      var channelId = allChannelIds.get(i);
      // Other code
  }
  </code></pre>
  
  <p>I wrote that before realizing there was a global scope <code>channelId</code> variable and before needing it. Apparently this resets it to <code>undefined</code>. If the global <code>channelId</code> was set each time the script executes, this should not be a problem, but apparently that is not the case.</p>