Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

Set value in version picker Edited

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

0 votes

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

 

@Alexey Matveev _cPrime_ ,

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

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?

it is a script listener, on issue updated

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)

Can you provide errors and the whole script?

@Alexey Matveev _cPrime_ How can I provide errors ?

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

Also provide the whole 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();
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

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

Jira v7.3.6.

What is the atlassian-jira.log file.  ?

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)
}
}
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)
}
}

The same error :( start with this line:

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

What error? No such property? not possible

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)
}
}

Ok @Alexey Matveev _cPrime_, Now the setFixVersion has error:

Untitled8.png

You can skip this error. It will work just fine

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)
}
}

It didn't change the fix version...

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

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

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)

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase

The benefits of using Jira in different departments

Jira is a great tool to use across different departments. Forget that paperwork – switch to Jira and get that tasks done smoothly. Marketing Jira allows for a complete digital transformation of you...

71 views 0 3
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