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

Update Custom Field Value using a “Scriptrunner for Jira” Custom Listener

Kevin Sep 13, 2016

 

 

Hello we are using Jira and are currently evaluating the Plugin "Scriptrunner for Jira" by Adaptavist.

I'd like to create a custom Listener which simply updates the value of a custom field. The field's type is a default textbox, nothing fancy there.

Regarding to the plugin's documentation and various web-searching, I came up with the following code:

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue 
 
def issue = event.issue as Issue 
MutableIssue issueToUpdate = (MutableIssue) issue
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
 
def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'My CustomField'} 
issueToUpdate.setCustomFieldValue(cf, "myvalue")

The validator does not complain about anything here and the script seems to be executed without any errors. The problem is that the custom field's value is simply not updated. Maybe some of you guys have the missing piece.

Every line seems to be needed as the validator complains otherwise. Thank you in advance for your help.

 

//Edit

I just got an answer from Adaptavist that got my code finally working. Please find the working code below:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "My CustomField"}
def changeHolder = new DefaultIssueChangeHolder()
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), "myvalue"),changeHolder)

3 answers

1 accepted

1 vote
Answer accepted
Nic Brough [Adaptavist] Community Leader Sep 13, 2016

What are you putting in the validator part?  You've talked about it, but not mentioned how you've configured it.

Also, can you confirm what type of field "My Custom Field" is - definitely a string?

Kevin Sep 13, 2016

Hello!

As this is a custom listener. I don't see any validator part besides the script itself. Please see the screenshot below:

custom_listener.png

 

I can confirm that the type of field is a "Text Field (single line)" applied to "Issue type(s):Global (all issues)".

 

Nic Brough [Adaptavist] Community Leader Sep 13, 2016

I see one of my colleagues has solved it for us smile

Suresh Jan 08, 2018

may i know how to update Checkbox = Yes, an issue update.

0 votes
Marco Hödtke Jul 11, 2017

Kevin, can you please specify which version of Script runner you are using? In the screen shot I see that there is a field for Inline Scripting. I user version 3.0.6 of the ScriptRunner and there is no such field.

0 votes
sylvesp Mar 15, 2019

I've just ran into the same exact issue... with the specifics that the problem only happens with NextGen Projects.

It works well with classic projects.

Seems to be a bug in the Jira rest API for NextGen projects?

https://community.atlassian.com/t5/Jira-questions/Problems-with-custom-fields/qaq-p/1028540#U1033707

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Here’s what we’re doing to make Jira Cloud faster

Hello Community 🤗 I’m Nikhil, a Product Manager on the Jira Cloud team working on performance improvements. Performance is an ongoing journey and we continue to invest heavily in it. We under...

137 views 1 19
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