Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to change the options listed in Component/s based on selection from a Custom field

Frantisek Kust April 20, 2020

Hi Team,

I am looking if possible to create a dependency between a Custom field and Component/s field.

Both are single select list.

-> The custom field has 3 values (e.g Product A, Product B, and Product C).

Once I will select Product A, the Component/s list will be restricted to only valid options for this Product.

Could be this achieved using the Behaviours plugin?
Or another approach like Listener?

I found there a very similar request, just Component determining Custom field with code as below, but I would need to revert it and don't know how. :-(

{code}

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.project.component.ProjectComponent

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def selectList1 = getFieldById(fieldChanged)
def selectList2 = getFieldByName("Sub-component")

def selectList1Val = selectList1.getValue()
Map fieldOptions = [:]
fieldOptions.put ("-1", "None")

selectList1Val.each{
def selectListname = it.getAt("name")

switch (selectListname){
case "Comp 1" :fieldOptions.putAll (["22601":"Sub1", "22602":"Sub2", "22603":"Sample Sub 3"])
break
case "Comp 2" :fieldOptions.putAll (["22604":"Sample Sub 1", "22605":"Sub 2 sample"])
break
}

}
selectList2.setFieldOptions (fieldOptions)

{/code}

 

Could be anyone so nice and help me with, pls?

Many thanks in advance,

Frank

2 answers

1 accepted

0 votes
Answer accepted
Andre Serrano
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 24, 2020

Hi Frantisek,
Adaptavist Library website has a collection of custom scripts.
Although I couldn't find a script that solves your need, I found lots of scripts that were doing something similar.

I would recommend you to have a look at these scripts.
I'm convinced that by doing so you will be able to combine some of them and get to the result you want.

https://library.adaptavist.com/search?term=behaviour

Michelle Pogado August 6, 2020

Hi @Andre Serrano ,

I echo the same question as Frantisek.
I am also looking at customizing the options listed on Component/s field based on the user group.

A confirmation that this is not possible would be appreciated.
I have honestly tried many things including this https://library.adaptavist.com/entity/set-available-options-to-a-multi-select-list but nothing works.

So if this is indeed impossible, a confirmation (that it is indeed impossible) from adaptivist would be helpful.

Thanks!

1 vote
Kristian Walker _Adaptavist_
Community Champion
April 21, 2020

Hi Frantisek,

Thank you for your question.

I can confirm that the code and community answer you have linked to above is from ScriptRunner for Jira Server and this will not work with ScriptRunner for Jira Cloud.

I can confirm that the ScriptRunner for JIRA Cloud plugin is unable to perform any dynamic validation on issue forms to link or hide fields like the server version can with the Behaviors feature.

Also I can confirm that the plugin cannot dynamically populate field values based on other field values like it can in the server version can with the Behaviors feature.

The reason we are unable to provide this functionality is due to the restricted functionality and API's which Atlassian provide inside JIRA Cloud, due to the fact that Atlassian only provide a Rest API in Jira Cloud and not the same Java API that the server version contains.

You can see more detailed information on the differences between ScriptRunner for JIRA Cloud and ScriptRunner for JIRA Server inside of the documentation page located here.

This unfortunately means it will not be possible to achieve your requirement of creating a dependency between a custom field and the components field with ScriptRunner for Jira Cloud as the functionality to required to achieve this does not exist inside of Jira Cloud.

I hope this information helps.

Regards,

Kristian

Frantisek Kust April 21, 2020

Hi Kristian,

Thank you for your answer, but I choose wrongly ticket category, we are on the server and not having a cloud version. We are running version Jira v7.13.8.

Apologize me, but I don't understand if you are saying that this required functionality is not possible also for the server version?

Or it is possible to enable this functionality if we are having server-based Jira stage??

Thanks for the clarification.

--- And ---

In case that will be possible to achieve my requirement of creating a dependency between a custom field and the components field with ScriptRunner for Jira Server,  how code paste below has to change in a case to achieve reverse logic as described, pls? And how to implement it into relation? Attached to Component or Custom field?

https://prnt.sc/s3chpp

Thanks a lot in advance.
Frank

Kristian Walker _Adaptavist_
Community Champion
April 21, 2020

Hi Frantisek,

I can confirm that it is possible to achieve your requirement on the Server version using ScriptRunner and the Behaviors Feature that it provides.

I do not have an example of how to do this for ScriptRunner for Jira Server which i am able to share but I would advise re creating your question and tagging it as server so that other users who have achieved a similar requirement with the server version can assist yourself.

Regards,

Kristian

Mauricio F_
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 6, 2021

@Kristian Walker _Adaptavist_ We are on the server version and we need to do this. Can you please show me an example of how to hide values from the component/s list?

Kristian Walker _Adaptavist_
Community Champion
January 6, 2021

Hi Maurico,

I don't have an example of how to achieve your requirement that I can share with yourself.

I would you advise you raise a new question detailing what your requirement is in more detail along with tagging the answer as for Jira Server so that other users can see your question and provide suggestions on how they achieved this requirement themselves. 

Also, I would advise looking at the Adaptavist Libray located here as well the documentation located here as these pages contain sample scripts that you can take and look to modify to help achieve your requirement. 

The documentation page located here outlines how to set default values in the Components field and this may be a useful example to show how to manipulate this field to achieve your requirements.

I hope this helps.

Kristian

Frantisek Kust January 6, 2021

Hi Mauro,

I was able to achieve this dependency requirement by using Scriptrunner Behaviour plugin. 

You can map there 2 fields together and create condition for one of it.

Mine are those 2 fields - Product and Components.

Product is custom field, Component is system.

The logic is that selection from Product field affect available options in Component field (aka it will limit options to predefined relevant to picked Product 'value')

Behaviour:

import static com.atlassian.jira.issue.IssueFieldConstants.COMPONENTS
import com.atlassian.jira.component.ComponentAccessor
def final PRODUCT = 'customfield_12316340'

def product = getFieldById(PRODUCT)
def component = getFieldById(COMPONENTS)

component.setRequired(true)
//component.setDescription(new Date().toString() + ComponentAccessor.projectComponentManager.findAllForProject(19207).collect {c -> c.getName()}.join(' '))
//def componentsForProject =
component.setFieldOptions(
ComponentAccessor.projectComponentManager.findAllForProject(12320021)
.findAll { c -> c.getDescription().contains(product.getValue()) }
.collectEntries { c -> [(c.getId()): c.getName()] }
)

->  So you need to know ID of your project and your custom field.

Component definition

I am utilizing component description field to specify to which Product each component belongs.

This is how my workaround is working.

Frantisek

ServiceRocket Managed Services August 22, 2024

Hello Frantisek,

Is this for Cloud or Server version of Jira 

Suggest an answer

Log in or Sign up to answer