which custom field type can be used as an input for a numeric calculation?

I have a scripted field that calculates the product of 3 custom number fields. This works fine but I really need to restrict the input to the 3 number fields to only allow a 1,2, or 3 to be entered. I've tried radio buttons and select list but they aren't treated as numbers so then my scripted field doesn't work.

Can anyone suggest how I can restrict the input and have them be recognized as numbers?

Here is my current script:

def Risk_Severity = getCustomFieldValue("Risk Severity")

def Risk_Probability = getCustomFieldValue("Risk Probability")

def Risk_Detectability = getCustomFieldValue("Risk Detectability")

if (Risk_Severity) {

return (Risk_Severity * Risk_Probability) * Risk_Detectability

}

else {

return null

}

Thank you,
Jeannette

3 answers

0 vote

As Nic says, toInteger() is fine, or

x as Integer

However I would probably keep them as numeric fields, and just have a validator that the number is an integer between and including 1 and 3.

That will let you do aggregate calculations on the values using script runner, or in jql if it supports it one day.

0 vote

There aren't any numeric fields that will work that way.

Your best bet really is your select or radio button approach, coupled with the script runner being a bit more clever - it can convert the numbers displayed to actual numbers.

If my memory is working today, it's something like value.toInteger(), so you could write return (Risk_Severity.toInteger() * Risk_Probability.toInteger()) * Risk_Detectability.toInteger()

This sounds like a good idea but I'm not getting the syntax right. I changed one of my fields to a select list. I've tried the following script and keep getting an error on the first line.

import com.atlassian.jira.issue.customfields.option.LazyLoadedOption.toNumber();

def Risk_Severity = getCustomFieldValue("Risk Severity - Select")

def Risk_Probability = getCustomFieldValue("Risk Probability")

def Risk_Detectability = getCustomFieldValue("Risk Detectability")

if (Risk_Severity) {

return (Risk_Severity.toInteger() * Risk_Probability) * Risk_Detectability

}

else {

return null

}

Try removing your import and:

def Risk_Severity = getCustomFieldValue("Risk Severity - Select").value as Integer

0 vote

Hi jnet,

in order to implement that feature you need to use Behaviour Custom Field too.

Your scripted field should be :

import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
 
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
Issue issue  = issue;

CustomField Risk_Severity = getCustomFieldValue("Risk Severity - Select");
Integer value_Risk_Severity = issue.getCustomFieldValue(Risk_Severity);
CustomField Risk_Probability = getCustomFieldValue("Risk Probability");
Integer value_Risk_Probability = issue.getCustomFieldValue(Risk_Probability);
CustomField Risk_Detectability = getCustomFieldValue("Risk Detectability");
Integer value_Risk_Detectability = issue.getCustomFieldValue(Risk_Detectability);
if(value_Risk_Severity && value_Risk_Probability && value_Risk_Detectability){
return value_Risk_Severity*value_Risk_Probabilityvalue_Risk_Detectability;
}
return null;

For each numeric custom field, you need to add the following server side script :

FormField aFF = getFieldByName("Your Custom Field Name")

def a = aFF.getValue()

if(!"1".equals(a) && !"2".equals(a) && !"3".equals(a)){

aFF.setError("Invalid number. Please, specify 1,2 or 3")

} else {

aFF.clearError()

}

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 Thursday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

177 views 0 2
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