How to set the value of fix version default field in an issue through groovy script?

I have added a custom field for target version and when i create the issue, I can see the target version as the correct release version but the Fix Version built in one is set as None. What should I write in my script to make that the same as the value in my custom field? I have the version object as well as the version name string, but what exactly should be the line of code to set the fix version as that object ?

4 answers

1 accepted

0 votes
Accepted answer
Vasiliy Zverev Community Champion Nov 24, 2016

Dear @Shagufta Gurmukhdas, if you hava version object then to set this only version you can use this

issue.setFixVersions(Arrays.asList(version))

Note, that all other version will be deleted.

Worked, thanks a lot! @Vasiliy Zverev btw i am able to write this on the create postfunction, for the subtasks created for the issue currently being created. But for this parent issue, writing this same code doesnt work. Nor does anything else, like setting the summary etc. (in the create postfunction of issue being created) How to set the fields of the current issue?

Vasiliy Zverev Community Champion Nov 24, 2016

On create transition it is requred:

  1. Place post-function after postfunciton to create original issue 
  2. Add code to store changes

    ComponentAccessor.getIssueManager().updateIssue(
            ComponentAccessor.getJiraAuthenticationContext().getUser()
            , issue
            , UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false).build()
    )

 

Here issue is the original issue? or the one with updated fields?

Yes, I did that, but somehow this current issue still doesnt get updated..

Vasiliy Zverev Community Champion Nov 24, 2016

Could you provide full code for this post-fucntion?

This is related to my other question..I want to update the current issue's version

 

import com.atlassian.jira.project.version.Version 
import com.atlassian.jira.project.version.VersionManager 
import com.atlassian.jira.issue.fields.CustomField 
import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.project.Project 
VersionManager versionManager = ComponentAccessor.versionManager 
def customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField versionCF = customFieldManager.getCustomFieldObjectByName("Version/Release") 
String valueOfversionType = issue.getCustomFieldValue(versionCF)
String versionstring=valueOfversionType.substring(1,valueOfversionType.size()-1) 
Version versionn = versionManager.getVersion(issue.getProjectId(), versionstring) 
issue.setFixVersions(Arrays.asList(versionn)) 
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser() 
ComponentAccessor.getIssueManager().updateIssue(user, issue, com.atlassian.jira.event.type.EventDispatchOption.DO_NOT_DISPATCH, false)

 

@Vasiliy Zverev

Whether I do this for the version or any other field like summary, description etc, it doesn't update the current issue's field. (The one for which the post function is being written)

 

@Vasiliy Zverev Even a simple statement like this doesnt work for the current issue

issue.description = "abc"

I could simply put it in a post function for updating field( the default inbuilt postfunction) but i need to make this conditional, i.e dependent on some other field, hence I want to do it manually, but this isnt working sad (I am on the cloud instance)

Oh! I got the answer! laugh The postfunction is supposed to be put before that of the issue actually being created. laugh laugh laugh laugh

All of the coded posted above will not work on Cloud as the API is totally different. The order of post functions in the cloud is not guaranteed as they are processed asynchronously.

Hi Shagufta,

The following code will set the fix version in ScriptRunner for JIRA Cloud for the issue with key SAMPLE-1

def issueKey = 'SAMPLE-1'
def result = put('/rest/api/2/issue/' + issueKey)
        .header('Content-Type', 'application/json')
        .body([
        fields:[
                fixVersions: [[name: 'Version 1.0.1']]
        ]
])
        .asString()
if (result.status == 204) {
    println 'Successfully set version to 1.0.1'
} else {
    println "Failed to set version to 1.0.1 ${result.status}: ${result.body}"
}

Regards, Jon

@Vasiliy Zverev or anyone else can you help?

In the Script Runner for JIRA Cloud documentation is an example groovy script to calculate value of one custom field based on another as a Script listener. You can create new script listener to listen to Issue created event and based on the example set custom field value. Or if you want to copy custom field value once you can use Built-in script. Please have a look if that helps. 

Suggest an answer

Log in or Sign up to answer
Community showcase
Published Nov 27, 2018 in Portfolio for Jira

Introducing a new planning experience in Portfolio for Jira (Server/DC)

In the past, Portfolio for Jira required a high degree of detail–foresight that was unrealistic for many businesses to   have–in   order to produce a reliable long-term roadmap. We're tur...

2,800 views 18 22
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you