It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Validation for list size does not work

Problem

I have a multi-value field which in some situations I want to verify the user selected only one value or no value

What I did

I am using script runner for this 

cfValues['Device Related']*.values().size() <= 1

So as I understand it the user can choose one or none 

But the script always fail validation and the user can not continue

What I am doing wrong

 

1 answer

1 accepted

0 votes
Answer accepted

Hi @Nehemia Litterat ,

Perhaps you could rewrite your behaviour as follows:

import com.atlassian.jira.component.ComponentAccessor;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import com.atlassian.jira.issue.customfields.option.Option;
import groovy.transform.BaseScript;

import org.apache.log4j.Category;

Category log = Category.getInstance("com.onresolve.stash.groovy");
log.setLevel(org.apache.log4j.Level.INFO);

@BaseScript FieldBehaviours fieldBehaviours;

def customField = getFieldById(getFieldChanged());
def customFieldOptions = customField.getValue();

if(customFieldOptions) {
List<Option> options = (List<Option>) customFieldOptions;
log.info("options.size() - " + options.size());
}

Then, you will have more flexibility to work with "com.atlassian.jira.issue.customfields.option.Option".

Kind regards,
Rafael

Hi 

Thanks for the reply

I used your code and wrote the following  (I am running this as validation script not behavior) 

//cfValues['Device Related']*.values().size() <= 1

import com.atlassian.jira.component.ComponentAccessor;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import com.atlassian.jira.issue.customfields.option.Option;
import groovy.transform.BaseScript;

import org.apache.log4j.Category;

Category log = Category.getInstance("com.onresolve.stash.groovy");
log.setLevel(org.apache.log4j.Level.INFO);

@BaseScript FieldBehaviours fieldBehaviours;

def customField = getFieldById("customfield_10901");
def customFieldOptions = customField.getValue();

log.error("Nehemia litterat cfValues['Device Related']*.values().size():"+cfValues['Device Related']*.values().size());
log.error("Nehemia litterat customFieldOptions " + customFieldOptions);

if(customFieldOptions) {
List<Option> options = (List<Option>) customFieldOptions;
log.error("Nehemia litterat validation options.size= " + options.size());
options.size() <2
} else {

true
}

The validation still does not work and I do not see anything in the logs

I am downloading the logs from the Troubleshooting and support tools  

and I am searching in all the logs files

 

any thoughts as to what I am doing wrong?

Hi @Nehemia Litterat ,

If you want to use validators whilst transitioning a ticket from "TODO" to "IN PROGRESS" as an example, then you could make use of this script instead:

import com.atlassian.jira.component.ComponentAccessor;
import com.onresolve.jira.groovy.user.FieldBehaviours;
import com.atlassian.jira.issue.customfields.option.Option;
import groovy.transform.BaseScript;
import com.opensymphony.workflow.InvalidInputException;

import org.apache.log4j.Category;

Category log = Category.getInstance("com.onresolve.stash.groovy");
log.setLevel(org.apache.log4j.Level.INFO);

@BaseScript FieldBehaviours fieldBehaviours;

def customFieldMamager = ComponentAccessor.getCustomFieldManager();
def customFieldOptions = issue.getCustomFieldValue(customFieldMamager.getCustomFieldObjectByName("My Select List"));

if(customFieldOptions) {
List<Option> options = (List<Option>) customFieldOptions;
log.info("options.size() " + options.size());
if(options.size() < 2) {
throw new InvalidInputException("More options should be selected.");
}
return true;
} else {
// if no value has been provided
throw new InvalidInputException("More options should be selected.");
}

"My Select List" is the name of my custom field of type "Select List (multiple choices)".

Screenshot 2019-09-12 at 20.06.31.png

Select "Custom script validator" when creating the Validator.

Screenshot 2019-09-12 at 20.09.18.png

 

As a result when transitioning to "IN PROGRESS" you will have the following if options.size() < 2 or no value is provided:

Screenshot 2019-09-12 at 20.12.52.png

Kind regards,
Rafael

Many thanks now it works

to tell you the truth I do not really understand why :(

What is the difference from getting the values perspective (less the actual logic)

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

303 views 1 3
Join discussion

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you