How set custom field value in Issue Create event

aas October 31, 2019

Hello everybody. I try to make Script Listener, which in the time of issue creation make a Issue in another instance of jira via REST and I want to parse response to get ID of created issue and set that ID in to custom field, but I can't. Is it possible to set value in custom field via script listener when Issue Create event happens?   

def issue = event.getIssue()

def key = issue.key

MutableIssue mI = ComponentAccessor.getIssueManager().getIssueByCurrentKey(key)

mI.setCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Issue ID"),"here is ID from response")

And why I can't use this

def issue = event.getIssue()

issue.setCustomFieldValue(cF, value)

 

1 answer

1 accepted

1 vote
Answer accepted
fjodors
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.
October 31, 2019

Hi

1) event.issue returns "Issue" interface, it doesn't have setCustomFieldValue methoid.
You need "MutableIssue" interface you want to update this issue.
2) From API
https://docs.atlassian.com/software/jira/docs/api/8.1.0/com/atlassian/jira/issue/MutableIssue.html

setCustomFieldValue
void setCustomFieldValue(CustomField customField, Object value)
Sets a custom field value on this Issue Object, but does not write it to the database. This is highly misleading. To actually set a custom field value, use OrderableField.updateIssue(com.atlassian.jira.issue.fields.layout.field.FieldLayoutItem, MutableIssue, java.util.Map)

So you need "updateIssue" method from IssueManager

def CF = customFieldManager.getCustomFieldObject(<fieldID>);
MutableIssue issueToUpdate = (MutableIssue) event.issue;
issueToUpdate.setCustomFieldValue(CF, <newValue>);
issueManager.updateIssue(event.getUser(), issueToUpdate, EventDispatchOption.ISSUE_UPDATED, false);

 

aas October 31, 2019

Thanks a lot, it works.

Suggest an answer

Log in or Sign up to answer