Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,296,438
Community Members
 
Community Events
165
Community Groups

Need assistance creating behavior



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)
}

2 answers

1 accepted

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. 

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Jira Service Management

Jira Service Management Documentation Opportunities

Hello everyone, Hope everyone is safe! A few months ago we posted an article sharing all the new articles and documentation that we, the AMER Jira Service Management team created. As mentioned ...

320 views 0 10
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you