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

Script runner - Behavior - Limit field options based in the issue type

Humberto Gomes
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.
January 26, 2021

Hi,

I need to show different values for same field based in the issue type.
From as I know, this can be done using Behaviors.

Jira Field:
Service and Sub-category - Is field is a cascade option.

Field options:
Service1 - 1 Sub 1
Service1 - 1 Sub 2

Service2 - 1 Sub 1
Service2 - 1 Sub 2

Service3 - 1 Sub 1
Service3 - 1 Sub 2

Context created:
Project = TESTSD
All issue types

What I need:
Issue type "Support"
must show only the options:
Service1 - 1 Sub 1
Service1 - 1 Sub 2

Service2 - 1 Sub 1
Service2 - 1 Sub 2

Issue type "BUG"
must show only the options:
Service3 - 1 Sub 1
Service3 - 1 Sub 2

Note 1 - this is to user in Service Desk

 

How can i do this? Can you help?

 

Thanks in advance

2 answers

1 accepted

1 vote
Answer accepted
Kian Stack Mumo Systems
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Humberto Gomes, can you please clarify, should this be shown on the customer portal? Or is this on the agent side? 

Thanks!

Kian

Humberto Gomes
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.
January 26, 2021

Hi,

Customer portal.

 

Humberto

Humberto Gomes
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.
January 26, 2021

Customer portal view:

ScreenHunter 763.png

 

 

Issue type mapping:

ScreenHunter 764.png

Kian Stack Mumo Systems
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Humberto Gomes, I am looking into a solution!

Kian Stack Mumo Systems
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 26, 2021

@Humberto Gomes

Try something like this!


  1. Create a new behavior and tie it to all request types in the project.
  2. Add the following code to the initialiser section:
    import com.atlassian.jira.component.ComponentAccessor

    import com.onresolve.jira.groovy.user.FieldBehaviours
    import com.atlassian.jira.issue.priority.Priority
    import groovy.transform.BaseScript

    @BaseScript FieldBehaviours fieldBehaviours
    def customFieldManager = ComponentAccessor.getCustomFieldManager()
    def optionsManager = ComponentAccessor.getOptionsManager()
    def customerSelectField = getFieldByName('Service and Sub-category')
    def customField = customFieldManager.getCustomFieldObject(customerSelectField.getFieldId())
    def config = customField.getRelevantConfig(getIssueContext())
    def options = optionsManager.getOptions(config)


    def optionMap = [:]
    optionMap.put("-1", "None")


    if (issueContext.issueType.name == "Support") {

    optionMap += options.findAll {it.value in ['Service1', 'Service2']}.collectEntries {[(it.optionId.toString()): it.value]}

    }
    else if(issueContext.issueType.name == "Bug"){
    optionMap += options.findAll {it.value in ['Service3']}.collectEntries {[(it.optionId.toString()): it.value]}

    }


    customerSelectField.setFieldOptions(optionMap)

     

    I did some testing with this and it appears to be working for me!
Humberto Gomes
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.
January 27, 2021

@Kian Stack Mumo Systems  Thanks for your time.

 

What i'm doing wrong here?

 

Behaviour:

 

Field options:

ScreenHunter 777.png

 

Mapping:

ScreenHunter 776.png

 

Code:

ScreenHunter 778.png

 

Service desk:

ScreenHunter 780.pngScreenHunter 779.png

 

 

IF I Change the mapping to:

ScreenHunter 781.png

The problem is the same.

Kian Stack Mumo Systems
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 27, 2021

Hmmm. I'm not sure on that one. What version of Jira are you using?

Humberto Gomes
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.
January 27, 2021

Jira Software 8.5.3

Humberto Gomes
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.
January 29, 2021

Hi,

 

This is the final code and it's working:

 

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()

def issueType = issueContext.getIssueType().getName()
def cascadingCustomField = customFieldManager.getCustomFieldObjects().findByName("Service and Subcategory")
def cascadingFormField = getFieldByName("Service and Subcategory")

def fieldConfig = cascadingCustomField.getRelevantConfig(issueContext)
def options = optionsManager.getOptions(fieldConfig)

// Get the parent's ID
def parentA_ID = options.find

{ it.value == 'A' }

.getOptionId()
def parentB_ID = options.find

{ it.value == 'B' }

.getOptionId()
def parentC_ID = options.find

{ it.value == 'C' }

.getOptionId()

// Set the parent's options
Map option1 = [:]
option1.put ("-1", "None")
option1.putAll([(parentA_ID) : "A" , (parentB_ID) : "B"])

Map option2 = [:]
option2.put ("-1", "None")
option2.putAll([(parentC_ID) : "C"])

// Set available options based on Issue Type
if (issueType == "Bug")

{ cascadingFormField.setFieldOptions(option1) }

else if (issueType == "Support")

{ cascadingFormField.setFieldOptions(option2) }

 

 

Thanks for all support.

@Kian Stack Mumo Systems 

I'm tagging this as the Accept Answer.

0 votes
Benz Kek _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.
January 26, 2021

Hi there, 

You can check out ScriptRunner's Library of the two below: 


The idea of setting normal Select List or Cascading Select List field options are the same as given in the above links. 

Other than that, do take note that there are two types of Behavior's mapping:

  1. Project/issuetype mapping
  2. Service Desk mapping

If you are trying to use it in Customer Portal, make sure you use the Service Desk mapping and map it to the appropriate Request Types. 

 

Cheers~ 

Bill Goodall February 23, 2024

Hi @Benz Kek _Adaptavist_

I find that these links you provided are done in Groovy. But we have some existing scripts written without groovy. Do i need to convert the whole script to groovy, or can i only convert this one line? 

Or else can you tell me how to translate that before Groovy was being used in these scripts? 

if (issueType == "Initiative" || issueType == "Feature" || issueType == "xyz Feature" || issueType == "xyz Initiative" ) {
def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["1. Must have", "2. Should have", "3. Could have", "4. Won't have this time"]
}.collectEntries {
[(it.id): it.name]
}
formField.setFieldOptions(optionsMap)
}

Which position would we be throwing the default assignment statement into? 
See my Option 1??, Option 2??, Option 3?/, Option 4?? positions below


if (issueType == "Initiative" || issueType == "Feature" || issueType == "xyz Feature" || issueType == "xyz Initiative" ) {
// Option 1??
def optionsMap = ComponentAccessor.getConstantsManager().getPriorities().findAll { priority ->
priority.getName() in ["1. Must have", "2. Should have", "3. Could have", "4. Won't have this time"]
// Option 2??
}.collectEntries {
[(it.id): it.name]
// Option 3??
}
formField.setFieldOptions(optionsMap)
// Option 4?? 
}

Also is this a valid approach? I am getting errors on the statement syntax 
def defaultValue = optionsMap.find(it.value == "3. Could have")

[Static type checking] - Cannot find matching method java.util.Map#find(boolean). Please check if the declared type is correct and if the method exists.
Possible solutions: find(), find(groovy.lang.Closure), find(groovy.lang.Closure), min(groovy.lang.Closure), any(), findAll()

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events