Hide values from Component List dynamically

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

Is it possible to hide values dynamically from the "Components" select list based on some criteria?
I would imagine by using a scriptrunner behaviour.

If anyone has an example would be much appreciated :)

3 answers

1 accepted

0 votes
Answer accepted
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 6, 2021

Hi there, you can try some something like below using the Behaviour: 

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def componentCF = getFieldById("components")
log.error componentCF.getValue()

def project = underlyingIssue.projectId
// Get all available components
def components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)

// Define your restricted components
def retrictedComponents = ['a','b'] // a and b is my component name

// Filter the components
def allowedComponents = components.findAll {
it.name in retrictedComponents
}

componentCF.setFieldOptions(allowedComponents)

I hope the above helps! 

BYA May 18, 2021

Hello

Thanks for your answer. 

I would like to hide some values ​​in the components, so i do that


import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def componentCF = getFieldById("components")
log.error componentCF.getValue()

def project = underlyingIssue.projectId

// Get all available components
def components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)

// Define your restricted components
def retrictedComponents = ["1/ GDA 2/ Création de compte",'1/ GDA 2/ Habilitation de compte'] // a and b is my component name

// Filter the components

def allowedComponents = components.findAll {
it.name in retrictedComponents
}
componentCF.setFieldOptions(allowedComponents)

But nothing is done. 

Can you please help me. 

Thanks,

Fayçal 

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.
May 18, 2021

Hi Fayçal, 

To exclude the components, you will need to change the logic from this: 

def allowedComponents = components.findAll { 
it.name in retrictedComponents
}

to this: 

def allowedComponents = components.findAll { 
!it.name in retrictedComponents
}

Else, it will include those in the retrictedComponents instead. 

BYA May 19, 2021

Hello @Benz Kek _Adaptavist_ ,

Thanks for your answer. 

I do this script in my Behaviour :

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def componentCF = getFieldById("components")
log.warn("componentCF: "+ componentCF)
log.error componentCF.getValue()

def project = underlyingIssue.projectId
log.warn("project: "+ project)

// Get all available components
def components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)
log.warn("components: "+ components)

// Define your restricted components
def restrictedComponents = ['1/ GDA 2/ Création de compte','1/ GDA 2/ Habilitation de compte'] // a and b is my component name
//def restrictedComponents = ['a','b'] // a and b is my component name
log.warn("restrictedComponents: "+ restrictedComponents)

// Filter the components
def allowedComponents = components.findAll {
!it.id in restrictedComponents
}

componentCF.setFieldOptions(allowedComponents)
log.warn("allowedComponents: "+ allowedComponents)

 But in my components Field, i have this : 

Image.png

I excluded those two values {'1/ GDA 2/ Création de compte','1/ GDA 2/ Habilitation de compte'} but i still see them in the list of components. 

can you please help. 

Thanks 

Fayçal.

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.
May 19, 2021

Not sure why you changed this from your previous post, but you are filtering using it.id instead of it.name that I specifically quoted them in my previous reply. 

The correct one should be: 

// Filter the components
def allowedComponents = components.findAll {
!it.name in restrictedComponents
}
Like BYA likes this
BYA May 21, 2021

it's works, thank you so much for your help

javier
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!
September 30, 2021

I am trying that script without success. Where should be added? initialiser?.  I don't know why is not working properly. Basically I would like to hide Component Q_NC_mayor in this below example but I see when creating an issue.

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def componentCF = getFieldById("components")
log.error componentCF.getValue()

def project = underlyingIssue.projectId
// Get all available components
def components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)

// Define your restricted components
def retrictedComponents = ['Q_NC_mayor']

// Filter the components
def allowedComponents = components.findAll {
!it.name in retrictedComponents
}

componentCF.setFieldOptions(allowedComponents)

Bohdan Lozinskyi April 11, 2024

Hi @Benz Kek _Adaptavist_ ,

In your case some Components are hidden by default but I'm not sure how to show different Components based on custom field value.

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.
April 15, 2024

Hey @Javier

It depends on how you want it to behave. 

For example 

  • If you want it to run the moment the Issue Screen (Create/Edit screen) is loaded, you will want to put it in the Intialiser
  • If you want it to run based on some custom field or system field values, you need to add it under Add Fields -> Server-Side Script. So it will run when that field value is interacted with by the user. 
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.
April 15, 2024

Hi @Bohdan Lozinskyi

In your case, you would need more conditions. 

For starters, you can try these where I change the component list based on the Priority field value: 

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours

def componentCF = getFieldById("components")
def priorityCF = getFieldById("priority")

def project = underlyingIssue.projectId
// Get all available components
def components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)

// Let's say I want to restrict components based on Priority value
def retrictedComponents
def priorityName = priorityCF.getValue().name

if (priorityName == "Highest") {
retrictedComponents = ['a','b'] // a and b is my component name
} else if (priorityName == "Low") {
retrictedComponents = ['c','d'] // c and d is my component name
}

// Filter the components
def allowedComponents = components.findAll {
it.name in retrictedComponents
}

componentCF.setFieldOptions(allowedComponents)

This code needs to be added under the Priority field -> Server-side script.  

0 votes
Allison May 3, 2024

Hi all, 

Thank you for this useful code. I have been able to use this successfully as an initialiser upon issue edit, but it fails upon issue create "Cannot get property 'projectId' on null object"

 

This makes sense, because as there is no Project yet set on an issue that is yet to be created.

 

I have tried many variations of trying to get the project ID through the issuecontext, but all have failed (examples: 

def project = getIssueContext().getProjectObj(), 
def project = getIssueContext().getProjectObject(),
def project = getIssueContext().getProjectID()
)
Is there a way to call the project based on what is currently selected in the Project/s field? I understand I may have to move the code to a server side script, but I am still stuck on how to identify the project to allow the script to call for the project's components.
Any help would be greatly appreciated. Thanks!
Allison May 6, 2024

Hypothetically, I don't really need to pull in the project components and could just hard code the list I want to show without caring what exists in the project, but I'm not sure if Jira will let me do that.

Allison May 6, 2024

For anyone looking to do something similar, here is how I got it to work: 

 

// Define some more variables for the project context
String projectKey = null
def projectObj = underlyingIssue?.getProjectObject()
if (projectObj) {
projectKey = projectObj.key
} else {
projectObj = getIssueContext().getProjectObject()
projectKey = projectObj?.getKey()
}

// Get all available components
def components = ComponentAccessor.projectComponentManager.findAllForProject(issueContext.projectId)
0 votes
Radek Dostál
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 6, 2021

What is the criteria in details?

It's a code, it needs to be detailed to make it work well.

Radek Dostál
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 6, 2021

By the way you might find this page useful: https://scriptrunner.adaptavist.com/6.16.0/jira/recipes/behaviours.html

 

This contains lots of examples including setting component values.

Suggest an answer

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

Atlassian Community Events