Groovy Sript Runner - multi-select using contains

JoeR May 8, 2013

Trying to make a field required when a prior field is populated. We dont use the behaviours plug-in. The first field is a multi-select and I have a saved issue with 1 of the 3 values selected. The field is customfield_14523, the value is 15300 and the field label is Production.

Per https://answers.atlassian.com/questions/103549/validate-multi-select-customfield-in-transition

I'm trying this command using Workflow Validator Built-in script

cfValues['customfield_14523']*.value.contains("15300") && cfValues["customfield_14807"]*.value == ("")

After failing, I thought I'd test the first half of the script with Script Runner and the saved issue.

Go into Script Runner, enter the key of the issue, give it this command:

cfValues['customfield_14523']*.value.contains("15300")

false, get this...javax.script.ScriptException: java.lang.NullPointerException: Cannot invoke method contains() on null object...

The multi-select object isn't null, it has a value...15300, viewable in firebug with a label of "Production"

I also tried ? instead of *...Production instead of "15300", double quotes, single quotes...fail.

Javascript doesn't seem to like multi-selects either.

Please advise!

4 answers

1 accepted

0 votes
Answer accepted
JamieA
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.
May 8, 2013

It should be cfValues['Production']

Give the cf name, not the ID.

> You assume that every entry in collection cfValues['customfield_14523'] has property 'value'

I think this is a reasonable assumption if you know it's a multiselect, but yeah, it's good to print out the class type.

JoeR May 8, 2013

cfValues["Capsil Platforms"]*.value.contains('Production')

is the code I was looking for. Thank you!

0 votes
JoeR May 9, 2013

To conclude finally, but I'm still confused...

this below is what REALLY works in a workflow which differs from what works in the Script Runner Validator.

cfValues["Capsil Platforms"]*.value.contains('Production')
&&
cfValues["Required PROD Date"] == '';

If field Capsil Platforms has "Production" selected and the Required Prod Date is null, prompt the user to enter the Required Prod Date.

When I tested the second half in the Script runner tester on an existing issue using two single quotes, it returned false. Such as:

cfValues["Required PROD Date"] == ''

If I set it to use null, it returned true.

cfValues["Required PROD Date"] == null

But when I put the code with null in a workflow as a create transition, it doesn't fire...so we played around and had to use two single quotes instead of null.

The tester uses an existing issue, a create is different...so during entry a field that hasn't been entered is not necessary set to null until it is saved?

0 votes
JoeR May 8, 2013

Here's the html from firebug, I wasn't clear about labels previously, sorry.

So based on the html below, would the second statement be correct in your opinion?

cfValues["Capsil Platforms"]*.value.contains('15300')

or

cfValues["Capsil Platforms"]*.value.contains('Production')

<div class="field-group">
<label for="customfield_14523">
Capsil Platforms
<span class="aui-icon icon-required"></span>
<span class="content">Required</span>
</label>
<select id="customfield_14523" class="select" size="5" name="customfield_14523" multiple="multiple">
<option value="15300" selected="selected"> Production </option>
<option value="15301"> T2 - RLSA </option>
<option value="15302"> T3 - MODL </option>
</select>
<div class="description">Press and hold Ctrl to select multiple values </div>
</div>

Sidenote: Our script runner is having issues, it doesn't generate any samples when i click the sample links so I have create sample grails code while adding a validator in a workflow, then go back to the test validator, paste, run it in the tester. I have another person looking at this as well, we noticed it's randomly returning true and false so we're not sure our install is correct either. I'll try the log statement in a few.

JamieA
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.
May 8, 2013

The html is not relevant here (except to understand your options) ... the validation happens server-side.

The correct statement is the second one, presuming Production is the value of the option. From your earlier comments I assumed 15300 was the display value.

If you want to look it up by ID then something like:

cfValues["Capsil Platforms"]*.id.contains(15300)

(untested).

From the html it looks like "Production" contains leading and trailing spaces? Unless that's added in by jira. Which would explain why it's not working for you.

0 votes
Adam Marszałek
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.
May 8, 2013

Hey Joe,

By writing:
cfValues['customfield_14523']*.value.contains("15300")

You assume that every entry in collection cfValues['customfield_14523'] has property 'value' which seems to be false assumption. cfValues['key'] gives you some probably some kind of single value, and best solution to find out what is this value would be to place such a statement in validator:
log.warn cfValues['customfield_14523']
It should give you class name of object that lays inside this custom field. Then post it here or consult apropriate javadoc to find out your way.


Regards,
Adam

JoeR May 8, 2013

Thank you for the log tip. So it appears that entering the

log.warn cfValues['customfield_14523']

returned nothing

log.warn cfValues['Capsil Platforms']

returned Production which is correct since it was the only selected value of the three choices.

<font></font>

<font></font>

Suggest an answer

Log in or Sign up to answer