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
Hi there, you can try some something like below using the Behaviour:
import com.atlassian.jira.bc.project.component.ProjectComponentimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScript@BaseScript FieldBehaviours behavioursdef componentCF = getFieldById("components")log.error componentCF.getValue()def project = underlyingIssue.projectId// Get all available componentsdef components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)// Define your restricted componentsdef retrictedComponents = ['a','b'] // a and b is my component name// Filter the componentsdef allowedComponents = components.findAll { it.name in retrictedComponents}componentCF.setFieldOptions(allowedComponents)
I hope the above helps!
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.
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.ProjectComponentimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScript@BaseScript FieldBehaviours behavioursdef componentCF = getFieldById("components")log.error componentCF.getValue()def project = underlyingIssue.projectId// Get all available componentsdef components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)// Define your restricted componentsdef retrictedComponents = ["1/ GDA 2/ Création de compte",'1/ GDA 2/ Habilitation de compte'] // a and b is my component name// Filter the componentsdef 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_ ,
I do this script in my Behaviour :
import com.atlassian.jira.bc.project.component.ProjectComponentimport com.atlassian.jira.component.ComponentAccessorimport com.onresolve.jira.groovy.user.FieldBehavioursimport groovy.transform.BaseScript@BaseScript FieldBehaviours behavioursdef componentCF = getFieldById("components")log.warn("componentCF: "+ componentCF)log.error componentCF.getValue()def project = underlyingIssue.projectIdlog.warn("project: "+ project)// Get all available componentsdef components = ComponentAccessor.getProjectComponentManager().findAllForProject(project)log.warn("components: "+ components)// Define your restricted componentsdef 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 namelog.warn("restrictedComponents: "+ restrictedComponents)// Filter the componentsdef allowedComponents = components.findAll { !it.id in restrictedComponents}componentCF.setFieldOptions(allowedComponents)log.warn("allowedComponents: "+ allowedComponents)
But in my components Field, i have this :
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.
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:
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.
For anyone looking to do something similar, here is how I got it to work:
It looks like you're new here. Sign in or register to get started.