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
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)".
Select "Custom script validator" when creating the Validator.
As a result when transitioning to "IN PROGRESS" you will have the following if options.size() < 2 or no value is provided:
Kind regards,
Rafael
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
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.