ScriptRunner Behaviours: change select list based on customfield value

bSte May 3, 2018

Hello - I am trying to show different select list options depending on the custom field value selected.  My script is not working, below is the script I am using.  

 

import com.atlassian.jira.component.ComponentAccessor

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def vertical = getFieldById("customField_10433")
def verticalValue = vertical.getValue()

if (vertical == "Retail"){
def retailOptions = optionsManager.getOptions(fieldConfig){
it.name in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker"]
}
else {
def autoOptions = optionsManager.getOptions(fieldConfig){
it.name in ["chevy", "ford", "buick"]
}
getFieldbyId("customfield_12711").setFieldOptions(allowedOptions)
}
}

5 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
JohnsonHoward
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.
May 4, 2018

Hi Ashlee,

There are a few reasons why this script is not working:

1. Your curly braces are misplaced ( these ones -> {} ).

2. You haven't got the field config for the options, so your 'fieldConfig' value that you have passsed to getOptions() will be null.

3. You need to add .FindAll() between your closure and getOptions(method), as you are currently checking for a boolean. 

4. You have not assigned anything to the 'allowedOptions' value you are passing to .setFieldOptions() method.

Other tips are:

1. Get the field by name instead of ID, as this accounts for system migrations

2. toString() the vertical value in the if statement so that you are always comparing two of the same object types

The script below has corrected all the issues. It is untested but I believe it should work. Let me know if you have more issues.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
Issue issue = issue
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def otherField = getFieldByName("other field")
def otherFieldCustomField = customFieldManager.getCustomFieldObjectByName("other field")
def verticalValue = vertical.getValue()
def fieldConfig = otherFieldCustomField.getRelevantConfig(issue)
def allowedOptions = null

if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll{
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker"]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll{
it.value in ["chevy", "ford", "buick"]
}

}
otherField.setFieldOptions(allowedOptions)

Thanks

Johnson Howard (Adaptavist)  

bSte May 4, 2018

Thanks Johnson, I am still getting a few errors on the actual script and when I run it this is the main error:

java.lang.NullPointerException: Cannot invoke method getFieldIdByName() on null object at com.onresolve.jira.groovy.user.FieldBehaviours.getFieldByName(FieldBehaviours.groovy:69) at Script1025.run(Script1025.groovy:16)

bSte May 4, 2018
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def vertical = getFieldByName("vertical")
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("type of engagement")
def verticalValue = vertical.getValue()
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issue)
def allowedOptions = null


if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker",]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Proactive Audience", "Outreach", "Campaign Tracker"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
JohnsonHoward
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.
May 4, 2018

Are you putting this in as a behaviour?

What do you mean when you are say that you 'run' it?

bSte May 4, 2018

I have tried it as both a behaviour and in scriptrunner. The run it was in reference to scriptrunner.

As a behaviour - using a server side script, I get an error on Issue issue = issue stating that issue is undeclared. And an error on the very bottom line: typeOfEngagement.setFieldOptions(allowedOptions) that reads: cannot find matching method

JohnsonHoward
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.
May 4, 2018

Try this as a behaviour mapped to the Vertical Field (Ignore the error on the bottom line, it won't effect the script) : 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def vertical = getFieldByName("vertical")
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("type of engagement")
def verticalValue = vertical.getValue()
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null


if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker",]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Proactive Audience", "Outreach", "Campaign Tracker"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)

  

Praveen
Contributor
May 6, 2018

Hi Ashlee,

You have to declare issue as

def issue =  getUnderlyingIssue();
bSte May 7, 2018

I am still getting errors on the: 

def verticalValue = issue.getValue(vertical) 

error is Cannot find matching method 

 

AND 

typeOfEngagement.setFieldOptions(allowedOptions)

error is cannot find matching method

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def issue = getUnderlyingIssue();
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def vertical = getFieldById("customfield_10433")
def typeOfEngagement = getFieldById("customfield_12711")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of engagement")
def verticalValue = issue.getValue(vertical)
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issue)
def allowedOptions = null


if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker",]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Proactive Audience", "Outreach", "Campaign Tracker"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
JohnsonHoward
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.
May 8, 2018

Ashlee,

The getValue method is not applicable to the Issue object. It is applicable for the FormField object. I have made a couple of changes, the code below has no errors when using an IDE that has the Groovy SDK installed. I have not ran it as I don't know your full configuration. Put this as a behaviour.

I don't know why you removed the getIssueContext() method that I put in? The way you are doing it would mean that there would always have to be an issue underneath the dialog that matched the one you were create/editing. I assume this is not the case, hence why I changed this to getIssueContext() not just issue (line 23).

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def issue = getUnderlyingIssue();
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

FormField vertical = getFieldById("customfield_10433")
FormField typeOfEngagement = getFieldById("customfield_12711")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of engagement")
def verticalValue = vertical.getValue()
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null


if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker",]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Proactive Audience", "Outreach", "Campaign Tracker"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
Vasanta Kumaar
Contributor
May 14, 2020

Hi JohnsonHoward,

 

I have tried the above script in Behavior. I have modified the script according to my requirement.

My actual requirement is need to change select list based on the Component/s field value.

We are using JIRA server with version v8.2.4.

I am getting error "unable to resolve class com.atlassian.jira.ComponentManager"

Can you please help me to sort out the issue.

Below is the code I modified according to my requirement.

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def issue = getUnderlyingIssue();
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

FormField component = getFieldById("Component/s")
FormField applicationx = getFieldById("customfield_17357")
def applicationxCustomField = customFieldManager.getCustomFieldObjectByName("Applicationx")
def componentvalue = component.getValue()
def fieldConfig = applicationxtCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null

if (componentValue.toString() == "AO Docs") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["On/Off Boarding", "PPWA", "LRM", "Email", "Libraries/Generic"]
}
}
else if (componentValue.toString() == "Zucchetti") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["HR Infinity", "Mobile App", "Asset Management"]
}
}
else if (componentValue.toString() == "LMS") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Discovery", "Elucidat", "Userlane"]
}
}
else if (componentValue.toString() == "Others") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Workplace", "Org. Chart"]
}
}
else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["On/Off Boarding", "PPWA", "LRM", "Email", "Libraries/Generic", "HR Infinity", "Mobile App", "Asset Management", "Discovery", "Elucidat", "Userlane", "Workplace", "Org. Chart"]
}
}
applicationx.setFieldOptions(allowedOptions)
Vasanta Kumaar
Contributor
May 14, 2020

Hi Johnson,

 

I have a requirement of display only options in single select drop down field based on the value selected in Component/s Field.

I came across the script you have mentioned and modified according to my requirement.

But unfortunately I am getting an error stating "unable to resolve class com.atlassian.jira.ComponentManager".

 

We are currently using JIRA server v8.2.4

Below code is modified according to my requirement. 

 

Can you help me to fix the issue I am facing,

 

Thanks,

Vasanta

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

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

@BaseScript FieldBehaviours fieldBehaviours

def issue = getUnderlyingIssue();
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

FormField component = getFieldById("Component/s")
FormField applicationx = getFieldById("customfield_17357")
def applicationxCustomField = customFieldManager.getCustomFieldObjectByName("Applicationx")
def componentvalue = component.getValue()
def fieldConfig = applicationxtCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null

if (componentValue.toString() == "AO Docs") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["On/Off Boarding", "PPWA", "LRM", "Email", "Libraries/Generic"]
}
}
else if (componentValue.toString() == "Zucchetti") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["HR Infinity", "Mobile App", "Asset Management"]
}
}
else if (componentValue.toString() == "LMS") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Discovery", "Elucidat", "Userlane"]
}
}
else if (componentValue.toString() == "Others") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Workplace", "Org. Chart"]
}
}
else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["On/Off Boarding", "PPWA", "LRM", "Email", "Libraries/Generic", "HR Infinity", "Mobile App", "Asset Management", "Discovery", "Elucidat", "Userlane", "Workplace", "Org. Chart"]
}
}
applicationx.setFieldOptions(allowedOptions)

 

JohnsonHoward
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.
May 19, 2020

Hi Vasanta,

You should just be able to remove this line:

import com.atlassian.jira.ComponentManager

Then it should work.

Thanks

Johnson 

Like Vasanta Kumaar likes this
Vasanta Kumaar
Contributor
May 19, 2020

Hi, Johnson,

 

I have removed the line as you suggested but now I am receiving a error in the above code, below is the screen shot.

Can you suggest How to resolve the issue.

Thanks,

Vasanta 

capture.png

JohnsonHoward
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.
May 19, 2020

I have just pasted this into my Jira console and go no errors: 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def issue = getUnderlyingIssue();
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

FormField component = getFieldById("Component/s")
FormField applicationx = getFieldById("customfield_17357")
def applicationxCustomField = customFieldManager.getCustomFieldObjectByName("Applicationx")
def componentValue = component.getValue()
def fieldConfig = applicationxCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null

if (componentValue.toString() == "AO Docs") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["On/Off Boarding", "PPWA", "LRM", "Email", "Libraries/Generic"]
}
}
else if (componentValue.toString() == "Zucchetti") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["HR Infinity", "Mobile App", "Asset Management"]
}
}
else if (componentValue.toString() == "LMS") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Discovery", "Elucidat", "Userlane"]
}
}
else if (componentValue.toString() == "Others") {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Workplace", "Org. Chart"]
}
}
else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["On/Off Boarding", "PPWA", "LRM", "Email", "Libraries/Generic", "HR Infinity", "Mobile App", "Asset Management", "Discovery", "Elucidat", "Userlane", "Workplace", "Org. Chart"]
}
}
applicationx.setFieldOptions(allowedOptions)

Vasanta Kumaar
Contributor
May 19, 2020

Hi Johnson, 

 

I used the above script in Behaviours. Now I am not getting any errors and getting only a warning message.  "Use CustomFieldManager.getCustomFieldObjectByName("string") instead. line 19 & line 31".

I have configured the script under Behaviour settings under fields Component/s.

I tried to check after saving it but if I select the option in Component/s field and when I check the drop down field applicationx I can see all the list. 

What can be the issue or how to use it with Adaptive script runner to work.

I am New to JIRA, so can you please help me out.

 

Thanks,

Vasanta

JohnsonHoward
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.
May 19, 2020

It could be that you are requesting the components/ custom field by id but giving it a name instead. Like so: 

 

FormField component = getFieldById("Component/s")
FormField applicationx = getFieldById("customfield_17357")

 

See my example below:

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def issue = getUnderlyingIssue();
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

FormField vertical = getFieldById("customfield_10433")
FormField typeOfEngagement = getFieldById("customfield_12711")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of engagement")
def verticalValue = vertical.getValue()
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(getIssueContext())
def allowedOptions = null


if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker",]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Proactive Audience", "Outreach", "Campaign Tracker"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
0 votes
SCK October 8, 2021

@JohnsonHoward how to find it.value NOT in? in a collection. or with in find all how to set not to show none

0 votes
Praveen
Contributor
May 7, 2018

Hi Ashlee,

The code used for getting the custom field value is wrong.

Try the below code

def cf = customFieldManager.getCustomFieldObjectById("customfield_10433")
def verticalValue =issue.getCustomFieldValue(cf)
JohnsonHoward
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.
May 8, 2018

Praveen,

The code you have suggested above will only work if the issue already exists and if the vertical custom field already has a value. So your suggestion wouldn't work if this script is to run when creating an issue. I assume that Ashlee wants to get the value of the vertical custom field when the user interacts with the custom field on the form. Therefore you need to use :

def vertical = getFieldById("customfield_10433")
def verticalValue = vertical.getValue()
Praveen
Contributor
May 8, 2018

Hi Johnson,

Thanks for pointing it out. I stand corrected here!

JohnsonHoward
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.
May 8, 2018

No problem Praveen. It's something that always trips people up.

bSte May 14, 2018

Hi Johnson,

I am still getting errors on the if else statement, more specifically it says allowedOptions is undeclared  and the variable field config is undeclared when passed as argument after optionsManager.getOptions(fieldConfig).findAll

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def vertticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def optionAuto = ["Proactive Audience", "Outreach", "Campaign Tracker"]
def optionRetail = ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker"]

if (vertical.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findall {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
JohnsonHoward
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.
May 14, 2018

Hi Ashlee,

It seems again that you have removed a critical part of the code that I shared with you.

You need to add this into your variables:

def allowedOptions = null

Try this and let me know

Thanks
Johnson Howard (Adaptavist) 

bSte May 14, 2018

Hopefully almost there, now I am getting an error on the very last line: typeOfEngagement.setFieldOptions(allowedOptions) - the errors says: cannot find matching method setFieldOptions

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def fieldConfig = CustomField.getRelevantConfig(issueContext)
def vertical = getFieldByName("vertical")
def vertticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def optionAuto = ["Proactive Audience", "Outreach", "Campaign Tracker"]
def optionRetail = ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker"]

if (vertical.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll() {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll() {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
JohnsonHoward
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.
May 15, 2018

Hi Ashlee,

Some other things that you have changed will cause this script not to work.
You have CustomField.geRelevantConfig(issueContext) - this is incorrect, you need to get the config the custom field that you are trying to set the options which is Type of Engagement. 

Also you have vertical.toString() instead of verticalValue.toString(), so you are converting the object to a string not the value.

Try this: 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def typeofEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def fieldConfig = typeofEngagementCustomField.getRelevantConfig(issueContext)
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")

if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll() {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll() {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
}
}
typeOfEngagement.setFieldOptions(allowedOptions)
bSte May 15, 2018

Hi Johnson, 

Great, so no errors but the code isn't working.  I have it as a server side script and have tried it on both the Type of Engagement Field and the Vertical field...any thoughts?

 

import com.atlassian.jira.ComponentAccessor
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def vertical = getFieldByName("vertical")
def verticalValue = vertical.getValue()
def typeOfEngagement = getFieldByName("type of engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def optionAuto = ["Proactive Audience", "Outreach", "Campaign Tracker"]
def optionRetail = ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker"]


if (verticalValue.toString() == "Retail"){
allowedOptions = optionsManager.getOptions(fieldConfig).findAll() {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker", "Email"]
}
}else {
allowedOptions = optionsManager.getOptions(fieldConfig).findAll() {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
}
}
JohnsonHoward
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.
May 15, 2018

Why has this line been removed?

typeOfEngagement.setFieldOptions(allowedOptions)
bSte May 15, 2018

I am still getting an error on that: cannot find matching method

JohnsonHoward
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.
May 16, 2018

You can ignore that error as it shouldnt have an effect on runtime.

But if you really want it to go away you can do this at the bottom:


if(allowedOptions){
typeOfEngagement.setFieldOptions(allowedOptions as List<Option>)
}
bSte May 16, 2018

Hi Johnson - so the code below works but for some reason it doesn't pull the list in the order listed...any ideas?

 

import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def verticalCustomField = getFieldByName("Vertical")
def verticalValue = verticalCustomField.getValue()
def typeOfEngagement = getFieldByName("Type of Engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (verticalValue.toString() == "Retail"){
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Email", "Audience Workshop", "Audience Plan", "Media Plan", "RFP", "Proactive Audience Outreach", "Campaign Tracker"]
})
}else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Demographic Profiling", "Audience Matrix", "RFP", "Email"]
})
}
JohnsonHoward
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.
May 16, 2018

You could trying using .sort() to help sort your options.

If the code is working could you please mark one of my above answers as accepted.

bSte May 16, 2018

Johnson - one last question - I am trying to list multiple fields either either with additional if or &&. They are not dependent on one another but they are all dependent on the variable field.  See code below: 

 

import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def campaignRetailOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def verticalCustomField = getFieldByName("Vertical")
def verticalValue = verticalCustomField.getValue()
def typeOfEngagement = getFieldByName("Type of Engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def campaignType = getFieldByName("Campaign Type")
def campaignTypeCustomField = customFieldManager.getCustomFieldObjectByName("Campaign Type")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def customFieldCRO = customFieldManager.getCustomFieldObjectByName("Campaign Type")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (verticalValue.toString() == "Retail"){
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Audience Workshop", "Campaign Tracker", "Email", "Media Plan", "Proactive Audience Outreach", "RFP"]}
if (verticalValue.toString() == "Retail") {
campaignRetailOptions = campaignType.setFieldOptions(options.findAll {
it.value in ["Awareness", "New Store Opening", "Sales Event", "In Market", "Prospecting", "Retention"]
}
})
}else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Audience Matrix", "Demographic Profiling", "Email", "RFP"]}) && campaignRetailOptions = campaignType.setFieldOptions(optionsCRO.findAll {
it.value in ["Awareness", "Launch", "Sales Event", "In Market"]
})
}
JohnsonHoward
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.
May 17, 2018

Does this work?

import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def allowedOptions = null
def campaignRetailOptions = null
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def verticalCustomField = getFieldByName("Vertical")
def verticalValue = verticalCustomField.getValue()
def typeOfEngagement = getFieldByName("Type of Engagement")
def typeOfEngagementCustomField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def campaignType = getFieldByName("Campaign Type")
def campaignTypeCustomField = customFieldManager.getCustomFieldObjectByName("Campaign Type")
def fieldConfig = typeOfEngagementCustomField.getRelevantConfig(issueContext)
def customField = customFieldManager.getCustomFieldObjectByName("Type of Engagement")
def customFieldCRO = customFieldManager.getCustomFieldObjectByName("Campaign Type")
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def configCRO = customFieldCRO.getRelevantConfig(getIssueContext())
def optionsCRO = optionsManager.getOptions(config)

if (verticalValue.toString() == "Retail") {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Audience Workshop", "Campaign Tracker", "Email", "Media Plan", "Proactive Audience Outreach", "RFP"]
})
}

if (verticalValue.toString() == "Retail") {
campaignRetailOptions = campaignType.setFieldOptions(options.findAll {
it.value in ["Awareness", "New Store Opening", "Sales Event", "In Market", "Prospecting", "Retention"]
}
)
} else {
allowedOptions = typeOfEngagement.setFieldOptions(options.findAll {
it.value in ["Audience Planning", "Audience Matrix", "Demographic Profiling", "Email", "RFP"]
})

campaignRetailOptions = campaignType.setFieldOptions(optionsCRO.findAll {
it.value in ["Awareness", "Launch", "Sales Event", "In Market"]
})
}
bSte May 17, 2018

It does not b/c I want them to work at the same time, I ended up putting each statement into the same if statement. Thanks for all your help!

0 votes
Praveen
Contributor
May 4, 2018

Hi, can you let me know on what exactly is the error you are facing?

0 votes
Praveen
Contributor
May 4, 2018

Hi, can you let me know what error you are facing ?

bSte May 4, 2018

Hi - here is the error: java.lang.NullPointerException: Cannot invoke method getFieldIdByName() on null object at com.onresolve.jira.groovy.user.FieldBehaviours.getFieldByName(FieldBehaviours.groovy:69) at Script1025.run(Script1025.groovy:16)

TAGS
AUG Leaders

Atlassian Community Events