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

Jeannette Lamb March 30, 2014

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 votes
Fabio Racobaldo _Herzum_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 30, 2014

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()

}

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 30, 2014

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()

Jeannette Lamb March 30, 2014

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

}

JamieA
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.
March 31, 2014

Try removing your import and:

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

0 votes
JamieA
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.
March 30, 2014

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.

Suggest an answer

Log in or Sign up to answer