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

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

Hide values from Component List dynamically

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

2 answers

1 accepted

0 votes
Answer accepted

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! 

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 

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. 

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.

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

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

javier I'm New Here Sep 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)

What is the criteria in details?

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

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

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