Set value in version picker

Dan27 July 25, 2018

Hi,

What is the code for setting a string value (text field) into "Fix version/s" (Version Picker)?

this String exists in the versions list.

 

thanks

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Dan27 July 26, 2018
0 votes
Alexey Matveev
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.
July 25, 2018

Hello,

The Fix Versions field is a system field, that is why you set it differently from Version Picker. 

You set fix version with the issue.setFIxVersions([version]) method. You set a custom field of the version type with issue.setCustomFieldValue(field, [value]) method. You can find more info here:

https://community.atlassian.com/t5/Jira-questions/How-to-set-the-value-of-fix-version-default-field-in-an-issue/qaq-p/295929

Dan27 July 25, 2018

Thank you @Alexey Matveev ! :)

Dan27 July 25, 2018

 

@Alexey Matveev ,

It's not working good for me... Capture2.PNGDo you have an idea ? 

Alexey Matveev
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.
July 26, 2018

What are the errors? Is it a post function? Is this post function first in the list of post funcitons? Is it the create issue transition?

Dan27 July 26, 2018

it is a script listener, on issue updated

Alexey Matveev
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.
July 26, 2018

I see. 

Then you should add this import:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

 

Then add the following lines to the end of the script:

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
Dan27 July 26, 2018

@Alexey Matveev I missed something...

Capture3.PNG

Alexey Matveev
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.
July 26, 2018

Can you provide errors and the whole script?

Dan27 July 26, 2018

@Alexey Matveev How can I provide errors ?

Alexey Matveev
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.
July 26, 2018

Hover the mouse over the ComponentAccessor, which is underlined in red. You will see errors. Make a screenshot.

Also provide the whole script

Dan27 July 26, 2018

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

//IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
//test

def issueManager = ComponentAccessor.getIssueManager();
Issue issue = event.getIssue();
//Issue issue = issueManager.getIssueObject("WBL-57061");

//Issue issue = event.getIssue();
def issueType=issue.getIssueType().name;
if (issueType=="Story" || issueType=="Bug-Internal" || issueType=="Bug-Customer" || issueType=="RFE"
|| issueType=="Sub-Test"
|| issueType=="Sub-Bug")
{
//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
def Parent = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Fix Version");
def ParentFixVersion = issue.getCustomFieldValue(Parent);

//Get Epic Link Key
def epic = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link");
def epicKey = issue.getCustomFieldValue(epic);

if(fixVersions[0]!=ParentFixVersion)
{
UserMessageUtil.error("Failed. Fix version of issue should be like the Epic's fix version");
//ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
//List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Fix Version")
//return changeList[0].toString.class;
issue.setFixVersions([ParentFixVersion])
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}Untitled4.pngUntitled5.png

Alexey Matveev
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.
July 26, 2018

What is your Jira version? Try to execute this script in a a listnere and have a look in the atlassian-jira.log file. 

Dan27 July 26, 2018

Jira v7.3.6.

What is the atlassian-jira.log file.  ?

Alexey Matveev
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.
July 26, 2018
Alexey Matveev
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.
July 26, 2018

Try to write like this and run the script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

//IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
//test

def issueManager = ComponentAccessor.getIssueManager();
def issue = ComponentAccessor.getIssueManager.getIssueObjectByCurrentKey(event.getIssue().getKey());
//Issue issue = issueManager.getIssueObject("WBL-57061");

//Issue issue = event.getIssue();
def issueType=issue.getIssueType().name;
if (issueType=="Story" || issueType=="Bug-Internal" || issueType=="Bug-Customer" || issueType=="RFE"
|| issueType=="Sub-Test"
|| issueType=="Sub-Bug")
{
//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
def Parent = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Fix Version");
def ParentFixVersion = issue.getCustomFieldValue(Parent);

//Get Epic Link Key
def epic = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link");
def epicKey = issue.getCustomFieldValue(epic);

if(fixVersions[0]!=ParentFixVersion)
{
UserMessageUtil.error("Failed. Fix version of issue should be like the Epic's fix version");
//ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
//List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Fix Version")
//return changeList[0].toString.class;
issue.setFixVersions([ParentFixVersion])
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
Alexey Matveev
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.
July 26, 2018
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

//IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
//test

def issueManager = ComponentAccessor.getIssueManager();
def issue = ComponentAccessor.getIssueManager().getIssueObjectByCurrentKey(event.getIssue().getKey());
//Issue issue = issueManager.getIssueObject("WBL-57061");

//Issue issue = event.getIssue();
def issueType=issue.getIssueType().name;
if (issueType=="Story" || issueType=="Bug-Internal" || issueType=="Bug-Customer" || issueType=="RFE"
|| issueType=="Sub-Test"
|| issueType=="Sub-Bug")
{
//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
def Parent = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Fix Version");
def ParentFixVersion = issue.getCustomFieldValue(Parent);

//Get Epic Link Key
def epic = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link");
def epicKey = issue.getCustomFieldValue(epic);

if(fixVersions[0]!=ParentFixVersion)
{
UserMessageUtil.error("Failed. Fix version of issue should be like the Epic's fix version");
//ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
//List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Fix Version")
//return changeList[0].toString.class;
issue.setFixVersions([ParentFixVersion])
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
Dan27 July 26, 2018

The same error :( start with this line:

def issue = ComponentAccessor.getIssueManager().getIssueObjectByCurrentKey(event.getIssue().getKey());

Alexey Matveev
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.
July 26, 2018

What error? No such property? not possible

Dan27 July 26, 2018

Untitled7.png

Alexey Matveev
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.
July 26, 2018
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

//IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
//test

def issueManager = ComponentAccessor.getIssueManager();
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(event.getIssue().getKey());
//Issue issue = issueManager.getIssueObject("WBL-57061");

//Issue issue = event.getIssue();
def issueType=issue.getIssueType().name;
if (issueType=="Story" || issueType=="Bug-Internal" || issueType=="Bug-Customer" || issueType=="RFE"
|| issueType=="Sub-Test"
|| issueType=="Sub-Bug")
{
//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
def Parent = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Fix Version");
def ParentFixVersion = issue.getCustomFieldValue(Parent);

//Get Epic Link Key
def epic = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link");
def epicKey = issue.getCustomFieldValue(epic);

if(fixVersions[0]!=ParentFixVersion)
{
UserMessageUtil.error("Failed. Fix version of issue should be like the Epic's fix version");
//ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
//List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Fix Version")
//return changeList[0].toString.class;
issue.setFixVersions([ParentFixVersion])
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
Dan27 July 26, 2018

Ok @Alexey Matveev, Now the setFixVersion has error:

Untitled8.png

Alexey Matveev
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.
July 26, 2018

You can skip this error. It will work just fine

Alexey Matveev
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.
July 26, 2018

Or do like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.version.Version
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

//IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)
//test

def issueManager = ComponentAccessor.getIssueManager();
def issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(event.getIssue().getKey());
//Issue issue = issueManager.getIssueObject("WBL-57061");

//Issue issue = event.getIssue();
def issueType=issue.getIssueType().name;
if (issueType=="Story" || issueType=="Bug-Internal" || issueType=="Bug-Customer" || issueType=="RFE"
|| issueType=="Sub-Test"
|| issueType=="Sub-Bug")
{
//Get fix versions of issue
Collection<Version> fixVersions = new ArrayList<Version>();
fixVersions = issue.getFixVersions();
def Parent = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Parent Fix Version");
def ParentFixVersion = issue.getCustomFieldValue(Parent) as Version;

//Get Epic Link Key
def epic = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link");
def epicKey = issue.getCustomFieldValue(epic);

if(fixVersions[0]!=ParentFixVersion)
{
UserMessageUtil.error("Failed. Fix version of issue should be like the Epic's fix version");
//ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager();
//List<ChangeItemBean> changeList = changeHistoryManager.getChangeItemsForField(issue, "Fix Version")
//return changeList[0].toString.class;
issue.setFixVersions([ParentFixVersion])
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
ComponentAccessor.getIssueManager().updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
}
}
Dan27 July 26, 2018

It didn't change the fix version...

Alexey Matveev
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.
July 26, 2018

Could you remove the if condition? I think it does not pass your if conditions. Also you need to use logging to find what is going on in your script. 

https://www.adaptavist.com/doco/display/SFJ/Set+logging+to+help+debug+your+scripts

Dan27 July 26, 2018

The if condition is passed, cause the error message that inside the condition is happened

Dan27 July 26, 2018

The log:

2018-07-26 10:57:54,345 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-07-26 10:57:54,345 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.ClassCastException: java.lang.String cannot be cast to com.atlassian.jira.project.version.Version
 at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueValue(DefaultVersionManager.java:577)
 at com.atlassian.jira.project.version.DefaultVersionManager.updateIssueFixVersions(DefaultVersionManager.java:565)
 at com.atlassian.jira.issue.fields.FixVersionsSystemField.updateIssueValue(FixVersionsSystemField.java:98)
 at com.atlassian.jira.issue.fields.AbstractVersionsSystemField.updateValue(AbstractVersionsSystemField.java:372)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateFieldValues(DefaultIssueManager.java:704)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:669)
 at com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue(DefaultIssueManager.java:655)
 at com.atlassian.jira.issue.managers.RequestCachingIssueManager.updateIssue(RequestCachingIssueManager.java:214)
 at com.atlassian.jira.issue.IssueManager$updateIssue$1.call(Unknown Source)
 at Script3832.run(Script3832.groovy:47)
Dan27 July 26, 2018

and:

2018-07-26 11:50:59,716 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2018-07-26 11:50:59,717 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: <inline script>
java.lang.NoClassDefFoundError: groovy/lang/GroovyObject
 at Script3906.run(Script3906.groovy:33)
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runScriptAndGetContext(ScriptRunnerImpl.groovy:156)
 at com.onresolve.scriptrunner.runner.ScriptRunner$runScriptAndGetContext$0.callCurrent(Unknown Source)
 at com.onresolve.scriptrunner.runner.ScriptRunnerImpl.runStringAsScript(ScriptRunnerImpl.groovy:144)
 at com.onresolve.scriptrunner.runner.ScriptRunner$runStringAsScript.call(Unknown Source)
 at com.onresolve.scriptrunner.canned.jira.workflow.listeners.CustomListener.doScript(CustomListener.groovy:118)
Caused by: java.lang.ClassNotFoundException: groovy.lang.GroovyObject
 ... 6 more
TAGS
AUG Leaders

Atlassian Community Events