web stats
returning the value of a variable in javascript - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 09-28-2015, 02:56 PM
ferdinand ferdinand is offline
OBX.1 Kenobi
 
Join Date: Sep 2014
Posts: 34
ferdinand is on a distinguished road
Default returning the value of a variable in javascript

Hi folks
I have a doubt.
I have made a script to read the clinical history number, (peide) and remove some numbers.

The script is as follows
var longitud=$('PEIDE').length();
for (var i = 0; i< $('PEIDE').length; i++)
{
var caracter = $('PEIDE').charAt(i);
if( caracter == "X" || caracter == "x") {
var res = str.substring(0, i);
globalChannelMap.put('res')
return false;
} else {

return true;
}


and after that a message builder step to integer that $('res') to pid field 5.

Problem is... not funcioning. Not a single error so I guess I'm doing something wrong.

Can you please help?
Reply With Quote
  #2  
Old 09-29-2015, 05:01 AM
mcalKno mcalKno is offline
 
Join Date: Apr 2015
Location: Pennsylvania
Posts: 247
mcalKno is on a distinguished road
Default

Quote:
Originally Posted by ferdinand View Post
Hi folks
I have a doubt.
I have made a script to read the clinical history number, (peide) and remove some numbers.

The script is as follows
var longitud=$('PEIDE').length();
for (var i = 0; i< $('PEIDE').length; i++)
{
var caracter = $('PEIDE').charAt(i);
if( caracter == "X" || caracter == "x") {
var res = str.substring(0, i);
globalChannelMap.put('res')
return false;
} else {

return true;
}


and after that a message builder step to integer that $('res') to pid field 5.

Problem is... not funcioning. Not a single error so I guess I'm doing something wrong.

Can you please help?
Wouldn't you need something like this...

Code:
globalChannelMap.put('res', res);
Then you could call $('res')
Reply With Quote
  #3  
Old 09-29-2015, 12:55 PM
ferdinand ferdinand is offline
OBX.1 Kenobi
 
Join Date: Sep 2014
Posts: 34
ferdinand is on a distinguished road
Default

nothing happens when I call $('res')
empty field
I use
message builder tmp['QRD']['QRD.8']['QRD.8.1']
mapping $('res')

It should be something in concept that i'm not doing right, I know
...
Reply With Quote
  #4  
Old 09-29-2015, 01:27 PM
plew plew is offline
OBX.1 Kenobi
 
Join Date: Oct 2011
Posts: 34
plew is on a distinguished road
Default

Quote:
Code:
var longitud = $('PEIDE').length();
 for (var i = 0; i < $('PEIDE').length; i++) {
     var caracter = $('PEIDE').charAt(i);
     if (caracter == "X" || caracter == "x") {
         var res = str.substring(0, i);
         globalChannelMap.put('res')
         return false;
     } else {
         return true;
     }
}
You're only reading the first character before your script returns.

You say message builder so I'm assuming this is in a transformer step. You don't need the returns at all if that's the case.

The globalChannelMap.put needs two arguments. It's never getting called, because it would definitely throw an error if it was.

Use this instead:
Code:
var longitud = $('PEIDE').length();
for (var i = 0; i < $('PEIDE').length; i++) {
    var caracter = $('PEIDE').charAt(i);
    if (caracter == "X" || caracter == "x") {
        var res = str.substring(0, i);
        globalChannelMap.put('res', res)
    }
}
Reply With Quote
  #5  
Old 09-29-2015, 01:54 PM
ferdinand ferdinand is offline
OBX.1 Kenobi
 
Join Date: Sep 2014
Posts: 34
ferdinand is on a distinguished road
Default

Yes this is a transformer step

I have done the mods you suggested and still doesn't work.

If only I found an example of use of variables in javascript...
Reply With Quote
  #6  
Old 09-29-2015, 02:25 PM
plew plew is offline
OBX.1 Kenobi
 
Join Date: Oct 2011
Posts: 34
plew is on a distinguished road
Default

Quote:
Originally Posted by ferdinand View Post
If only I found an example of use of variables in javascript...
Here's a good spot for that: https://developer.mozilla.org/en-US/...ript/Reference

Looked over your code a little more and came up with this:
Code:
var peide = new String($('PEIDE'))
for (var i = 0; i < peide.length; i++) {
    var caracter = peide.charAt(i);
    if (caracter == "X" || caracter == "x") {
        var res = peide.substring(0, i);
        globalChannelMap.put('res', res)
    }
}
The code was calling $ more than needed, referencing a variable that was undefined (str), and creating unused variables (longitud).

Here's another way of doing that thing:
Code:
var peide = new String($("PEIDE"))
var match = peide.match(/(.*)x.*/i);
globalChannelMap.put("res", match? match[1]: peide);

Last edited by plew; 09-29-2015 at 02:32 PM.
Reply With Quote
  #7  
Old 09-29-2015, 03:00 PM
ferdinand ferdinand is offline
OBX.1 Kenobi
 
Join Date: Sep 2014
Posts: 34
ferdinand is on a distinguished road
Default

Quote:
Originally Posted by plew View Post
Here's a good spot for that: https://developer.mozilla.org/en-US/...ript/Reference

Looked over your code a little more and came up with this:
Code:
var peide = new String($('PEIDE'))
for (var i = 0; i < peide.length; i++) {
    var caracter = peide.charAt(i);
    if (caracter == "X" || caracter == "x") {
        var res = peide.substring(0, i);
        globalChannelMap.put('res', res)
    }
}
The code was calling $ more than needed, referencing a variable that was undefined (str), and creating unused variables (longitud).

Here's another way of doing that thing:
Code:
var peide = new String($("PEIDE"))
var match = peide.match(/(.*)x.*/i);
globalChannelMap.put("res", match? match[1]: peide);

Thanks thanks thanks!!!
Finally it works!

So, Can I use the variables directly in javascript? it seems not.
Have you used any tool to debug it?

Yours faithfully,
Reply With Quote
Reply

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 02:09 PM.


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