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

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,557,547
Community Members
 
Community Events
184
Community Groups

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

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.

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