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?
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
Thanks for the response!
I tried to run it and got no result. Would a checkbox field be different than a "multiplechoice" field?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For more infomation, it returns 0. The field type is "checkboxes".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Interesting - I suppose you have a slightly different use case to what I thought. Nonetheless, I am glad it works for you now!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.