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
All I'm saying is to go back to your original implementation and ditch the check for if cf1.toString().isEmpty().
i.e.
<!-- @@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 && !cf8.toString().equals( "RED" ) ) { number = ((cf1 != null ? Integer.parseInt(cf1.toString()) : 0 ) * 45) + ((cf2 != null ? Integer.parseInt(cf2.toString()) : 0 ) * 32); } return number; -->
<!-- @@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 && !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; -->
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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"
Calc 2 if cf4 <> "RED"
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
-->
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
-->
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey guys, can anyone help on this one?
https://community.atlassian.com/t5/JIRA-questions/Using-calculated-number-field/qaq-p/584214
Thanks!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.