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

How to disable custom field value based on issue type?

Teja May 3, 2016

Hi,

want to disable custom field value based on the selection of issue type, Ex I have two issue types 1. Core request and 2. Custom request   and multiselect field contains [ABC, XYZ, ASD, FGH] . When the user selects issuetype Core request and multiselect field shows all value. if user selects Custom request and multiselect field shows only ABC, XYZ, FGH . how to do that any scripts?

Thanks in Advance 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

3 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 12, 2016

Hi Teju,

This can be done using the Behaviours module of the ScriptRunner add on by placing a behaviour on the Issue Type field.

I have enclosed below a sample script and screenshot of how it is configured which shows how I restrict the options in the select list when the Story issue type is selected. For all other issue types the select field shows all values.

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)
}

image2016-5-12 14:44:3.png

I hope this helps.

Thanks

Kristian

Teja May 18, 2016

Great...

Thanks 

Teju 

Henrik Gerdtsson March 29, 2018

Hello Kristian

When i try to implement this script in JIRA 7.1.0 I get an error regarding getIssueContext(),getIssueTypeObject(). It seems to be deprecated. A suggestion to use IssueContext.getIssueType() is displayed.

I am not able to get this script to work. Do you have any suggestions on how to get it to work in a v7+ Environment?

Br
Henrik

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 10, 2018

Hi Henrik,

The static type checking warnings on line 6 are just warnings however if you wish to remove these you can change line 6 to be as per below.

def issuetype = getIssueContext().getIssueType().name

I have just tried the script above on my local version of Jira 7.8.0 using the latest version of ScriptRunner and it still works restricting the select list options when the issue type of story is selected.

Can you please verify that you added the script as a behavior on the issue type field as well set the name of the Select List Field and the field options to match a valid custom field and set of options for the field for your instance. 

Thanks

Kristian

Henrik Gerdtsson April 10, 2018

Thank you Kristian,

I got it to work now. I am not quite sure why it did not work previously.

I do have another question regarding this behaviour. By default my select list has no value. When I create the issue it sais None. Is it possible to get this behaviour as well, None or no selection should be a valid option?
I have tried using "", " ", and "None" when selecting the valid options.

Br
Henrik

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 11, 2018

Hi Henrik,

The None option is provided by default

In order to set the none option please use a line of code similar to below

selectcf.setFormValue(null)

Thanks

Kristian

Henrik Gerdtsson April 12, 2018

Hello Kristian,

Yes I can set the value to None. I am not able to get None as an option for the user in the create screen.

Any ideas on how to do this?

Br
Henrik

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
April 12, 2018

Hi Henrik,

To check for none you should check if the value of the field is null.

Thanks

Kristian

Saravanan Ravikumar July 23, 2019

@Kristian Walker _Adaptavist_ Even i was trying to populate the "None" to be default. But unable to find it. Could you please help me out.

By default unable to generate the None value.

And in the custom field we have set it to be None. But in the script level how to set "none" to be default. 

Henrik Gerdtsson July 26, 2019

 

 I configured my custom field without default value and without none as an option. Se picture

customfield-configuration.png

Then I changed the script and added None first. That way None will be default. Se code below.

// If issue type matches change the cf values to be displayed
def optionsMap = [:]
if (issuetype == "Task") {
    optionsMap.put("-1","None") // Add None first in the list
    optionsMap += options.findAll {
        it.value in ["T2T Yes","T2T Accepted","T2T Partly","T2T Rejected"] // list of options you want to show
    }.collectEntries {
        [
                (it.optionId.toString()): it.value
        ]
    }
} else {
    optionsMap += options.findAll {
        it.value in ["Yes","Accepted","Partly","Rejected"] // list of options you want to show
    }.collectEntries {
        [
                (it.optionId.toString()): it.value
        ]
    }
}
selectcf.setFieldOptions(optionsMap)

 

I hope this helps.

Saravanan Ravikumar July 26, 2019

Now i want to display only one value for the custom field. That should be default.

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.

TAGS
AUG Leaders

Atlassian Community Events