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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

Suggest an answer

Log in or Sign up to answer
1 vote
Answer accepted
Carmen Creswell [Adaptavist]
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.
Dec 17, 2018 • edited

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. 

0 votes
Carmen Creswell [Adaptavist]
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.
Dec 17, 2018

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. 

TAGS
AUG Leaders

Atlassian Community Events