3

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.

1 Answer 1

9

I just got an answer from Adaptavist that is 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)
Sign up to request clarification or add additional context in comments.

2 Comments

I have used this method and it works well. Cheers Kevin!
@Kevin May you please accept your own answer. Thank you in advance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.