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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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

 

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

Thanks for this behavior, working with JSW 8.20 !

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

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

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.
Nov 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?

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.
Nov 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

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?

Suggest an answer

Log in or Sign up to answer