Forums

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

Remove option from Select list custom field using behaviour

Dan27
Contributor
June 22, 2020

Hi,

 

I know how can I choose specific options of custom field and display only them using Behaviour,

But I have a custom field that is dynamic and I can't put hardcoded data in my script.

 

How can I remove an option from this custom field (Select List single choice) using behaviours?

 

Thank you,

Daniel

1 answer

1 accepted

0 votes
Answer accepted
Subrat Mishra
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 22, 2020

I believe your Select List Custom field options are static ( as defined in Jira configuration ) . No matter the dynamic nature of the field  , you would need to list down the options always to show up for that field based on some kind of criteria/inputs (like value from other CF, any other condition..)

 

For example , below code will display the options only if they match a regex pattern . There can be also conditions like showing/removing an option based on the currentUser. 

 

import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField

def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager

def customField = customFieldManager.getCustomFieldObjectsByName("Custom Field")[0]
def fieldConfig = customField.getRelevantConfig(underlyingIssue)
def options = optionsManager.getOptions(fieldConfig)

def optionsList = []

FormField CF = getFieldByName("Custom Field")


// You need to design the regex or your conditions as required
def regex = "Option-.*"


options.each {
    if ( it ==~ /$regex/){
        optionsList.add{it}
    }
}
def optionsToSet = options.findAll { it.value in optionsList }
CF.setFieldOptions(optionsToSet)

Dan27
Contributor
June 22, 2020

Okay,

So there is an option take all of the options using script, not hardcoded, withoud the specific option we would like to remove?

 

Thank you!

Subrat Mishra
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 22, 2020

 

// Getting list of all option 

def customField = customFieldManager.getCustomFieldObjectsByName("Custom Field")[0]
def fieldConfig = customField.getRelevantConfig(underlyingIssue)
def options = optionsManager.getOptions(fieldConfig)

 

// Iterate over all the options  you get and if it's not matching with one you don't want , put it in a temp list and set the temp list as your options in the end .

options.each {
    if ( it != "I don't want this option"){
        optionsList.add(it)
    }
}
def optionsToSet = options.findAll { it.value in optionsList }
CF.setFieldOptions(optionsToSet)

Like Servet Kıraç likes this
Dan27
Contributor
June 22, 2020

Hi, Thank you!

It didnt work :( the option I want to remove is still there in create screen..

this is my script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;


def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def CF = getFieldByName("CustomFieldName")

// Getting list of all option

def customField = customFieldManager.getCustomFieldObject("customfield_id")
def fieldConfig = customField.getRelevantConfig(underlyingIssue)
def options = optionsManager.getOptions(fieldConfig)

def optionsList = []
// Iterate over all the options you get and if it's not matching with one you don't want , put it in a temp list and set the temp list as your options in the end .

options.each {
if ( it != "OptionToRemove"){
optionsList.add(it)
}
}
def optionsToSet = options.findAll { it.value in optionsList }
CF.setFieldOptions(optionsToSet)

Subrat Mishra
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 22, 2020

My bad  ! The Options were parsed as string literals earlier that's why the match was never found  . Need to define them as list. 

This should work for you !

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;


def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def CF = getFieldByName("CustomFieldName")

// Getting list of all option

def customField = customFieldManager.getCustomFieldObject("customfield_id")
def fieldConfig = customField.getRelevantConfig(underlyingIssue)

// Define the Option variable as a List Type
List options = optionsManager.getOptions(fieldConfig)

def optionsList = []
// Iterate over all the options you get and if it's not matching with one you don't want , put it in a temp list and set the temp list as your options in the end .

options.each {
if ( it.toString() != "OptionToRemove"){
optionsList.add(it.toString())
}
}
def optionsToSet = options.findAll { it.value in optionsList }
CF.setFieldOptions(optionsToSet)

Like Servet Kıraç likes this
Subrat Mishra
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 22, 2020

Below code should work for you ! Note the custom field declaration . You did it differently than what I mentioned .

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;


def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def CF = getFieldByName("CustomFieldName")

// Getting list of all option

def customField = customFieldManager.getCustomFieldObjectsByName("Custom Field Name")[0]
def fieldConfig = customField.getRelevantConfig(underlyingIssue)
List options = optionsManager.getOptions(fieldConfig)

def optionsList = []
// Iterate over all the options you get and if it's not matching with one you don't want , put it in a temp list and set the temp list as your options in the end .

options.each {
if ( it.toString() != "OptionToRemove"){
optionsList.add(it.toString())
}
}
def optionsToSet = options.findAll { it.value in optionsList }
CF.setFieldOptions(optionsToSet)

Dan27
Contributor
June 23, 2020

Hi, Thanks dor your help, It still not working..

I tried to run in using script console, and 'OptionsList' was empty in the end of the script..

this is my code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.CustomFieldManager;

//def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

//def groupManager = ComponentAccessor.getGroupManager()
//def users = groupManager.getUsersInGroup("DanielTest")

def optionsManager = ComponentAccessor.optionsManager
def customFieldManager = ComponentAccessor.customFieldManager

def CF = getFieldByName("Custom Field Name")

// Getting list of all option

def customField = customFieldManager.getCustomFieldObjectsByName("Custom Field Name")[0]
def fieldConfig = customField.getRelevantConfig(underlyingIssue)
List options = optionsManager.getOptions(fieldConfig)

def optionsList = []
// Iterate over all the options you get and if it's not matching with one you don't want , put it in a temp list and set the temp list as your options in the end .

options.each {
if ( it.toString() != "Name of option to remove"){
optionsList.add(it.toString())
}
}
def optionsToSet = options.findAll { it.value in optionsList }
CF.setFieldOptions(optionsToSet)

Dan27
Contributor
June 23, 2020

My bad, In script console it return what I want, all of the options without the one I need to remove.

 

The behavipur is working in Edit screen, bit not in Create screen, how is it possible? what am I doing wrong?

Thanks

Subrat Mishra
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 23, 2020

Could you try it as "Initialiser" in behaviour ? I'm using this automation for two different requirements ( Custom Field, Fix Version) and they just work fine . I use "initialiser" for the custom field requirement .

Behaviour usually responds when the field changes in such cases , but initialiser kicks in as soon as the form is loaded .

Dan27
Contributor
June 23, 2020

I change it to initialiser and it works only in edit screen, and I need it in Create screen.

What am I doing wrong?

Subrat Mishra
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 23, 2020

You are right , it doesn't work on "Create" screen . Below change is required to make it work on create screen . It will work on edit screen as well . I have verified . 

 

// Getting list of all option
def customField = customFieldManager.getCustomFieldObjectsByName("Approvals")[0]
def fieldConfig = customField.getRelevantConfig(getIssueContext())
List options = optionsManager.getOptions(fieldConfig)

Dan27
Contributor
June 23, 2020

Thank you!!!!!!! It is working :) :) :) 

Like Subrat Mishra likes this
Subrat Mishra
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 23, 2020

Great ! 

"underlyingIssue" doesn't work on create screen , we should be using "getIssueContext()" . In my case the custom field is required only on edit screen so it works fine with "underlyingIssue". 

Brian Alden September 21, 2021

For the line  optionsList.add{it}  my Initialiser screen is telling me that it cannot find a matching method. Is there something else that I need to import or define first in order to use "add"?

//Imports added per Adaptavist library
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

//Define field variables

def cf01 = getFieldByName("Configuration")
def cf02 = getFieldByName("Specification")
def cf03 = getFieldByName('Affected Item')

//Define default hidden fields.

def hiddenFields = [cf01,cf02]
hiddenFields.each { it.setHidden(true).setRequired(false) }

//Hide "Multiple" from "Affected Items"

def cf03object = customFieldManager.getCustomFieldObject(cf03.fieldId)
def cf03config = cf03object.getRelevantConfig(getIssueContext())
def cf03options = ComponentAccessor.optionsManager.getOptions(cf03config)
def cf03limitList

cf03options.each {
    if (it.toString() != "Multiple (see sub-tasks)") {
        cf03limitList.add(it.toString())
    }
}

def cf03limit = cf03options.findAll {it.value in cf03limitList}
cf03.setFieldOptions(cf03limit)

//End

Suggest an answer

Log in or Sign up to answer