Custom Text Field comparison always returns false - how is this possible?

Ben August 2, 2017

Hi there fellows, 

This one is breaking my head ;-)

I have this custom text field which has the following formula: 

<!-- @@Formula:

String critical = "Critical"; // this variable has a string value of Critical
String high = "High"; // this variable has a string value of High
String medium = "Medium"; // this variable has a string value of Medium
String low = "Low"; // this variable has a string value of Low
String verylow = "Very Low"; // this variable has a string value of Very Low

String probability = (String)issue.get("Risk probability"); //This field is a Select List (single choice) from 5 possible entries.
String consequence = (String)issue.get("Risk consequence"); //This field is a Select List (single choice) from 5 possible entries.

if (probability == critical)
return "The same values:" + probability;
else
return "Field Value:|" + probability + "| != constant value:|" + critical + "|";

-->

When the calculation executes, it always result in FALSE

Capture.PNG

  • The first value from the bottom is "Risk probability" custom field of type Select List (single choice)  
  • The second from the bottom is the "Risk consequence" custom field of type Select List (single choice) 
  • The last value from the bottom is the result of the calculation formula at the top. I cannot seem to get it to validate true. 

The following script has the same outcome:

<!-- @@Formula:

String critical = "Critical"; // this variable has a string value of Critical
String high = "High"; // this variable has a string value of High
String medium = "Medium"; // this variable has a string value of Medium
String low = "Low"; // this variable has a string value of Low
String verylow = "Very Low"; // this variable has a string value of Very Low

String probability = issue.get("Risk probability").toString(); //This field is a Select List (single choice) from 5 possible entries.
String consequence = issue.get("Risk consequence").toString(); //This field is a Select List (single choice) from 5 possible entries.

if (probability == critical)
return "The same values:" + probability;
else
return "Field Value:|" + probability + "| != constant value:|" + critical + "|";

-->

 What am I missing? Or do I need to use another approach?

1 answer

0 votes
David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 2, 2017

Hi,

this is likely because you are using the "==" operator to compare Strings. This would be valid in Groovy, but in Java (and BeanShell, which is derived from Java), the "==" operator returns true only if the two operands are one and the same. To test for String equality, you must use the equals() method:

"a".equals("a")

returns true, but:

"a" == "a"

returns false (because the two strings are two different Java objects, even though they contain the same characters).

Ben August 2, 2017

HappyMe.png

Thank you David for calling that out, very much appraciated!!!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events