how to set a field name with two checkboxes required?

yueping tian June 18, 2023

I have a field named Defect Cause, it has two checkboxes.  I set "Default Cause" required. but it just worked for the first checkbox,   Can this be done using scriptrunner? 

 

1.PNG

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.
June 18, 2023

Hi @yueping tian

From the screenshot you have shared, it appears you are working with a Cascading Select-List, and not two checkboxes.

If the Cascading Select List is set to required, the priority is given to only the Parent List. The Child List is not mandatory. This is an expected Behaviour.

I suggest referring to this Adaptavist Library example code, where both the parent and child list values are set.

You will need to play around with the code, i.e. include if/else conditions to set the value of the child list depending on the option selected from the parent list.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

yueping tian June 19, 2023

@Ram Kumar Aravindakshan _Adaptavist_ yes, it a  Cascading Select-List. The problem now is, I don't know how to write the second select list in scriptrunner.    Then it can be marked as mandatory with .setRequired()。 Thanks

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.
June 19, 2023

Hi @yueping tian

As I have already mentioned in my previous comment:

If the Cascading Select List is set to required, the priority is given to only the Parent List. The Child List is not mandatory. This is an expected Behaviour.

It is not possible to make the Child List mandatory using the Behaviour. This is because the setRequired() function only works for the parent list in the Cascading List. So it is pointless to try this approach.

The alternative you could try is to add a transition validator on the Create transition, as I have suggested using the example from the Adaptavist Library sample code.

The second alternative I can suggest is, i.e. if you want to do this via Behaviour, not to use a Single Cascading List and instead two separate Single Select Lists.

Whereby, upon making a selection from the first list, you set the second list as required depending on the option selected from the first list etc.

For this approach, you will need to use the Server-Side Behaviour for the first list i.e. something like:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours
// First List
def list1 = getFieldById(fieldChanged)
def list1Value = list1.value.toString()

//Second List

def list2 = getFieldByName('List2')
list2.required = false

def optionManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def list2CustomField = customFieldManager.getCustomFieldObject(list2.fieldId)
def list2Config = list2CustomField.getRelevantConfig(issueContext)
def list2Options = optionManager.getOptions(list2Config)

def listBNewOptions = ['Option1', 'Option2', 'Option3', 'Option4', 'Option5']

if(list1Value == 'Zone1') {
listBNewOptions = ['Option1', 'Option2']
list2.required = true
} else if(list1Value == 'Zone2') {
listBNewOptions = ['Option3', 'Option4']
list2.required = true
} else if(list1Value == "Zone3") {
listBNewOptions = ['Option5']
list2.required = true
}

def listBNewOptionsMap = list2Options?.findAll {
it.value in listBNewOptions
}

list2.setFieldOptions(listBNewOptionsMap)

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

Below is a screenshot of the Behaviour configuration for your reference:-

behaviour_config.png

I hope this helps to answer your question.

Thank you and Kind regards,

Ram

yueping tian June 19, 2023

hi , @Ram Kumar Aravindakshan _Adaptavist_  I got a answer, it worked .

def cfield = getFieldByName("Defect Cause")
ArrayList arrayList = cfield.getValue() as ArrayList

if(arrayList){
    if (arrayList?.size() != 2){
        cfield.setError("Please fill in both cascading field values")
    }else{
        cfield.clearError()
    }
}
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.
June 19, 2023

Hi @yueping tian

Yes you can use this approach. 

This uses the validator to check if the child field has been filled or not.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events