Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,111
Community Members
 
Community Events
168
Community Groups

How to populate a custom field depending on the multi selection of another checkbox CT

Hi Team,


I have the following case scenario.

A custom field "Attributes" has 12 options.
For each option, a custom field "Firelight Dev hours attributes" is populated with a value.
But since the custom field "Attributes" is a checkbox and multiple selection can be done, for each selection, the value that need to be populated will be added for each selection.
For example,
Consider the following options in the custom field "Attributes":
- Option A ----> Custom field "Firelight Dev hours attributes" will be populated with value "5"
- Option B ----> Custom field "Firelight Dev hours attributes" will be populated with value "10"
- Option C ----> Custom field "Firelight Dev hours attributes" will be populated with value "18"

If Option A and Option B is selected, The custom field "Firelight Dev hours attributes" will be populated with the sum of "5" and "10".

Now we can select the following options for this example:
A
B
C
A and B
B and C
A and C
A, B, and C

But we have 12 options with all the combinations.

Is there a script in scriptrunner server that can chose all the possible options and return the possible value to be populated?

Thank you in advance for your support.

 

1 answer

Hi @Jason Li Ting Chung

For your requirement, you could try something like this:-

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def checkBox = getFieldById(fieldChanged)
def checkBoxValue = checkBox.value

def selectedOptions = [] as ArrayList<String>

def sampleTextField = getFieldByName('Sample Number Field')

def total

if (checkBoxValue in String) {
selectedOptions.add(checkBoxValue)
} else if (checkBoxValue in ArrayList) {
selectedOptions.addAll(checkBoxValue)
}

if (selectedOptions == ['Option 1']) {
total = 1
} else if (selectedOptions == ['Option 2']) {
total = 2
} else if (selectedOptions == ['Option 3']) {
total = 3
} else if (selectedOptions == ['Option 4']) {
total = 4
} else if (selectedOptions == ['Option 1', 'Option 2']) {
total = 1 + 2
} else if (selectedOptions == ['Option 1', 'Option 3']) {
total = 1 + 3
} else if (selectedOptions == ['Option 1', 'Option 4']) {
total = 1 + 4
} else if (selectedOptions == ['Option 2', 'Option 3']) {
total = 2 + 3
} else if (selectedOptions == ['Option 2', 'Option 4']) {
total = 2 + 4
} else if (selectedOptions == ['Option 3', 'Option 4']) {
total = 3 + 4
} else if (selectedOptions == ['Option 1', 'Option 2', 'Option 3']) {
total = 1 + 2 + 3
} else if (selectedOptions == ['Option 1', 'Option 2', 'Option 4']) {
total = 1 + 2 + 4
} else if (selectedOptions == ['Option 1', 'Option 3', 'Option 4']) {
total = 1 + 3 + 4
} else if (selectedOptions == ['Option 2', 'Option 3', 'Option 4']) {
total = 2 + 3 + 4
}

sampleTextField.setFormValue(total)

Please note that the sample code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

I hope this helps to solve your question. :)

Thank you and Kind regards,

Ram 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events