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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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

0 votes
Answer accepted

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

Thanks!

Kian

Hi,

Customer portal.

 

Humberto

Customer portal view:

ScreenHunter 763.png

 

 

Issue type mapping:

ScreenHunter 764.png

@Humberto Gomes, I am looking into a solution!

@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!

@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.

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

Jira Software 8.5.3

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.

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~ 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
Community showcase
Published in Jira Service Management

Why upgrade to Jira Service Management Premium?

We often have questions from folks using Jira Service Management about the benefits to using Premium. Check out this video to learn how you can unlock even more value in our Premium plan.  &nb...

165 views 0 4
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you