Script Runner(validate) check if there is a particular version defined in a multiselect custom field of versions

Brent Webster
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 22, 2013

Using script runner as a workflow valdator, I need to check if there is a particular version defined in a multiselect custom field of versions (i.e. field: 'Planned For').

On the validation condition line, I've used the assert command to

assert cfValues['Planned For'] == "main"
           |            |                      |
           |            [main]              false
           [Customer:null, Planned For:[main], ...]

So 'Planned For' is an array but lets confirm:
assert cfValues['Planned For'].class == "main"
           |            |                     |         |
           |            [main]            |         false
           |                                  class java.util.ArrayList
           [Customer:null, ....

What are the elements of the array?
assert cfValues['Planned For'][0].class == "main"
           |            |                     |      |       |
           |            [main]            main|       false
           |                                          class com.atlassian.jira.project.version.VersionImpl
           [Customer:null, ....

I reviewed Jamie's page: https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts and tried various formats:

--> cfValues['Planned For'][0] == "main"
--> cfValues['Planned For'].contains("main")
--> cfValues['Planned For']*.value.contains("main")
--> cfValues['Planned For']?.value.contains("main")

What am I missing? Thanks, Brent




4 answers

1 accepted

1 vote
Answer accepted
Brent Webster
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 27, 2013

After much head banging for checking a custom field(Planned For) of Versions

Here's my condition of a validator where

if Manager then allow
else if Planned For is null then allow
else if Planned For only equals "main" version then allow
else if Planned For only equals "trunk" version then allow
else fails

isUserMemberOfRole('Managers') || cfValues['Planned For'] == null || cfValues['Planned For']*.getName() == ["main"] || cfValues['Planned For']*.getName() == ["trunk"]

or can use the contains method if one of the multiple version is set to xxxx. i.e. "main"
cfValues['Planned For']*.getName().contains("main")

0 votes
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 27, 2013

The examples were mostly for select/multiselects... unfort you were using a collection of Versions. Here would be where to look: https://developer.atlassian.com/static/javadoc/jira/5.2/reference/com/atlassian/jira/project/version/Version.html

0 votes
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 27, 2013

Just FYI, you don't need to write it all on the same line... the condition box will expand. Eg you might find this easier to read (I do):

if (isUserMemberOfRole('Managers')) {
    return true
}

// no versions specified        
if (! cfValues['Planned For']) {
    return true
}

if (["trunk", "main"].intersect(cfValues['Planned For']*.name)) {
    return true
}

return false

0 votes
Brent Webster
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 22, 2013

Well that looks like crap

I reviewed Jamie's page: https://jamieechlin.atlassian.net/wiki/display/GRV/Built-In+Scripts and tried various formats:

--> cfValues['Planned For'][0] == "main"
--> cfValues['Planned For'].contains("main")
--> cfValues['Planned For']*.value.contains("main")
--> <code class="java plain"></code>cfValues['Planned For']?.value.contains("main")

What am I missing? Thanks, Brent<br>

Suggest an answer

Log in or Sign up to answer