Sidebar

Why will my code not execute my if statement?

0 votes
1.4K views
asked Jan 30, 2014 by gary-t-8719 (14,860 points)
edited Jan 30, 2014 by gary-t-8719
commented Jun 8, 2015 by marc-m-9513 (570 points)
I am not able to get the following if statement to work, any ideas??

 var Route = qie.doTableLookup(uniqueDoctor, uniqueDoctor, 'Provider Table', 'Physician No', 'Route');
      var route= Route+'';
      qie.debug('Route: ' + Route);
         if (route=='FAX'){
          uniqueRoutes[Route] = uniqueDoctor;}
          else {
             uniqueRoutes[Route] = Route;
          }
}
commented Jun 8, 2015 by gary-t-8719 (14,860 points)
edited Jun 9, 2015 by gary-t-8719
After looking at this code it would appear that there may be a problem with the Cross Reference Table. I would suggest the following three items.

1. Check to make sure the table is cross referencing the values as expected
2. I would suggest using the triple equals over the double equals or
3. Best Practice would be to use the stringUtils functions found in the code wizard as they are type agnostic and null safe.

3 Answers

0 votes
 
Best answer

While the string vs string explanation is important to understand QIE now has integrated into the Code Wizard the stringUtils. Using String Utils eliminates the problem with variable type comparisons and in addtion they are "null" safe. It would be considered best practice to always use the stringUtils over the JavaScript variable comparison method.

var patient = source.getNode('IN1-17');
 
if (StringUtils.equals(patient, 'self')) {
 
   //This code will execute when the two variables values match.
 
}
answered Jun 8, 2015 by gary-t-8719 (14,860 points)
0 votes

When dealing with strings in QIE, consideration should be given to the difference between a Java String and a JavaScript String.

QIE is a Java based application so all of the functions are executed on the back end using Java. However, all of the customized scripts written in the front end of QIE are written in JavaScript. This means that when you call any of the built-in QIE functions that return a “String” ( like message.getNode()), the “String” value that is returned is a Java string because the function is executed on the server side or back end.

You can run into problems when you try to use a Java String as a JavaScript String or vice versa. You can also run into problems when comparing values, as in an ‘if’ statement, if the comparison that is being used does not take into consideration that the values might not be the same type.

To convert a Java String to a JavaScript String you just need to add the + ‘’ The plus symbol is a JavaScript concatenation operator and the double single quotes are just an empty string value. You can actually append any string value (in-between the double single quotes) using the JavaScript concatenation operator and the result is a JavaScript String.

So to convert a Java String to a JavaScript String you can do any of the following:

var jsString = javaString + ’’;

 

Below is an example where the code block inside the if statement would never get executed because the patient variable is a Java String and "Self" is a JavaScript String and the two comparisons will never match because of the variable type.

var patient = source.getNode('IN1-17');

if (patient === 'self') {

   This code will never execute even if both variables values are the same.

}

To fix this issue we just need to convert the patient variable to a JavaScript string by adding the + ''

var patient = source.getNode('IN1-17') + '';

if (patient === 'self') {

   This code will execute when the two variables values match.

}

 

answered Jan 30, 2014 by gary-t-8719 (14,860 points)
0 votes

Also check things like case.  Using the example listed earlier:

var patient = source.getNode('IN1-17'); //Assume this returns 'SELF'
 
if (StringUtils.equals(patient, 'self')) { //This would be false
   //This code will execute when the two variables values match.
}
 
if (StringUtils.equalsIgnoreCase(patient, 'self')) { //This would be true
   //This code will execute when the two variables values match.
}

 

answered Jun 9, 2015 by rich-c-2789 (16,180 points)
...