JIRA Calculated Number Field - If statement formula help

Hi, apologies if this is a noob question.

I've been able to get the Calculated Number field to work based on the below, where 3 select custom field values are multiplied and added together.

<!-- @@Formula:

((issue.get("customfield_1") != null ? Integer.parseInt(issue.get("customfield_1").toString()) : 1 ) * 10) +

((issue.get("customfield_2") != null ? Integer.parseInt(issue.get("customfield_2").toString()) : 1 ) * 45) +

((issue.get("customfield_3") != null ? Integer.parseInt(issue.get("customfield_3").toString()) : 1 ) * 30)

-->

What I would like to do now is use an IF statement (based on a value of another select custom field) to process one calculation (eg. value = RED) and process another calculation (eg. value <> RED).

Both sets of calculations would be almost identical except RED would be as per above and all other values would see calculations at twice the multiplication factor.

<!-- @@Formula:

((issue.get("customfield_1") != null ? Integer.parseInt(issue.get("customfield_1").toString()) : 1 ) * 20) +

((issue.get("customfield_2") != null ? Integer.parseInt(issue.get("customfield_2").toString()) : 1 ) * 90) +

((issue.get("customfield_3") != null ? Integer.parseInt(issue.get("customfield_3").toString()) : 1 ) * 60)

-->

How can this be achived? Thanks

4 answers

1 accepted

This widget could not be displayed.

All I'm saying is to go back to your original implementation and ditch the check for if cf1.toString().isEmpty().

i.e.

&lt;!-- @@Formula:

Object cf1 = issue.get("customfield_10600");
Object cf2 = issue.get("customfield_10601");
Object cf8 = issue.get("customfield_10015");

int number = ((cf1 != null ? Integer.parseInt(cf1.toString()) : 0 ) * 20) +
             ((cf2 != null ? Integer.parseInt(cf2.toString()) : 0 ) * 20);

if( cf8 != null &amp;&amp; !cf8.toString().equals( "RED" ) ) {
    number = ((cf1 != null ? Integer.parseInt(cf1.toString()) : 0 ) * 45) +
             ((cf2 != null ? Integer.parseInt(cf2.toString()) : 0 ) * 32);
}

return number;
--&gt;

Thanks Phillip! It works a treat.

I see the issue with my original was the : 1, when it should have been : 0

This widget could not be displayed.
&lt;!-- @@Formula:

Object cf1 = issue.get("customfield_1");
Object cf2 = issue.get("customfield_2");
Object cf3 = issue.get("customfield_3");
Object cf4 = issue.get("customfield_4");

int mult = 1;
if( cf4 != null &amp;&amp; !cf4.toString().equals( "RED" ) ) {
    mult = 2;
}

int number = ((cf1 != null ? Integer.parseInt(cf1.toString()) : 1 ) * 10 * mult) +
             ((cf2 != null ? Integer.parseInt(cf2.toString()) : 1 ) * 45 * mult) +
             ((cf3 != null ? Integer.parseInt(cf3.toString()) : 1 ) * 30 * mult);
return number;
--&gt;

Thanks Philiip.

For extra points, how could I achieve this if it required a whole new calculation based on cf4?

The reason I ask is the multiplication is not exactly 2x

Calc 1 if cf4 = "RED"

  • cf1 = *10
  • cf2 = *45
  • cf3 = *30


Calc 2 if cf4 <> "RED"

  • cf1 = *25
  • cf2 = *60
  • cf3 = *35

Thanks Phillip!

With your answer, I've been able to solve my problem by learning from yours and making two different calculations.

The multiplication was not 2x.

Code below.

<!-- @@Formula:

Object cf1 = issue.get("customfield_1");

Object cf2 = issue.get("customfield_2");

Object cf3 = issue.get("customfield_3");

Object cf4 = issue.get("customfield_4");

Object cf5 = issue.get("customfield_5");

Object cf6 = issue.get("customfield_6");

Object cf7 = issue.get("customfield_7");

Object cf8 = issue.get("customfield_8");

int number = ((cf1 != null ? Integer.parseInt(cf1.toString()) : 1 ) * 20) +

((cf2 != null ? Integer.parseInt(cf2.toString()) : 1 ) * 20) +

((cf3 != null ? Integer.parseInt(cf3.toString()) : 1 ) * 20) +

((cf4 != null ? Integer.parseInt(cf4.toString()) : 1 ) * 100) +

((cf5 != null ? Integer.parseInt(cf5.toString()) : 1 ) * 20) +

((cf6 != null ? Integer.parseInt(cf6.toString()) : 1 ) * 30) +

((cf7 != null ? Integer.parseInt(cf7.toString()) : 1 ) * 200);

if( cf8 != null && !cf8.toString().equals( "RED" ) ) {

number = ((cf1 != null ? Integer.parseInt(cf1.toString()) : 1 ) * 45) +

((cf2 != null ? Integer.parseInt(cf2.toString()) : 1 ) * 32) +

((cf3 != null ? Integer.parseInt(cf3.toString()) : 1 ) * 60) +

((cf4 != null ? Integer.parseInt(cf4.toString()) : 1 ) * 120) +

((cf5 != null ? Integer.parseInt(cf5.toString()) : 1 ) * 32) +

((cf6 != null ? Integer.parseInt(cf6.toString()) : 1 ) * 35) +

((cf7 != null ? Integer.parseInt(cf7.toString()) : 1 ) * 200);

}

return number;

-->

My issue now is the Select options which are blank/not answered, are now adding into the total. Eg. cf7 was not answered, but it is adding 200 onto the total.

I could solve the problem by defaulting the initial state of the Select value to be 0, but would prefer the blank option.

If cf7 was not answered, wouldn't cf7.toString() return an empty string? If so, you could just check on that before casting cf7.toString() to an Integer.

Something like this...

(cf7 != null ? (cf7.toString().isEmpty() ? 0 : Integer.parseInt(cf7.toString())) : 1 ) * 200

This is the update code that is accepted by Calculated Number field, but it produces a number of 40 when all fields have not been answered and cf8 = RED. I must have missed something.

It's been shortened to only have 3 objects

<!-- @@Formula:

Object cf1 = issue.get("customfield_10600");

Object cf2 = issue.get("customfield_10601");

Object cf8 = issue.get("customfield_10015");

int number = ((cf1 != null ? (cf1.toString().isEmpty() ? 0 : Integer.parseInt(cf1.toString())) : 1 ) * 20) +

((cf2 != null ? (cf2.toString().isEmpty() ? 0 : Integer.parseInt(cf2.toString())) : 1 ) * 20);

if( cf8 != null && !cf8.toString().equals( "RED" ) ) {

number = ((cf1 != null ? (cf1.toString().isEmpty() ? 0 : Integer.parseInt(cf1.toString())) : 1 ) * 45) +

((cf2 != null ? (cf2.toString().isEmpty() ? 0 : Integer.parseInt(cf2.toString())) : 1 ) * 32);

}

return number;

-->

I did a quick test on my local JIRA instance and found out that if a custom field isn't answered (say, cf1), then cf1 == null.

So, it should be sufficient to just check for null and not additionally check if the toString method is empty.

I'm sorry Phillip. I'm not really good at making coding changes. Any chance you can make the proposed amendments to this code? Your help is much appreciated!!

<!-- @@Formula:

Object cf1 = issue.get("customfield_10600");

Object cf2 = issue.get("customfield_10601");

Object cf8 = issue.get("customfield_10015");

int number = ((cf1 != null ? (cf1.toString().isEmpty() ? 0 : Integer.parseInt(cf1.toString())) : 1 ) * 20) +

((cf2 != null ? (cf2.toString().isEmpty() ? 0 : Integer.parseInt(cf2.toString())) : 1 ) * 20);

if( cf8 != null && !cf8.toString().equals( "RED" ) ) {

number = ((cf1 != null ? (cf1.toString().isEmpty() ? 0 : Integer.parseInt(cf1.toString())) : 1 ) * 45) +

((cf2 != null ? (cf2.toString().isEmpty() ? 0 : Integer.parseInt(cf2.toString())) : 1 ) * 32);

}

return number;

-->

This widget could not be displayed.

Hi every one

It may sound like a simple question for you guys.

I would like to calculate value based on a simple select list:

If Select list value1= A, show 1

If Select list value2= B, show 2

Any advide please?

You should post this as a new question, or else nobody will notice.

I believe you're looking for something like:

if (issue.get("customfield_12345")==null)
  return null;
if (issue.get("customfield_12345").getValue().equals("A"))
  return 1;
if (issue.get("customfield_12345").getValue().equals("B"))
  return 2;

This widget could not be displayed.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

112 views 2 0
Join discussion

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