I am working on a POC to demonstrate field behavior modification by suing the behavior module of Scriptrunner
I am trying to limit the option values for this single selection custom filed “SelectBehavior”
I have created a behavior and mapped it to my project and issue types
I have the following code in the initializer section of the behavior and also added the field SelectBehavior to the behavior
But when I create a new “Test Defect” I don’t see any change in the drop down value for the field SelectBehavior
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def FormField formField = getFieldByName("SelectBehavior")
def customField = customFieldManager.getCustomFieldObject(formField.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)
def optionsMap = options.findAll {
it.value in ["None","Two"]
[
(it.optionId.toString()) : it.value
]
}
String issueType = issue.issueType.name
if (issueType.equals("Test Defect"))
{ log.info "behavior good"
formField.setFieldOptions(optionsMap)
formField.setRequired(true)
}
Hey saravana!
So, I think the reason your script wasn't working is because you are using this on the Create screen and the "issue" object doesn't technically exist yet. I also tweaked your script slightly to give it some updates. I used this on my instance and it worked:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def selectedIssueType = getIssueContext().getIssueTypeObject().getName()
def fieldName = "SelectListA"
def field = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObjectsByName(fieldName)[0]
def fieldConfig = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(fieldConfig)
def optionsMap = options.findAll {
it.value in ["AAA","BBB"]
}
if (selectedIssueType == "Test Defect") {
field.setFieldOptions(optionsMap)
}
You'll want to change the value of the variable fieldName so that it matches what exists in your instance. This code is made to go in the Initializer field.
Hey saravana!
So, I think the reason your script wasn't working is because you are using this on the Create screen and the "issue" object doesn't technically exist yet. I also tweaked your script slightly to give it some updates. I used this on my instance and it worked:
import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def selectedIssueType = getIssueContext().getIssueTypeObject().getName()
def fieldName = "SelectListA"
def field = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObjectsByName(fieldName)[0]
def fieldConfig = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(fieldConfig)
def optionsMap = options.findAll {
it.value in ["AAA","BBB"]
}
if (selectedIssueType == "Test Defect") {
field.setFieldOptions(optionsMap)
}
You'll want to change the value of the variable fieldName so that it matches what exists in your instance. This code is made to go in the Initializer field.
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.