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.

2 answers

0 votes
Alexey Matveev Community Champion 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 Community Champion 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 Community Champion 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.

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

Suggest an answer

Log in or Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,982 views 12 18
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot