JIRA Calculated Number Field - If statement formula help

Andy D July 22, 2013

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

1 vote
Answer accepted
Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 28, 2013

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;

Andy D July 29, 2013

Thanks Phillip! It works a treat.

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

1 vote
Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 22, 2013
&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;
Andy D July 23, 2013

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
Andy D July 23, 2013

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.

Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 23, 2013

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

Andy D July 24, 2013

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;

-->

Phillip Ponzer [Cprime]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 24, 2013

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.

Andy D July 25, 2013

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;

-->

0 votes
Sahil Arora May 7, 2017
0 votes
EL October 20, 2013

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?

David _old account_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 21, 2013

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;

Suggest an answer

Log in or Sign up to answer