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

Shagufta Gurmukhdas November 23, 2016

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
Answer accepted
Vasiliy Zverev
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.
November 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.

Shagufta Gurmukhdas November 24, 2016

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
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.
November 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()
    )

 

Shagufta Gurmukhdas November 24, 2016

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

Vasiliy Zverev
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.
November 24, 2016
Shagufta Gurmukhdas November 24, 2016

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

Vasiliy Zverev
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.
November 24, 2016

Could you provide full code for this post-fucntion?

Shagufta Gurmukhdas November 27, 2016

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

Shagufta Gurmukhdas November 27, 2016

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)

Shagufta Gurmukhdas November 27, 2016

 

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

issue.description = "abc"
Shagufta Gurmukhdas November 27, 2016

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)

Shagufta Gurmukhdas November 27, 2016

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

Jon Mort [Adaptavist]
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.
November 28, 2016

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.

4 votes
Jon Mort [Adaptavist]
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.
November 28, 2016

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

0 votes
Martin Brehovsky November 28, 2016

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. 

0 votes
Shagufta Gurmukhdas November 23, 2016

@Vasiliy Zverev or anyone else can you help?

Suggest an answer

Log in or Sign up to answer