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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

How can I update the FixVersion after the issue is created using a scriptrunner post function?

I used the following to set the FixVersion based on the ScriptRunner documentation available here:

https://scriptrunner.adaptavist.com/latest/jira/recipes/workflow/postfunctions/set-issue-attributes.html

import com.atlassian.jira.component.ComponentAccessor

def versionManager = ComponentAccessor.getVersionManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def project = issue.getProjectObject()
def targetVersion = "FixVersionNameHere"
def version = versionManager.getVersion(project.getId(), targetVersion.toString())

issue.setFixVersions([version])

It worked when placed before the Create Issue event (commit to the database), as expected. I see in the ScriptRunner docs that it says "If you need your function to run after that, you have to use a more complex method."

Any ideas what that "more complex method" looks like?

ScriptRunner version 5.6.2

1 answer

1 accepted

0 votes
Answer accepted

@Kmmaughs 

You need to use update issue function

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Logger


def log = Logger.getLogger("com.onresolve.jira.groovy")
def userManager = ComponentAccessor.getUserManager()
def issueManager = ComponentAccessor.getIssueManager()

def user = userManager.getUserByName("UserName")
def versionManager = ComponentAccessor.getVersionManager()
def project = issue.getProjectObject()
def targetVersion = "FixVersionNameHere"
def version = versionManager.getVersion(project.getId(), targetVersion.toString())

issue.setFixVersions([versions])

//Lower level doesn't require permission
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)

Thank you! I think your example works, though admittedly I went a slightly different route and used a Script Listener. I assigned FixVersions to Epics (the relationship was one-to-one). Then, the Epic Links/Name were determined based on some criteria in my script. Towards the end of the script, with the Epic Key stored as strEpicKey, I set the FixVersion. The script ended up looking something like this:

import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventType
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.event.issue.field.CustomFieldUpdatedEvent

def strEpicKey = "Some Epic Key (Ex:PROJ-123)"

def epicIssue = issueManager.getIssueObject(strEpicKey) 
def epicLink = customFieldManager.getCustomFieldObjectsByName("Epic Link")[0]
def versionManager = ComponentAccessor.getVersionManager() 
def project = issue.getProjectObject() 
epicLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(epicLink), epicIssue),new DefaultIssueChangeHolder())

def targetVersion = issueManager.getIssueByCurrentKey(strEpicKey).getFixVersions()[0]

def field = ComponentAccessor.fieldManager.getOrderableField("fixVersions")
field.updateValue(null, issue, new ModifiedValue(issue.fixVersions, [targetVersion]),new DefaultIssueChangeHolder())

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Software

How to create Jira issus from Excel file?

When to use CSV importer When managing your processes in Jira, there are many occasions where you need to create a lot of tasks. Creating them one by one will cost you a lot of time and effort and i...

4,304 views 22 31
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you