Validation for list size does not work

Nehemia Litterat September 12, 2019

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
Rafael Pinto Sperafico
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 12, 2019

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

Nehemia Litterat September 12, 2019

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?

Rafael Pinto Sperafico
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 12, 2019

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

Nehemia Litterat September 12, 2019

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