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.
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()
}
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thank you all friends, it was really helpful for me.
Best regards.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes I added a behaviour in ScriptRUnner. Please see my screen shots...
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 must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You added script to the wrong place.
Look, there is a link Add serverside script above Conditions None. You have to add there
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Perfect! You're a genius ;)
Thanks a lot. I really appreciate it.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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();
}
Any idea why?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.