The Atlassian Community Forums are currently in read-only mode. We will be relaunching on a new platform on September 22 (read more here). We apologize for the extended downtime. For concerns or questions, please email communitymanagers@atlassian.com. See you on the other side, on the new Atlassian Community Forums! :)

×

Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Scriptrunner Formfield affected version

Niklas Kordasch
November 22, 2021

Dear Community,

I would need some help trying to count the elements from a formfield. 

I set up a behaviour from scriptrunner, which should only allow one affected version in the ticket. 

The list has always the size 0

 This is my code:

 

FormField affectedVersions = getFieldById(AFFECTED_VERSIONS)
List<ProjectComponent> components = affectedVersions.getValue() as List

if (components.size() > 1) {
affectedVersions.setError("Only one affected Version is allowed!")
}
else { affectedVersions.clearError() }

 

Thanks :)

Niklas

 

1 answer

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

1 vote
Answer accepted
PD Sheehan
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 Champions.
November 22, 2021

Don't mix apples and oranges... and don't try to assign affected versions to project components.

That's probably where your problem lies.

Also, you would not get into this sort of trouble if you used the soft-typing in groovy. Try this:

def affectedVersionFld = getFieldByID(AFFECTED_VERSION)
affectedVersionFld.clearError()
def affectedVersions = affectedVersionsFld.value as List
if(affectedVersions.size() > 1){
affectedVersion.setError("Only on Affected Version is allowed!")
}
Niklas Kordasch
November 22, 2021

Thank you Peter, 

 

I tried the code, but i get the same result.

my testcode is:

def affectedVersionFld = getFieldById(AFFECTED_VERSIONS)
affectedVersionFld.clearError()
def affectedVersions = affectedVersionFld.value as List
if(affectedVersions.size() > 1){
affectedVersionFld.setError("Only on Affected Version is allowed!")
}
affectedVersionFld.setError(affectedVersions.size()+"")

 

Result:Test.PNG

Niklas Kordasch
November 22, 2021

It won't work maybe because I want to check the size, when i create an issue.

PD Sheehan
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 Champions.
November 23, 2021

Where did you put this code?  It needs to be in the "Affects Versions" config:

2021-11-23 07_59_31-Window.png

2021-11-23 08_00_29-Window.png

Niklas Kordasch
November 23, 2021

Oh that was the problem, big thank you :)