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

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 vote

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).

HappyMe.png

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

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

125 views 0 4
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you