Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Published in Trello

🏊‍♀️ Atlympic Event: Trello

Hello Community!  Quick disclaimer: We are running a contest on Community (The Atlympics!) from July 23rd - August 8th of 2021. If you are interested in participating in this contest (prizes! ...

100 views 1 10
Read article

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