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
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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 ---
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I dont see any error, this is you debug messages.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No matter what value you choose in select list? Please check that is id is correct for your multiselect field.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Multi-selects contain arrays of options, so I suspect your cast of the value to a string is letting you down.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Nick, can you correct this script then? Still not able to make it work.
Using it as behavior script on create issue screen
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.