Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner: While creating an issue, only allow specific customfield values based on Issue Type

Michael
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.
May 18, 2023

Hello all,

Within Behaviors, I'm trying to figure out how to pull the "Value" of the issue type field drop down within the create screen in order to set specific options within a single select customfield (Client).

So far I have this:

import com.atlassian.jira.component.ComponentAccessor

import com.onresolve.jira.groovy.user.FieldBehaviours

import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def IssueTypeField = #????#


def IssueTypeFieldValue = #????#

log.warn("issue type value = ${IssueTypeField}")

def ClientField = getFieldByName("Client")

def optionManager = ComponentAccessor.optionsManager

def customFieldManager = ComponentAccessor.customFieldManager

def ClientFieldCustomField = customFieldManager.getCustomFieldObject(ClientField.fieldId)

def ClientFieldConfig = ClientFieldCustomField.getRelevantConfig(issueContext)

def ClientFieldOptions = optionManager.getOptions(ClientFieldConfig)

def allAppOptions = [

    "Issue-Type-#1": ["Client A"],

    "Issue-Type-#2": ["Client A", "Client B"],

    ]

if (allAppOptions[IssueTypeFieldValue]) {

    ClientField.setFieldOptions(allAppOptions[IssueTypeFieldValue])

} else {

    ClientField.setFieldOptions([])

}

Can someone help?

Thanks,

Mike

1 answer

1 accepted

0 votes
Answer accepted
Radek Dostál
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.
May 18, 2023

When you're on 'Create' normal field getters won't work, because the issue doesn't exist. There is a different way to get it by reading it off the current context:

def issueTypeName = getIssueContext().getIssueType().getName()

 

Which does change also if you switch issue types midway through the form. 

 

Other than that, I'm not sure whether the way you're trying to set field options will work, this seems a bit sus with nested string lists:

def allAppOptions = [

    "Issue-Type-#1": ["Client A"],

    "Issue-Type-#2": ["Client A", "Client B"],

    ]

If it doesn't, see https://docs.adaptavist.com/sr4js/latest/features/behaviours/api-quick-reference on how to get it to work.

Michael
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.
May 18, 2023

Hi Radek,

The above code for the issue type name:

def issueTypeName = getIssueContext().getIssueType().getName()

 Worked like a charm, and so did the original "def allAppOptions" coding.

Thank you very much!

~Mike

Suggest an answer

Log in or Sign up to answer