Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Get Jira Multi Select Options

Hemanshu Sood
Contributor
June 18, 2018

Hi,

I have 2 custom fields A and B.

A is a multi-select field

I want to write a behavior script(for create issue screen) according to which if a user selects any option(out of A) which contains a String "XXX" then field B is set to hidden. I tried below script but it did not work:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor

FormField Impact = getFieldById ("customfield_18750") // 

FormField bim = getFieldById ("customfield_11251")// 
log.debug("--- Entering3 ---")


def selectedOption = Impact.getValue() as String
if ((selectedOption.contains("UK")))
{

log.debug("--- Entering If ---")
bim.setHidden(true)
}

else
{
log.debug("--- Entering Else ---")
bim.setHidden(false)
}

 

Please assist

2 answers

0 votes
Mark Markov
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.
June 18, 2018

Hello @Hemanshu Sood

I check your code and it works pretty well.

Where are you place this script? It must be mapped to your multi-select field

Hemanshu Sood
Contributor
June 18, 2018

Hi Mark, as mentioned above, I am using it as a behavior script on create issue screen. It is mapped to multi select field in the behavior

Mark Markov
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.
June 18, 2018

Hmmm, it seems like you use GUI condition to run it on create screen. Sometimes it works really bad, try to remove that condition and make it in code, like this:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

if (getActionName() == "Create") {
FormField Impact = getFieldById("customfield_18750") //

FormField bim = getFieldById("customfield_11251")//
log.debug("--- Entering3 ---")


def selectedOption = Impact.getValue() as String
if ((selectedOption.contains("UK"))) {

log.debug("--- Entering If ---")
bim.setHidden(true)
} else {
log.debug("--- Entering Else ---")
bim.setHidden(false)
}

 

Hemanshu Sood
Contributor
June 18, 2018

Hi mark,

It is entering the script but giving the below error in logs:

 


java.lang.NullPointerException: Cannot invoke method contains() on null object
at Script1.run(Script1.groovy:14)

Mark Markov
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.
June 18, 2018

It seems your Impact field has no value, so we need to check that in code.

If code now gives you error, its good sign. It finally start to work :)

try this:

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

if (getActionName() == "Create") {
FormField Impact = getFieldById("customfield_18750") //

FormField bim = getFieldById("customfield_11251")//
log.debug("--- Entering3 ---")


def selectedOption = Impact.getValue() as String
if (selectedOption && selectedOption.contains("UK")) {

log.debug("--- Entering If ---")
bim.setHidden(true)
} else {
log.debug("--- Entering Else ---")
bim.setHidden(false)
}
}
Hemanshu Sood
Contributor
June 18, 2018

Yup, still giving error , I selected 2 options in the create issue screen for the Multi select field, giving the below error:

s/1.0/behaviours/validatorsByPid.json [jira.groovy.user.FieldBehaviours] --- Entering3 ---
2018-06-18 09:07:24,133 http-bio-8080-exec-14 DEBUG soodhema 547x2541x1 7xy39s 10.99.242.218 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validatorsByPid.json [jira.groovy.user.FieldBehaviours] --- Entering Else ---

Mark Markov
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.
June 18, 2018

I dont see any error, this is you debug messages.

Hemanshu Sood
Contributor
June 18, 2018

The script is just not working then, even after selecting 2 options,(one of them contains UK), this script is not able to hide the custom field

Mark Markov
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.
June 18, 2018

It seems somewhere we made a mistake, to find out where let's, add some debug messages to see what exactly happens when our code runs

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

log.debug("You are on Action Name: "+ getActionName())
if (getActionName() == "Create") {
FormField Impact = getFieldById("customfield_18750") //
log.error("FormField 1: "+ Impact)
FormField bim = getFieldById("customfield_11251")//
log.debug("Formfield 2: "+ bim)
log.debug("--- Entering3 ---")


def selectedOption = Impact.getValue() as String
log.debug("Selected option: "+ selectedOption)
log.debug("Pass condition?: "+ selectedOption && selectedOption.contains("UK"))
if (selectedOption && selectedOption.contains("UK")) {

log.debug("--- Entering If ---")
bim.setHidden(true)
} else {
log.debug("--- Entering Else ---")
bim.setHidden(false)
}
}
Hemanshu Sood
Contributor
June 18, 2018

Selected option: null

this is what I am getting , the code seems fine, but as Nic said above it is multi select field , 

hence when I select more than 1 option in the field, how can I get it as String, or how to get both the values and check if condition on them

Mark Markov
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.
June 18, 2018

Whats happen(logs) when you choose values in your multiselect.

Script runs every time when change data in field

Yes, as Nic said, select list its a list object, but when you cast it as String it became String object

Hemanshu Sood
Contributor
June 18, 2018

FormField 1: Form field ID: customfield_18750, value: null

 Formfield 2: Form field ID: customfield_11251, value: null

 --- Entering3 ---

Selected option: null

 

This is what comes in logs

Mark Markov
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.
June 18, 2018

No matter what value you choose in select list? Please check that is id is correct for your multiselect field.

0 votes
Nic Brough -Adaptavist-
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.
June 18, 2018

Multi-selects contain arrays of options, so I suspect your cast of the value to a string is letting you down.

Hemanshu Sood
Contributor
June 18, 2018

Hi Nick, can you correct this script then? Still not able to make it work.

Using it as behavior script on create issue screen

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events