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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Bridget Sauer
Published Apr 19, 2018 in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

366 views 0 6
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