web stats
Channel level variable - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-06-2015, 08:18 PM
Michael Clarke Michael Clarke is offline
What's HL7?
 
Join Date: May 2015
Posts: 2
Michael Clarke is on a distinguished road
Lightbulb 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 at 08:21 PM.
Reply With Quote
  #2  
Old 05-07-2015, 05:54 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,299
cory_cole is on a distinguished road
Default

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.
Reply With Quote
  #3  
Old 05-07-2015, 07:42 AM
Michael Clarke Michael Clarke is offline
What's HL7?
 
Join Date: May 2015
Posts: 2
Michael Clarke is on a distinguished road
Default

Yes that is my intent

So

s = '' in a transformer is perfectly acceptable?
Reply With Quote
  #4  
Old 05-07-2015, 07:46 AM
cory_cole cory_cole is offline
Mirth Guru
 
Join Date: Mar 2012
Posts: 1,299
cory_cole is on a distinguished road
Default

Yes it is.
Reply With Quote
  #5  
Old 05-07-2015, 08:41 AM
narupley's Avatar
narupley narupley is online now
Mirth Employee
 
Join Date: Oct 2010
Posts: 7,124
narupley is on a distinguished road
Default

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.
Reply With Quote
Reply

Tags
best practice, channel, variable

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 07:49 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation