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

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

This widget could not be displayed.

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

This widget could not be displayed.

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.

 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

534 views 3 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you