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.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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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 :
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
In your case some Components are hidden by default but I'm not sure how to show different Components based on custom field value.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Javier,
It depends on how you want it to behave.
For example
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For anyone looking to do something similar, here is how I got it to work:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What is the criteria in details?
It's a code, it needs to be detailed to make it work well.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Show up and give back by attending an Atlassian Community Event: we’ll donate $10 for every event attendee in March!
Join an Atlassian Community Event!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.