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

Need to restrict Component/s to single select using ScriptRunner

I have researched and found numerous posts about restricting the Components field to a single choice.  However, I don't see a specific way to do this with ScriptRunner.  I am totally new to ScriptRunner so am wondering if there is some example logic already to do this.  If not, do I accomplish this with a behaviour or a script validator in the workflow? I just need direction on what is the correct method.  Any advice is greatly appreciated.

3 answers

0 votes

Hello, 

You could add a behaviour. In the behaviour add Component/s field and paste the script below to the serverside script

import static com.atlassian.jira.issue.IssueFieldConstants.*

def cfComponent = getFieldById(COMPONENTS);
def compValue = cfComponent.getFormValue()
if (compValue instanceof List) {
cfComponent.setError("too many components chosen");
} else {
cfComponent.clearError();
}

Thanks Alexey for your quick response.  However, nothing was happening so I added logging.  The component field is NULL.  I have tried it for other fields also and I've tried using the getFieldByName and they are still NULL.  Any clue?

 

import static com.atlassian.jira.issue.IssueFieldConstants.*

def cfComponent = getFieldById(COMPONENTS);
def compValue = cfComponent.getFormValue()
log.debug ("***compValue = " + compValue )

if (compValue instanceof List) {
cfComponent.setError("Please select only ONE Component");
} else {
cfComponent.clearError();
}

Hi Alexey, thanks for the quick response.  However, I am getting null value for the components field.  I have tried other fields also and all are null.  **I added logging to check that it was getting executed.  Any idea why this might be?

 

import static com.atlassian.jira.issue.IssueFieldConstants.*

def cfComponent = getFieldById(COMPONENTS);
def compValue = cfComponent.getFormValue()
log.debug ("***compValue = " + compValue )

if (compValue instanceof List) {
cfComponent.setError("Please select only ONE Component/QA Request Category");
} else {
cfComponent.clearError();
}

Hi Alexey, thanks for your quick response.  However, I am getting null for the COMPONENT.  I tried other fields also and still null.  **I used logging to determine why it is not executed.  Any ideas on why this might be?

 

import static com.atlassian.jira.issue.IssueFieldConstants.*

def cfComponent = getFieldById(COMPONENTS);
def compValue = cfComponent.getFormValue()
log.debug ("***compValue = " + compValue + "::"+ cfComponent.getValue())

if (compValue instanceof List) {
cfComponent.setError("Please select only ONE Component/QA Request Category");
} else {
cfComponent.clearError();
}

 

**my response keeps disappearing so hopefully you are getting spammed

Did you add a behaviour in ScriptRunner? Did you map the behaviour to the correct project and issuetypes? Did you see your log.debug() in atlassian log?

Yes I added a behaviour in ScriptRUnner.  Please see my screen shots...

 

Screen Shot 2017-11-30 at 11.28.10 AM.pngScreen Shot 2017-11-30 at 11.28.46 AM.pngScreen Shot 2017-11-30 at 11.30.29 AM.png

 

2017-11-30 11:22:45,487 http-nio-8080-exec-25 DEBUG W0017963 682x67004x1 1aj7tyb 172.21.132.162 /rest/com.onresolve.jira.plugin.Behaviours/1.0/behaviours/validators.json [c.o.j.groovy.user.FieldBehaviours] ***compValue = null::null

You added script to the wrong place.

Look, there is a link Add serverside script above Conditions None. You have to add there

Perfect! You're a genius ;) 

Thanks a lot.  I really appreciate it.

I tried this script today, but getting the message "too many components" if I only add one.

import static com.atlassian.jira.issue.IssueFieldConstants.*

def cfComponent = getFieldById(COMPONENTS);
def compValue = cfComponent.getFormValue()
if (compValue instanceof List) {
cfComponent.setError("Too many components chosen. Please select only one.");
} else {
cfComponent.clearError();
}

 

2020-03-02_01-33.png

Any idea why?

Hi Alexey, thanks for your quick response. I am posting here vs a reply cause it keeps disappearing.  Hoping this goes through...

I am getting null for the COMPONENT. I tried other fields also and still null. **I used logging to determine why it is not executed. Any ideas on why this might be?


import static com.atlassian.jira.issue.IssueFieldConstants.*
def cfComponent = getFieldById(COMPONENTS);
def compValue = cfComponent.getFormValue()
log.debug ("***compValue = " + compValue + "::"+ cfComponent.getValue())
if (compValue instanceof List) {
cfComponent.setError("Please select only ONE Component/QA Request Category");
} else {
cfComponent.clearError();
}

Hi,

Please try this Behaviors Script :

def ComponentsField = getFieldById("components")
def components = ComponentsField.getValue() as Collection
if (components.size() > 1) {
ComponentsField.setValid(false)
ComponentsField.setError("Component can only be one value.")
}
else {
ComponentsField.setValid(true)
ComponentsField.clearError()
}

2021-07-15 22_59_53-Window.png

 

Suggest an answer

Log in or Sign up to answer
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