Missed Team ’24? Catch up on announcements here.

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

Need assistance creating behavior

saravana athilingam November 21, 2018



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.
December 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. 

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.
December 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