Check the selection of a multi select custom field for specific values

Deleted user March 1, 2016

Hello Guys,

Could you please help me to validate the values selected in a multi select custom field of Jira.

In the multi select custom field 'cf1', if the user selects any field other that "cf1_value_1", then it should check whether another field 'cf2' selected is None, and should not allow to create the issue if 'cf2' is empty.

cf1 is a multi-select and cf2 is a single select.

It doesn't matter if "cf1_value_1" is selected, but if the selection contains any other values then it should trigger the validation.

I tried the below, but dint work.

 \{

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import java.sql.Timestamp;
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option

Issue issue = issue

def platform = customFieldManager.getCustomFieldObject("customfield_1")

def migrate = customFieldManager.getCustomFieldObject("customfield_2")

def migrateValue = issue.getCustomFieldValue(migrate).getValue()

def platformValue = issue.getCustomFieldValue(platform).getValue()

 

if (platformValue.containsAll(['cf1_value_2','cf1_value_3,'cf1_value_4,'cf1_value_5]))
{
if (migrateValue == 'None')
{
throw new InvalidInputException("Please update the AWS Migration plan")
}
else {
return
}

}
else {
return
}

\}

Thanks,

Fahad

2 answers

1 accepted

5 votes
Answer accepted
Kristian Walker _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.
March 1, 2016

Hi Fahad,

You could use the Behaviours plugin in order to make the Text Field (cf2) required when the first option is selected in the Multi Select Field (cf1).

I have attached a sample script below to show how you could do this.

import com.atlassian.jira.component.ComponentAccessor

// Get a pointer to my custom fields
def multisSelectList = getFieldByName("DemoMultiSelect")
def textField = getFieldByName("DemoTextField")

// Get the Value of My Select Field
def selectVal = multisSelectList.getValue()

// If the first option is selected in the multi select list then make the text field required.
if(selectVal.toString().contains("1")){
    textField.setRequired(true) // Make text field required
}else{
    textField.setRequired(false) // Make text field optional
}

Please note that behaviours works on the create and edit screens for issues.

I hope this helps.

Thanks

Kristian

Tai Lam Phat September 19, 2019

Thank you so much

1 vote
JamieA
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 1, 2016

Please use the \{code\} macro, your code is unreadable. Also it's not even valid.

If they are multiselects, getCustomFieldValue will return a List<Option>, so you can't call getValue() on that. Please check the examples at https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/validators/simple-scripted-validators.html#_validating_a_multi_select_option_is_selected.

 

Sarath October 15, 2020

I have a similar requirement.

Instead of making text field required/not-required but to make it hide/unhide.

My field "Field A" is a multi-select field with options A,B,C,Other. First I converted this field into multi select dropdown from check-box type by using below line in initializer (Behaviors).

getFieldByName("Field A").convertToMultiSelect()

After that, I used the below code in server-side script by adding a field. There are no errors.

import com.atlassian.jira.component.ComponentAccessor

// Get a pointer to my custom fields
def multisSelectList = getFieldByName("Field A")
def textField = getFieldByName("Other")

// Get the Value of My Select Field
def selectVal = multisSelectList.getValue()

// If the first option is selected in the multi select list then make the text field required.
if(selectVal.toString().contains("Other")){
textField.setHidden(false) // Make text field required
}else{
textField.setHidden(true) // Make text field optional
}

But when I

1. select only "Other" value in Field A, text field appears BUT after selecting Other value and then remove it, the text field still appears.
2. first select values A/B/C and then select "Other", text field appears. Again when I remove "Other" value after selecting it, it won't hide.

And none of the above scenarios are applicable if do not convert the field from checkbox multi select to dropdown multi select. The field behavior is perfect. 

Any help appreciated

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events