Announcement

Collapse
No announcement yet.

Channel level variable

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Channel level variable

    Mirth Connect Server 3.2.1.7650

    I noticed that if I use a variable at the top of a transformer and do not use the var option then it is available to any code templates that exist at channel level

    i.e.

    Create a code template at channel level

    function UpdateVariable()
    {
    s = 'Michael'
    }

    Then do this at the top of a transformer step

    s = ''

    UpdateVariable()

    logger.info(s)

    [2015-05-06 23:02:02,281] INFO (transformer:?): Michael

    Is it acceptable to use Mirth like that, and if not is there a different preferred way to sharing variables across a channel. I know it works and is really handy but I would hate to be basing everything I am doing on that and have it broken in the future?

    If not best practice then what is?

    Also on another note ... why doesn't Mirth list the transformer name why is it always (transformer:?)?
    Last edited by Michael Clarke; 05-06-2015, 07:21 PM.

  • #2
    That is what channel variables are for but they only exist for each message. If you change the value on message1 but don't anything with variable in message2, it will not have the value from message1.

    Comment


    • #3
      Yes that is my intent

      So

      s = '' in a transformer is perfectly acceptable?

      Comment


      • #4
        Yes it is.

        Comment


        • #5
          I would like to add that what you described does not "share variables across channels". It will only affect the current script you're in. So if you set a global variable "s" in one script, don't expect it to be around in any other script.

          On the note of global variables, I would advise against it. It's usually bad practice except for certain specific use-cases. In fact, it leads to unintended results a lot of the time, because variables with common names can conflict, etc. Plenty of people on here have run into issues when using global variables in JavaScript. The worst kind of issues too, the ones that "look" like they should work, but don't

          Instead, pass the information you need into your function, and then have the function pass the result(s) back. Like this:

          Code:
          var s = '';
          s = updateVariable(s);
          // s is now "Michael"
          
          function updateVariable(s) {
              return 'Michael';
          }
          Sometimes it makes sense to pass an object into a function and modify the object itself instead of passing anything back:

          Code:
          var list = new java.util.ArrayList();
          updateList(list);
          // list now contains "Michael"
          
          function updateList(list) {
              list.add('Michael');
          }
          Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

          Nicholas Rupley
          Work: 949-237-6069
          Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


          - How do I foo?
          - You just bar.

          Comment

          Working...
          X