Use behaviour to hide values based on issue type

Cedric_DEVAUX April 5, 2018

Hello,

 

During the screen creation, I need to hide certain values of a selectlist based on issuetype.

I search on Atlassian community and I found this script written by Kristian Walker:

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-disable-custom-field-value-based-on-issue-type/qaq-p/529895

I tried but I got a message to inform me that getIssueContext() is deprecated.

I'm newbie on scripting ^^

Could you help me to rearrange to script with my requirements?

Replace getissuetypeobject().name by getissuetypeId in order to use issuetype id and not the name

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*

// Get the current issue type from the issue context incase the issue
// has not been created yet    
def issuetype = getIssueContext().getIssueTypeObject().name

// Get a pointer to my select field
def selectcf = getFieldByName("DemoSelectList2")

// Get pointers to the required custom field and otion managers
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def customField = customFieldManager.getCustomFieldObject(selectcf.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

// If issue type matches change the cf values to be displayed
if (issuetype.contains("Story")) {
    def optionsMap = options.findAll {
        it.value in ["A", "B"] // list of options you want to show
    }.collectEntries {
        [
                (it.optionId.toString()): it.value
        ]
    }
    selectcf.setFieldOptions(optionsMap)
}

Thanks a lot.

 

Cedric

3 answers

0 votes
Saravanan Ravikumar July 23, 2019

How to hide only one value from the custom field.?

Regards

Cedric_DEVAUX July 23, 2019

From a select list?

If yes, to hide 1 value, the tips is to select values you want to show and not hide unwanted value

here a example:

You want to hide Value C

if (issuetype == "63" || issuetype == "10402" || issuetype == "11301") { 
def optionsMap = options.findAll {
it.value in ["Value A", "Value B", "Value D"] // list of options you want to show
}.collectEntries {
[
(it.optionId.toString()): it.value
]
}
selectcf.setFieldOptions(optionsMap)
}
Like Saravanan Ravikumar likes this
Saravanan Ravikumar July 24, 2019

Hi Cedric,

in-stead of hiding one value i thought to make one value to be default for one issue type alone.

So i wrote the script for the same.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.*

// Get the current issue type from the issue context incase the issue
// has not been created yet
def issuetype = getIssueContext().getIssueType().name

// Get a pointer to my select field
def selectcf = getFieldByName("Discovery Method")

def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(selectcf.getFieldId())
def config = customField.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(config)

if (issuetype.contains("In-Sprint Bug")) {
def optionToSelect = options.find { it.value == "Unit Test" }

selectcf.setFormValue(optionToSelect.optionId)
}

 

But i cannot make one value to be default. Any idea on this ? how to make one particular value to default.

Cedric_DEVAUX July 24, 2019

Hi Saravanan,

I think you need more an initialiser

https://scriptrunner.adaptavist.com/latest/jira/behaviours-overview.html

it's talk about how to initialise the description.

Saravanan Ravikumar July 24, 2019

I couldn't find the proper method to use it. It would be great if you help me out. Please

0 votes
Omprakash Thamsetty
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.
June 6, 2019

@Cedric_DEVAUX  Does it working for you. Underlyingissue shows the issue type name but it is not setting the values based on issue type.

Omprakash Thamsetty
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.
June 6, 2019

@Cedric_DEVAUX @Alexey Matveev 

Its working if we do not have any context root. How to read the values from context root for the fields.

0 votes
Alexey Matveev
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.
April 5, 2018

You should change 

getIssueContext().getIssueTypeObject().name

to 

underlyingIssue?.getIssueType().name

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events