Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scripted Behavior to restrict checkox selction upto 2 values on create screen

Sudheer Kumar Vempala May 3, 2023

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")

}

1 answer

0 votes
Fabio Racobaldo _Herzum_
Community Champion
May 3, 2023

Hi @Sudheer Kumar Vempala ,

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

Sudheer Kumar Vempala May 3, 2023

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
Fabio Racobaldo _Herzum_
Community Champion
May 3, 2023

Hey @Sudheer Kumar Vempala ,

I probably get your issue. Please could you provide information about checkbox options? Probably there are spaces in your options

Fabio Racobaldo _Herzum_
Community Champion
May 3, 2023

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")

}
Sudheer Kumar Vempala May 3, 2023

Hello Fabio

 

here is the list 

  • Yes- Regression Coverage candidate
  • Yes- Monitoring/ Alerts candidate
  • Regression- Under Evaluation
  • Regression- Out of scope
  • Regression- In Progress
  • Regression- Implemented (Automation)
  • Regression- Implemented (Manual)
  • Regression- On hold due to data dependency
  • Monitoring- Under Evaluation
  • Monitoring- Out of scope
  • Monitoring- In Progress
  • Monitoring- Implemented

 

I tired with another checkbox field same Issue I see. 

Fabio Racobaldo _Herzum_
Community Champion
May 3, 2023

Please try the script provided in my previous comment.

Fabio Racobaldo _Herzum_
Community Champion
May 3, 2023

hey @Sudheer Kumar Vempala , did u test it?

Sudheer Kumar Vempala May 3, 2023

Hello Fabio,

Thanks for your quick response and help.

Scrpt is working now. I will accept answer

Fabio Racobaldo _Herzum_
Community Champion
May 3, 2023

@Sudheer Kumar Vempala  thanks for your feedback

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.20.4
TAGS
AUG Leaders

Atlassian Community Events