You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
Hello Commity,
I am tring to restrict a checkbox field name "Flagged" to allow upto 2 values on create screen. The following script is working for when user select 2 options and throwing error if they select more than 2 values.
But script is aslo throwing error if they seclect 1 value and not select any value.
looking for some help to optimize it.
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def field = getFieldById(getFieldChanged())
def ComponentsField = getFieldByName("Flagged")
def selectedValues = field.getValue() as List<String>
if (selectedValues.size() <= 2){
ComponentsField.setValid(true)
ComponentsField.clearError()
}
else
//if (selectedValues.size() > 2)
{
ComponentsField.setValid(false)
ComponentsField.setError("Maximum 2")
}
please confirm that you put that code to Flagged field.
Then try the following code :
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def field = getFieldById(getFieldChanged())
def selectedValues = field.getValue() as List<String>
if (selectedValues == null || selectedValues.size() <= 2){
field .setValid(true)
field .clearError()
}
else
//if (selectedValues.size() > 2)
{
field .setValid(false)
field .setError("Maximum 2")
}
Please let me know,
Fabio
Hello Fabio,
Thanks for the quick reponse.
Yes correct script is mapped to "Flagged" field.
I have tired the updated script with additon of "null" now script is working 0-selection and 2 but the weird part is still 1-sections is tworing error.
I tired differnt version where count <=1 code is failing if user selcects one option.
if (selectedValues == null || selectedValues.size() <= 1
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey @Sudheer Kumar Vempala ,
I probably get your issue. Please could you provide information about checkbox options? Probably there are spaces in your options
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Try this code :
import static com.atlassian.jira.issue.IssueFieldConstants.*
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
def field = getFieldById(getFieldChanged())
def selectedValues = field.getValue() as ArrayList<String>
if (selectedValues == null || selectedValues.size() <= 2){
field .setValid(true)
field .clearError()
}
else
//if (selectedValues.size() > 2)
{
field .setValid(false)
field .setError("Maximum 2")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Fabio
here is the list
I tired with another checkbox field same Issue I see.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please try the script provided in my previous comment.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hey @Sudheer Kumar Vempala , did u test it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hello Fabio,
Thanks for your quick response and help.
Scrpt is working now. I will accept answer
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Sudheer Kumar Vempala thanks for your feedback
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.