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

Jason Li Ting Chung September 20, 2022

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

0 votes
Ram Kumar Aravindakshan _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.
September 25, 2022

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