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
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)
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
// 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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 .
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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".
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.