Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scripted field- Check box field converted to numeric field

Duane Cronkite
Contributor
May 22, 2024

Hello Everyone!

I have a scenario where I would like to convert the data in a checkbox field to a numeric field. For example :

Checkbox field: Option A, Option B

I would like to assign a numeric value to the options: Option A = 5pts, Option B = 10pts

The scripted field would show the total.

So if both options were checked the scripted field would show 15.

 

Is this possible?

 

 

 

1 answer

0 votes
Matt Gudenas May 23, 2024

Hi @Duane Cronkite! Yes, it is possible with ScriptRunner. You can use the Fields functionality to create a custom field that matches your requirements. Here is an example script that you can use to get started:

import com.atlassian.jira.component.ComponentAccessor

// The issue type for which we want the scripted field to be displayed

final issueTypeName = 'Story'

// Replace with the field name that you want to run this against

final multipleChoiceFieldName = 'Multiple choice'

if (issue.issueType.name != issueTypeName) {

return null

}

def numberOfUsersField = ComponentAccessor.customFieldManager.customFieldObjects.findByName(multipleChoiceFieldName);

if (!numberOfUsersField) {

log.debug "Custom field is not configured for that context"

return null

}

def val = (List) numberOfUsersField.getValue(issue)

// Replace the options with whatever the checkbox options are

def sum = switch(val[0]) {

case 'Option A': yield 10

case 'Option B': yield 5

default: yield 0

}

return sum
Some documentation about these fields can be found here:
Do let me know if you have any further questions.


Duane Cronkite
Contributor
May 23, 2024

Thanks for the response!

 

I tried to run it and got no result. Would a checkbox field be different than a "multiplechoice" field?

Duane Cronkite
Contributor
May 23, 2024

For more infomation, it returns 0. The field type is "checkboxes".

Matt Gudenas May 23, 2024

The field type shouldn't matter here - are you sure that the options and the field names match exactly?

Try putting some log.debug statements after each line to see where it breaks.

Duane Cronkite
Contributor
May 23, 2024

I ran it through chatGPT and this made it work. Does this make sense?

 

 

import com.atlassian.jira.component.ComponentAccessor

// The issue type for which we want the scripted field to be displayed
final issueTypeName = 'Story'

// Replace with the field name that you want to run this against
final multipleChoiceFieldName = 'Multiple choice'

if (issue.issueType.name != issueTypeName) {
return null
}

// Fetch the custom field object by name
def multipleChoiceField = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).findByName(multipleChoiceFieldName)

if (!multipleChoiceField) {
log.debug "Custom field is not configured for that context"
return null
}

// Get the value of the multiple choice field (checkboxes)
def val = multipleChoiceField.getValue(issue)

if (!val || !(val instanceof Collection)) {
return null
}

// Replace the options with whatever the checkbox options are
def sum = val.collect { option ->
switch(option.value) {
case 'Option A': return 10
case 'Option B': return 5
default: return 0
}
}.sum()

return sum

Matt Gudenas May 23, 2024

Interesting - I suppose you have a slightly different use case to what I thought. Nonetheless, I am glad it works for you now!

 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events