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

How set custom field value in Issue Create event Edited

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

0 votes
Answer accepted

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

 

Thanks a lot, it works.

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

Demo Den Ep. 7: New Jira Cloud Reports

Learn how to use two new reports for next-gen projects in Jira Cloud:  Cumulative flow diagram and Sprint burndown chart. Ivan Teong, Product Manager, Jira Software, demos the Cumulative ...

240 views 1 2
Join discussion

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