Need to restrict Component/s to single select using ScriptRunner

Tammy Robinson November 28, 2017

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.

4 answers

4 votes
Santhosh Kumar Arogyaswamy July 15, 2021

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

 

Sergey Kislitsyn August 24, 2021

In 2021 it is the working solution.

Alexey's script doesn't work because components.getValue() always return a List, even if there is only one component choosen

Sylvain Leduc September 22, 2023

Thanks for this behavior, working with JSW 8.20 !

0 votes
Huseyin Melih Altın
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!
December 13, 2023

Thank you all friends, it was really helpful for me.

Best regards.

0 votes
Tammy Robinson November 29, 2017

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();
}

0 votes
Alexey Matveev
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.
November 28, 2017

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();
}
Tammy Robinson November 29, 2017

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();
}

Tammy Robinson November 29, 2017

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();
}

Tammy Robinson November 29, 2017

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

Alexey Matveev
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.
November 29, 2017

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?

Tammy Robinson November 30, 2017

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

Alexey Matveev
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.
November 30, 2017

You added script to the wrong place.

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

Tammy Robinson November 30, 2017

Perfect! You're a genius ;) 

Thanks a lot.  I really appreciate it.

Jiri Kanicky March 1, 2020

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?

Suggest an answer

Log in or Sign up to answer