Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,459,450
Community Members
 
Community Events
176
Community Groups

Script runner - version selection

Hi, 
A beginner - question regarding the script runner.
The code below correctly returns the version with the highest ID (the last created version). 

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.*

if (getActionName() != "Create Issue") {
return
}

def versionManager = ComponentAccessor.getVersionManager()
def versions = versionManager.getVersions(issueContext.projectObject)
if (versions)
{
getFieldById(AFFECTED_VERSIONS).setFormValue([versions.last().id])
}

How to properly enhance this script to show the last created version, but only taking versions that start with / that have, e.g. "RC_*" in their name? 

I'm hoping for your help after many hours invested in learning and testing methods proposed for slightly different cases - which unfortunately returned () for me.

Thanks a lot!

1 answer

1 accepted

0 votes
Answer accepted

Hi @Ellie M_

For your requirement, you could add a basic if/else filter to your code as shown below:-

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

import static com.atlassian.jira.issue.IssueFieldConstants.*

@BaseScript FieldBehaviours behaviours

def versionManager = ComponentAccessor.versionManager
def affectedVersions = getFieldById(AFFECTED_VERSIONS)

def versions = versionManager.getVersions(issueContext.projectObject)

def filteredVersions = [] as List<Long>


versions.each {
if(it.name.startsWith("RC_")) {
filteredVersions.add(it.id)
}
}

affectedVersions.setFormValue(filteredVersions.last())

Please note, this sample code is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a print screen of the versions available for testing:-

rc_image.png

Below is a print screen of the sample test. As expected, the latest RC version is selected.

image1.png

I hope this helps to solve your question. :)

Thank you and Kind Regards,

Ram

It absolutely helped, Ram!
With some fine-tuning after testing, it now perfectly serves the need. 

Thank you so much for the prompt help. Much appreciated!

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events